On 18 January 2014 20:51, Kevin K <richyoke...@gmail.com> wrote: > I have some code that I need help vectorizing. > I want to convert the following to vector form, how can I? I want to get rid > of the inner loop - apparently, it's possible to do so. > X is an NxD matrix. y is a 1xD vector. > > def foo(X, y, mylambda, N, D, epsilon): > ... > for j in xrange(D): > aj = 0 > cj = 0 > for i in xrange(N): > aj += 2 * (X[i,j] ** 2) > cj += 2 * (X[i,j] * (y[i] - w.transpose()*X[i].transpose() + > w[j]*X[i,j]))
As Peter said the y[i] above suggests that y has the shape (1, N) or (N, 1) or (N,) but not (1, D). Is that an error? Should it actually be y[j]? You don't give the shape of w but I guess that it is (1, D) since you index it with j. That means that w.transpose() is (D, 1). But then X[i] has the shape (D,). Broadcasting those two shapes gives a shape of (D, D) for cj. OTOH if w has the shape (D, 1) then cj has the shape (1, D). Basically your description is insufficient for me to know what your code is doing in terms of all the array shapes. So I can't really offer a vectorisation of it. > > ... > > If I call numpy.vectorize() on the function, it throws an error at runtime. You've misunderstood what the numpy.vectorize function is for. The vectorize function is a convenient way of generating a function that can operate on arrays of arbitrary shape out of a function that operates only on scalar values. Oscar -- https://mail.python.org/mailman/listinfo/python-list