[SciPy-user] How to start with SciPy and NumPy

David Baddeley david_baddeley@yahoo.com...
Sat Jan 24 14:08:41 CST 2009

Hi Vincent,

if you're new to both python and numerical programming I'd suggest you make yourself familiar with basic python first and then move on to the numerical stuff - it'll probably be easier that way. To answer your question, there are two main ways in which Numpy and Scipy help with numeric programming. The first (and simplest) of these is by providing lots of pre-rolled algorithms to do useful things (e.g. computing bessel functions, fourier transforms, and much more). The second, and arguably more important (at least when it comes to performance) is to facilitate vectorisation, which is best illustrated with an example. Say you wanted to compute the sin of a range of numbers between 0 and 2pi, with a spacing of .1 (i.e. 0, 0.1, 0.2 ..... 2pi). The standard python code (could be simplified somwhat by using list comprehensions, but if you're new to python that'd probably be more than a little confusing) would be as follows:

#define some x values
x = [ ]
for i in range(2*pi/0.1):

#calculate the corresponding y values 
y = [ ]
for x_ in x:

The equivalent code using numpy/scipy would be:

x = numpy.arange(0, 2*pi, 0.1)
y = numpy.sin(x)

This is much closer to the underlying maths, making it quicker to program and more readable, and also much faster. The reason for the speed increase is that python is an interpreted language and the for loops above are slow. Numpy effectively executes these under the hood in compiled c code which is much faster. An equally important factor is cost of allocating and navigating the python lists used for storage in the python example - as each data point is processed new memory needs to be allocated which is highly unlikely to be contiguous with the original.

This probably doesn't fully answer your question, but should give you a starting point do do a little googling / more reading in the documentation.  There's got to be some explanation of the benefits of vectorisation already our there - anyone got an idea where you'd find it?


----- Original Message ----

Message: 2
Date: Sat, 24 Jan 2009 18:31:26 +0100
From: Vicent <vginer@gmail.com>
Subject: [SciPy-user] How to start with SciPy and NumPy
To: scipy-user@scipy.org
Content-Type: text/plain; charset="iso-8859-1"

I am a Python beginner.

At this moment, I am starting to develop and numerical algorithm, which is
supposed to do lot of calculations, but it doesn't use matrices at all, for

So, at this moment, as I don't know much about NumPy and SciPy, and although
it is said that they are very useful and enhancers for developing scientific
Python applications, I don't see the point to use them...

For example, I don't know if there is a great advantage in using NumPy data
types (like bool_, int_ and so on) instead of the associated Python data
types (bool, int, etc.).

When I read NumPy and SciPy documentation, I found lots of new features that
I may use, but I haven't been able to find any quick explanation about how
those modules improve Python performance (something like "Instead of doing
*this* with standard Python, do *this* using NumPy and SciPy because it's

For example, what is the difference between "random" from random module and
"random" from numpy.random? Or are they the same?

Before I thought that working with NumPy+SciPy would be mandatory for me,
and so that I should have to adapt my code to all its special features, from
the beggining. But, at this moment, my strategy would be working with
"plain" Python, and when necessary, look for features I need in NumPy and
SciPy. Is it OK?

Can you give a light to me?

Sorry if I seem too rude or hard, I didn't mean to. I am just a bit lost...

Thank you in advance, and sorry for my English mistakes.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20090124/6a264fef/attachment-0001.html 


SciPy-user mailing list

End of SciPy-user Digest, Vol 65, Issue 54

      Get the world&#39;s best email - http://nz.mail.yahoo.com/

More information about the SciPy-user mailing list