numpy

Adding a field to a structured numpy array

What is the cleanest way to add a field to a structured numpy array? Can it be done destructively, or is it necessary to create a new array and copy over the existing fields? Are the contents of each field stored contiguously in memory so that such copying can be done efficiently? ...

Using numpy to build an array of all combinations of two arrays

Hi, I'm trying to run over the parameters space of a 6 parameter function to study it's numerical behavior before trying to do anything complex with it so I'm searching for a efficient way to do this. My function takes float values given a 6-dim numpy array as input. What I tried to do initially was this: 1) First I created a functio...

Is 'for x in array' always result in sorted x? [Python/NumPy]

For arrays and lists in Python and Numpy are the following lines equivalent: itemlist = [] for j in range(len(myarray)): item = myarray[j] itemlist.append(item) and: itemlist = [] for item in myarray: itemlist.append(item) I'm interested in the order of itemlist. In a few examples that I have tried they are identical, b...

How to pickle numpy's Inf objects?

When trying to pickle the object Inf as defined in numpy (I think), the dumping goes Ok but the loading fails: >>> cPickle.dump(Inf, file("c:/temp/a.pcl",'wb')) >>> cPickle.load(file("c:/temp/a.pcl",'rb')) Traceback (most recent call last): File "<pyshell#257>", line 1, in <module> cPickle.load(file("c:/temp/a.pcl",'rb')) ValueErr...

How to find the compiled extensions modules in numpy

I am compiling numpy myself on Windows. The build and install runs fine; but how do I list the currently enabled modules .. and modules that are not made available (due to maybe compilation failure or missing libraries)? ...

Can't import Numpy in Python

I'm trying to write some code that uses Numpy. However, I can't import it: Python 2.6.2 (r262, May 15 2009, 10:22:27) [GCC 3.4.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named numpy ...

Numpy: Is there an array size limit?

I'm learning to use Numpy and I wanted to see the speed difference in the summation of a list of numbers so I made this code: np_array = numpy.arange(1000000) start = time.time() sum_ = np_array.sum() print time.time() - start, sum_ >>> 0.0 1783293664 python_list = range(1000000) start = time.time() sum_ = sum(python_list) print time....

Multidimensional list(array) reassignment problem

Good day coders and codereses, I am writing a piece of code that goes through a pile of statistical data and returns what I ask from it. To complete its task the method reads from one multidimensional array and writes into another one. The piece of code giving me problems is: writer.variables[variable][:, :, :, :] = reader.variables[va...

gotchas where Numpy differs from straight python ?

Folks, is there a collection of gotchas where Numpy differs from python, points that have puzzled and cost time ? "The horror of that moment I shall never never forget !" "You will, though," the Queen said, "if you don't make a memorandum of it." For example, NaNs are always trouble, anywhere. If you can explain this without...

How to create probability density function graph using csv dictreader, matplotlib and numpy?

I'm trying to create a simple probability density function(pdf) graph using data from one column of a csv file using csv dictreader, matplotlib and numpy... Is there an easy way to use CSV DictReader combined with numpy arrays? Below is code that doesn't work. The error message is TypeError: len() of unsized object, which I'm guessing ...

What does matrix**2 mean in python/numpy?

Hi, I have a python ndarray temp in some code I'm reading that suffers this: x = temp**2 Is this the dot square (ie, equivalent to m.*m) or the matrix square (ie m must be a square matrix)? In particular, I'd like to know whether I can get rid of the transpose in this code: temp = num.transpose(whatever) num.sum(temp**2,axis=1)) ...

How do you deal with missing data using numpy/scipy?

One of the things I deal with most in data cleaning is missing values. R deals with this well using its "NA" missing data label. In python, it appears that I'll have to deal with masked arrays which seem to be a major pain to set up and don't seem to be well documented. Any suggestions on making this process easier in Python? This is bec...

How can I create a numpy array holding values of a multi-variable function?

I want to create an array holding a function f(x,y,z). If it were a function of one variable I'd do, for instance: sinx = numpy.sin(numpy.linspace(-5,5,100)) to get sin(x) for x in [-5,5] How can I do the same to get, for instance sin(x+y+z)? ...

The Assignment Problem, a numpy function?

Since an assignment problem can be posed in the form of a single matrix, I am wandering if numpy has a function to solve such a matrix. So far I have found none. Maybe one of you guys know if numpy/scipy has an assignment-problem-solve function? Edit: In the meanwhile I have found a python (not numpy/scipy) implementation at http://www....

calculate euclidean distance with numpy

I have two points in 3D: (xa,ya,za) (xb,yb,zb) And I want to calculate the distance: dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2) What's the best way to do this with Numpy, or with Python in general? I have: a = numpy.array((xa,ya,za)) b = numpy.array((xb,yb,zb)) ...

numpy array slice using None

This had me scratching my head for a while. I was unintentionally slicing an array with None and getting something other than an error (I expected an error). Instead, it returns an array with an extra dimension. >>> import numpy >>> a = numpy.arange(4).reshape(2,2) >>> a array([[0, 1], [2, 3]]) >>> a[None] array([[[0, 1], ...

How can I generate a complete histogram with numpy?

I have a very long list in a numpy.array. I want to generate a histogram for it. However, Numpy's built in histogram requires a pre-defined number of bins. What's the best way to generate a full histogram with one bin for each value? ...

Compile Matplotlib for Python on Snow Leopard

I've killed half a day trying to compile matplotlib for python on Snow Leopard. I've used the googles and found this helpful page (http://blog.hyperjeff.net/?p=160) but I still can't get it to compile. I see comments from other users on that page, so I know I'm not alone. I already installed zlib, libpng and freetype independently. I...

Python 2.5 and 2.6 and Numpy compatibility problem

In the computers of our laboratory, which have Python 2.6.2 installed in them, my program, which is an animation of the 2D random walk and diffusion, works perfectly. However, I can't get the exact same program to work on my laptop, which has Python 2.5. By that not working, I mean the animation is screwed; the axis always changes ever...

Using lambda for a constraint function

import numpy from numpy import asarray Initial = numpy.asarray [2.0, 4.0, 5.0, 3.0, 5.0, 6.0] # Initial values to start with bounds = [(1, 5000), (1, 6000), (2, 100000), (1, 50000), (1.0, 5000), (2, 1000000)] # actual passed bounds b1 = lambda x: numpy.asarray([1.4*x[0] - x[0]]) b2 = lambda x: numpy.asarray([1.4*x[1] - x[1...