numpy

Array broadcasting with numpy

How do I write the following loop using Python's implicit looping? def kl(myA, myB, a, b): lots of stuff that assumes all inputs are scalars x, y = meshgrid(inclusive_arange(0.0, xsize, 0.10),\ inclusive_arange(0.0, ysize, 0.10)) for j in range(x.shape[0]): for i in range(x.shape[1]): z[j, i] = kl(x[j, ...

Processing data by reference or by value in python

Consider the following session. How are the differences explained? I thought that a += b is a syntactical sugar of (and thus equivalent to) a = a + b. Obviously I'm wrong. >>> import numpy as np >>> a = np.arange(24.).reshape(4,6) >>> print a [[ 0. 1. 2. 3. 4. 5.] [ 6. 7. 8. 9. 10. 11.] [ 12. 13. 14. 15. 16. ...

Converting (part of) a numpy recarray into a 2d array?

We've got a set of recarrays of data for individual days - the first attribute is a timestamp and the rest are values. Several of these: ts a b c 2010-08-06 08:00, 1.2, 3.4, 5.6 2010-08-06 08:05, 1.2, 3.4, 5.6 2010-08-06 08:10, 1.2, 3.4, 5.6 2010-08-06 08:15, 2.2, 3.3, 5.6 2010-08-06 08:20, 1.2, 3.4, 5.6 We'd l...

Numpy position and vector arrays - implement collision detection

I'm using point sprites in PyOpenGL with numpy and glDrawArrays. So I have two arrays, one for the points and one for the vectors. r = lambda: random.random() self.pts = numpy.zeros((2000,2), dtype=numpy.uint16) for pt in self.pts: pt[0] = 300*r() pt[1] = 200*r() self.vect...

is fftshift broken in scipy?

I use the latest version of numpy/scipy. The following script does not work: import numpy as np import matplotlib.pyplot as plt from scipy.fftpack import fft, fftshift, fftfreq hn= np.ones(10) hF = fft(hn,1024) shifted = fftshift(hF) It gives the following error message: Traceback (most recent call last): File "D:...

What is the fastest way to plot a 2d numpy array of pixel data with pygtk?

I have a numpy array of pixel data that I want to draw at interactive speeds in pygtk. Is there some simple, fast way to get my data onto the screen? ...

SimpleJSON and NumPy array

What is the most efficient way of serializing a numpy array using simplejson? ...

What is the preferred way to preallocate NumPy arrays?

I am new to NumPy/SciPy. From the documentation, it seems more efficient to preallocate a single array rather than call append/insert/concatenate. For example, to add a column of 1's to an array, i think that this: ar0 = np.linspace(10, 20, 16).reshape(4, 4) ar0[:,-1] = np.ones_like(ar0[:,0]) is preferred to this: ar0 = np.linspace...

[python] import png image as matrix?

Hi, I want to do some image processing using Python - is there a simple way to import .png image files as a matrix of greyscale/RGB values (possibly using PIL)? ...

Numpy: how to retrieve k max values from each row?

Hi, I want to retrieve k max values from each row in a numpy array. I have been digging through the documentation but couldn't find an answer (I am probably looking in the wrong places). Does anybody have a simple code snippet that does this? thanks so much, Diederik ...

Using numpy's flatten_dtype with structured dtypes that have titles

Hi, I usually don't post questions on these forums, but I've searched all over the place and I haven't found anything about this issue. I am working with structured arrays to store experimental data. I'm using titles to store information about my fields, in this case the units of measure. When I call numpy.lib.io.flatten_dtype() on my ...

find a minimum value in an array of floats

how would one go about finding the minimum value in an array of 100 floats in python? I have tried minindex=darr.argmin() and print darr[minindex] with import numpy (darr is the name of the array) but i get: minindex=darr.argmin() AttributeError: 'list' object has no attribute 'argmin' what might be the problem? is there a better alte...

Iterate across arbitrary dimension in numpy

I have a multidimensional numpy array, and I need to iterate across a given dimension. Problem is, I won't know which dimension until runtime. In other words, given an array m, I could want m[:,:,:,i] for i in xrange(n) or I could want m[:,:,i,:] for i in xrange(n) etc. I imagine that there must be a straightforward feature in num...

Why Does Looping Beat Indexing Here?

A few years ago, someone posted on Active State Recipes for comparison purposes, three python/NumPy functions; each of these accepted the same arguments and returned the same result, a distance matrix. Two of these were taken from published sources; they are both--or they appear to me to be--idiomatic numpy code. The repetitive calcula...

[Numpy] read csv into record array?

Hi, I wonder if there is a direct way to import the contents of a csv file into a record array, much in the way that R's read.table(), read.delim(), and read.csv() family imports data to R's data frame? Or is the best way to use csv.reader() and then apply something like numpy.core.records.fromrecords()? ...

Using Numpy arrays as lookup tables

I have a 2D array of Numpy data read from a .csv file. Each row represents a data point with the final column containing a a 'key' which corresponds uniquely to 'key' in another Numpy array - the 'lookup table' as it were. What is the best (most Numponic) way to match up the lines in the first table with the values in the second? ...

Obtaining an invertible square matrix from a non-square matrix of full rank in numpy or matlab

Assume you have an NxM matrix A of full rank, where M>N. If we denote the columns by C_i (with dimensions Nx1), then we can write the matrix as A = [C_1, C_2, ..., C_M] How can you obtain the first linearly independent columns of the original matrix A, so that you can construct a new NxN matrix B that is an invertible matrix with a n...

Numpy for R user?

Hi, long-time R and Python user here. I use R for my daily data analysis and Python for tasks heavier on text processing and shell-scripting. I am working with increasingly large data sets, and these files are often in binary or text files when I get them. The type of things I do normally is to apply statistical/machine learning algorith...

Numpy index slice without losing dimension information

I'm using numpy and want to index a row without losing the dimension information. import numpy as np X = np.zeros((100,10)) X.shape # >> (100, 10) xslice = X[10,:] xslice.shape # >> (10,) In this example xslice is now 1 dimension, but I want it to be (1,10). In R, I would use X[10,:,drop=F]. Is there something similar in nu...

Adding an array in numpy at a specified location

Is there a fast way in numpy to add array A to array B at a specified location? For instance, if B = [ [0, 1, 2], [2, 3, 4], [5, 6, 7] ] and A = [ [2, 2], [2, 2] ] and I want to add A to B starting from point (0, 0) to get C = [ [2, 3, 2], [4, 5, 4], [5, 6, 7], ] Of course I can do that via ext...