Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64256

Re: Need help vectorizing code

Newsgroups comp.lang.python
Date 2014-01-18 13:18 -0800
References <140f8aea-d3c5-4c0d-94f5-6aa064e353d1@googlegroups.com>
Message-ID <6bd7e22e-cfc8-4a80-9f17-3cc3ff8bad67@googlegroups.com> (permalink)
Subject Re: Need help vectorizing code
From Kevin K <richyokevin@gmail.com>

Show all headers | View raw


I didn't paste the whole function, note the ... before and after. I do use the values.

I want to get rid of one of the loops so that the computation becomes O(D). Assume vectors a and c should get populated during the compute, each being 1xD.

Thanks


On Saturday, January 18, 2014 12:51:25 PM UTC-8, Kevin K 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]))
> 
> 
> 
> ...
> 
> 
> 
> If I call numpy.vectorize() on the function, it throws an error at runtime.
> 
> 
> 
> Thanks

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Need help vectorizing code Kevin K <richyokevin@gmail.com> - 2014-01-18 12:51 -0800
  Re: Need help vectorizing code Joshua Landau <joshua@landau.ws> - 2014-01-18 21:04 +0000
  Re: Need help vectorizing code Kevin K <richyokevin@gmail.com> - 2014-01-18 13:18 -0800
  Re: Need help vectorizing code Peter Otten <__peter__@web.de> - 2014-01-18 22:50 +0100
  Re: Need help vectorizing code Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-01-19 15:46 +0000

csiph-web