numpy

Matlab equivalent of Numpy broadcasting?

Hi, I'm trying to find some way to substract a size 3 vector from each column of a 3*(a big number) matrix in Matlab. Of course I could use a loop, but I'm trying to find some more efficient solution, a bit like numpy broadcasting. Oh, and I can't use repmat because I just don't have enough memory to use it (as it creates yet another 3*...

numpy arrays type conversion in C

Hi, I would like to convert the numpy double array to numpy float array in C(Swig). I am trying to use PyObject *object = PyArray_FROM_OT(input,NPY_FLOAT) or PyObject *object = PyArray_FROMANY(input,NPY_FLOAT,0,0,NPY_DEFAULT) or PyObject *object = PyArray_FromObject(input,NPY_FLOAT,0,0) or PyObject *object = PyArray_Contiguou...

Using PIL and NumPy to convert an image to Lab array, modify the values and then convert back

I am trying to convert a PIL image into an array using NumPy. I then want to convert that array into Lab values, modify the values and then convert the array back in to an image and save the image. I have the following code: import Image, color, numpy # Open the image file src = Image.open("face-him.jpg") # Attempt to ensure image is ...

Numpy minimum in (row, column) format

How can I know the (row, column) index of the minimum of a numpy array/matrix? For example, if A = array([[1, 2], [3, 0]]), I want to get (1, 1) Thanks! ...

numpy join entries intersecting at a cell

In numpy, how can I join the entries that intersects at a cell? For example: In the example, I want to join rows/columns B and F into one row/column BF, where each element is the average of the ones with the same color. ...

Interpolation over an irregular grid

So, I have three numpy arrays which store latitude, longitude, and some property value on a grid -- that is, I have LAT(y,x), LON(y,x), and, say temperature T(y,x), for some limits of x and y. The grid isn't necessarily regular -- in fact, it's tripolar. I then want to interpolate these property (temperature) values onto a bunch of dif...

How to elementwise-multiply a scipy.sparse matrix by a broadcasted dense 1d array?

Suppose I have a 2d sparse array. In my real usecase both the number of rows and columns are much bigger (say 20000 and 50000) hence it cannot fit in memory when a dense representation is used: >>> import numpy as np >>> import scipy.sparse as ssp >>> a = ssp.lil_matrix((5, 3)) >>> a[1, 2] = -1 >>> a[4, 1] = 2 >>> a.todense() matrix([[...

How find values in an array that meet two conditions using Python

I have an array a=[1,2,3,4,5,6,7,8,9] and I want to find the indices of the element s that meet two conditions i.e. a>3 and a<8 ans=[3,4,5,6] a[ans]=[4,5,6,7] I can use numpy.nonzero(a>3) or numpy.nonzero(a<8) but not numpy.nonzero(a>3 and a<8) which gives the error: ValueError: The truth value of an array with more than one eleme...

Numpy and line intersections

How would I use numpy to calculate the intersection between two line segments? In the code I have segment1 = ((x1,y1),(x2,y2)) and segment2 = ((x1,y1),(x2,y2)). Note segment 1 does not equal segment2. So in my code I've also been calculating the slope and y-intercept, it would be nice if that could be avoided but I don't know of a way...

Linear interpolation on a numpy array

I have the following numpy array: # A B C Y my_arr = np.array([ [.20, .54, .26], # <0 [.22, .54, .24], # 1 [.19, .56, .25], # 2 [.19, .58, .23], # 3 [.17, .62, .21] ]) # 4+ if a user enters a y (example,...

Numpy interconversion between multidimensional and linear indexing

I'm looking for a fast way to interconvert between linear and multidimensional indexing in Numpy. To make my usage concrete, I have a large collection of N particles, each assigned 5 float values (dimensions) giving an Nx5 array. I then bin each dimension using numpy.digitize with an appropriate choice of bin boundaries to assign each...

Finding missing values in a numpy array

Alright, extreme rookie question here. In my program, I generate a 2D numpy array, some of whom's entries are missing (not the "nan" kind of nonexistant, but the "None" kind, or NoneType). I'd like to put a mask over these entries, but I seem to be having some trouble doing so. Ordinarily, to mask over, say, all entries with value 2, ...

Numpy object array of numerical arrays

I want to create an array with dtype=np.object, where each element is an array with a numerical type, e.g int or float. For example: >>> a = np.array([1,2,3]) >>> b = np.empty(3,dtype=np.object) >>> b[0] = a >>> b[1] = a >>> b[2] = a Creates what I want: >>> print b.dtype object >>> print b.shape (3,) >>> print b[0].dtype int64 b...

Numpy records in the c api

I'm playing with writing some C code to speed up an inner loop in my python code. This loop operates on a numpy record, e.g. soemthing like this: a = numpy.zeros((10,), dtype=[("myfvalue" ,"float"), ("myc", "int8"), ("anotheri", "uint64")]) which is then passed into c code like so: myCFunc(a, "blah") I...

uploading data using Numpy.genfromtxt with multiple formats

I have a file with a time stamp as a column, and numbers in all the rest. I can either load one or the other correctly, but not both. Frustrating the heck out of me... This is what I am doing: import numpy as np file = np.genfromtxt('myfile.dat', skip_header = 1, usecols = (0,1,2,3), dtype = (str, float), delimiter = '\t') So colu...

Splicing NumPy arrays

I am having a problem splicing together two arrays. Let's assume I have two arrays: a = array([1,2,3]) b = array([4,5,6]) When I do vstack((a,b)) I get [[1,2,3],[4,5,6]] and if I do hstack((a,b)) I get: [1,2,3,4,5,6] But what I really want is: [[1,4],[2,5],[3,6]] How do I accomplish this without using for loops (it needs to b...

Finding blank regions in image

This question is somewhat language-agnostic, but my tool of choice happens to be a numpy array. What I am doing is taking the difference of two images via PIL: img = ImageChops.difference(img1, img2) And I want to find the rectangular regions that contain changes from one picture to another. Of course there's the built in .getbbox() ...

Optimal extraction of columns from numpy matrix

Say I have a numpy matrix like so: [[ x1, x2, x3, ... ], [ y1, y2, y3, ... ], [ z1, z2, z3, ... ], [ 1, 1, 1, ... ]] From which I want to extract a list of lists like so: [[x1, y1, z1], [x2, y2, z2], [x3, y3, z3], ... ] What is the most optimal way of doing this? At the moment I have: tpoints = [pt[:3].tolist() for pt in nu...

adjusting heights of individual subplots in matplotlib in Python

if I have a series of subplots with one column and many rows, i.e.: plt.subplot(4, 1, 1) # first subplot plt.subplot(4, 1, 2) # second subplot # ... how can I adjust the height of the first N subplots? For example, if I have 4 subplots, each on its own row, I want all of them to have the same width but the first 3 subplots to be short...

Numpy matrix to array

I am using numpy. I have a matrix with 1 column and N rows and I want to get an array from with N elements. For example, if i have M = matrix([[1], [2], [3], [4]]), I want to get A = array([1,2,3,4]). To achieve it, I use A = np.array(M.T)[0]. Does anyone know a more elegant way to get the same result? Thanks! ...