1

In the following summation:

enter image description here

I need to replace s(dot product) with the following:

enter image description here

My code, which is extremely slow:

    summing = 0

    update = np.zeros((5172,))

    for index, xi in enumerate(x_v1):
        yval = y[index]
        kernel_sum = 0
        for index_val, x_val in enumerate(x_v1):
            kernel_sum += update[index_val] *(np.dot(x_val, xi.transpose()) + 1)**2
        dot_product = kernel_sum
        dot_product = np.clip(dot_product, -20, 20)



        summing += yval * np.log(sigmoid(dot_product)) + (1-yval)* np.log(1-sigmoid(dot_product))

How can I make this faster?

Jobs
  • 2,807
  • 4
  • 21
  • 46
  • Have you looked at using [`numpy.einsum`](https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.einsum.html)? [Here's a nice answer](https://stackoverflow.com/questions/26089893/understanding-numpys-einsum) explaning to use it. – wflynny Mar 31 '16 at 17:33
  • 1
    If your code is working and you need some hints on how to improve it, you might want to move such question to CodeReview (http://codereview.stackexchange.com/) – AlessioX Mar 31 '16 at 17:44

1 Answers1

0

You should completely remove the for-loop ("vectorize the code", as they say in Matlab...). The best way to do this is to first formulate your equations in matrix form and then try to write them using numpy only.

For example, you can compute your entire quadratic kernel using only numpy operations as follows:

K = (x_v1.dot(x_v1.T) + rho) ** 2

which will already reduce the computation time substantially.

Tomer Levinboim
  • 932
  • 11
  • 18
  • Thanks. Can you also answer this? http://stackoverflow.com/questions/36457849/building-a-decision-tree – Jobs Apr 06 '16 at 17:28