473,388 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

numpy performance and list comprehension

TG
Hi there.

Reading the page on python performance ( http://scipy.org/PerformancePython
) made me realize that I can achieve tremendous code acceleration with
numpy just by using "u[:,:]" kind of syntax the clever way.

Here is a little problem (Oja's rule of synaptic plasticity)

* W is a matrix containing the weights of connections between elements
i
and j
* V is an array containing the values of elements

I want to make W evolve with this rule :

dW[i,j] / dt = alpha * (V[i] * V[j] - W[i,j] * V[i]^2)

(don't pay attention to the derivate and stuff)

So, how would you write it in this nifty clever way ?

As a begining I wrote this :

W += V.flatten().reshape((V.size,1)) *
V.flatten().reshape((1,V.size))

But it is not complete and, I guess, not efficient.

Apr 3 '07 #1
2 3816
On Apr 3, 5:42 pm, "TG" <gir...@gmail.comwrote:
Hi there.

Reading the page on python performance (http://scipy.org/PerformancePython
) made me realize that I can achieve tremendous code acceleration with
numpy just by using "u[:,:]" kind of syntax the clever way.

Here is a little problem (Oja's rule of synaptic plasticity)

* W is a matrix containing the weights of connections between elements
i
and j
* V is an array containing the values of elements

I want to make W evolve with this rule :

dW[i,j] / dt = alpha * (V[i] * V[j] - W[i,j] * V[i]^2)

(don't pay attention to the derivate and stuff)

So, how would you write it in this nifty clever way ?

As a begining I wrote this :

W += V.flatten().reshape((V.size,1)) *
V.flatten().reshape((1,V.size))

But it is not complete and, I guess, not efficient.
alpha * (V[i] * V[j] - W[i,j] * V[i]^2) =
alpha * V[i] * (V[j] - W[i,j] * V[i])

Assuming that V is a column vector, you could do it like this:

V = array([[5],[3],[7]])
W = array([[1,5,3],[2,2,7],[3,9,8]])
W += alpha * V * (transpose(V) - W * V)

Apr 3 '07 #2
TG wrote:
Hi there.

Reading the page on python performance ( http://scipy.org/PerformancePython
) made me realize that I can achieve tremendous code acceleration with
numpy just by using "u[:,:]" kind of syntax the clever way.

Here is a little problem (Oja's rule of synaptic plasticity)

* W is a matrix containing the weights of connections between elements
i
and j
* V is an array containing the values of elements

I want to make W evolve with this rule :

dW[i,j] / dt = alpha * (V[i] * V[j] - W[i,j] * V[i]^2)

(don't pay attention to the derivate and stuff)

So, how would you write it in this nifty clever way ?
irstas is correct. I'm just going to show off another feature of numpy,
numpy.newaxis.

import numpy as np

V = np.array([1, 2, 3])
VT = V[:, np.newaxis] # VT.shape == (3, 1)

W = np.array([[1,2,3], [4,5,6], [7,8,9]])

dWdt = alpha * VT*(V - W*VT)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Machin | last post by:
Please consider the timings below, where a generator expression starts out slower than the equivalent list comprehension, and gets worse: >python -m timeit -s "orig=range(100000)"...
20
by: mclaugb | last post by:
Has anyone recompiled the Scientific Computing package using NumPy instead of Numeric? I need a least squares algorithm and a Newton Rhaphson algorithm which is contained in Numeric but all the...
13
by: Gary Wessle | last post by:
Hi I am trying to install NumPy in my debian/testing linux 2.6.15-1-686. with no numpy for debian/testing, I am left alone, since the experimental version available by debian will result in a...
1
by: spr | last post by:
Hi, I'm trying to learn Python and I'd appreciate any comments about my small snippets of code. I read an old anecdote about performance here: http://www.python.org/doc/essays/list2str/ ...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
5
by: auditory | last post by:
I am a newbie here I am trying to read "space separated floating point data" from file I read about csv module by searching this group, but I couldn't read space separated values with csv....
0
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
5
by: Marc Oldenhof | last post by:
Hello all, I'm pretty new to Python, but use it a lot lately. I'm getting a crazy error trying to do operations on a string list after importing numpy. Minimal example: Python 2.5.1...
6
by: John [H2O] | last post by:
I have a script: from numpy import float OutD= v= OutD.append(]) On linux: Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.