[SciPy-user] [newb] how to create arrays

Anne Archibald peridot.faceted@gmail....
Thu Jan 3 01:56:05 CST 2008

On 02/01/2008, Neal Becker <ndbecker2@gmail.com> wrote:
> How would I create a vector of complex random variables?
> I'm thinking the best way is to create a complex vector, then
> assign to the real and imag parts (using e.g., random.standard_normal).
> I don't see any way to create an uninitialized array.  I guess I'd have to
> use zeros?  Is there any way to avoid the wasted time of initializing just
> to write over it?

You can create an uninitialized array if you want to. But, from your
question, you may well be thinking about your problem in the wrong
way. If all you want to do is store a whole bunch of values you
compute in a loop, use a python list. Python lists are really quite
efficient and convenient. The point of scipy is that it lets you
operate on the whole vector at once:

a = numpy.linspace(0,numpy.pi,1000)
b = numpy.sin(a)
print numpy.average(b)

The main reason for this is conceptual clarity: you can start thinking
of array operations as single steps in your program, allowing you to
do more with the same size of program. Secondarily, using the numpy
functions allows the looping to be done in compiled code, which is
much faster than python code. If you are filling an array, element by
element, with a loop in python, the time the python code takes to run
will be much longer than the time spend initializing with zeros. Thus
numpy.empty() gets much less use than you might expect. Look into
using numpy functions - linspace, arange, zeros, ones,
exp/sin/arctan/etc. to create your array.

Good luck,

More information about the SciPy-user mailing list