numpy

Efficient way to convert numpy record array to a list of dictionary

How do I convert the numpy record array below: recs = [('Bill', 31, 260.0), ('Fred', 15, 145.0)] r = rec.fromrecords(recs, names='name, age, weight', formats='S30, i2, f4') to a list of dictionary like: [{'name': 'Bill', 'age': 31, 'weight': 260.0}, 'name': 'Fred', 'age': 15, 'weight': 145.0}] ...

Convert dict to array in NumPy

I'd like to take a dictionary of a dictionary containing floats, indexed by ints and convert it into a numpy.array for use with the numpy library. Currently I'm manually converting the values into two arrays, one for the original indexes and the other for the values. While I've looked at numpy.asarray my conclusion has been that I must...

Numpy with python 3.0

NumPy installer can't find python path in the registry. Cannot install Python version 2.6 required, which was not found in the registry. Is there a numpy build which can be used with python 3.0? ...

Left inverse in numpy or scipy?

I am trying to obtain the left inverse of a non-square matrix in python using either numpy or scipy. How can I translate the following Matlab code to Python? >> A = [0,1; 0,1; 1,0] A = 0 1 0 1 1 0 >> y = [2;2;1] y = 2 2 1 >> A\y ans = 1.0000 2.0000 Is there a numpy or scipy eq...

Reading a binary file in Python: takes a very long time to read certain bytes.

This is very odd I'm reading some (admittedly very large: ~2GB each) binary files using numpy libraries in Python. I'm using the: thingy = np.fromfile(fileObject, np.int16, 1) method. This is right in the middle of a nested loop - I'm doing this loop 4096 times per 'channel', and this 'channel' loop 9 times for every 'receiver', an...

How to use numpy with cygwin

Hi I have a bash shell script which calls some python scripts. I am running windows with cygwin which has python in /usr/bin/python. I also have python and numpy installed as a windows package. When I execute the script from cygwin , I get an ImportError - no module named numpy. I have tried running from windows shell but the bash scri...

Python: select function

With this code: import scipy from scipy import * x = r_[1:15] print x a = select([x > 7, x >= 4],[x,x+10]) print a I get this answer: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14] [ 0 0 0 14 15 16 17 8 9 10 11 12 13 14] But why do I have zeros in the beginning and not in the end? Thanks in advance. ...

How to graphically edit the graph of a mathematical function (with python)?

Is there already a python package allowing to graphically edit the graph of a function? ...

How to get data in a histogram bin.

I want to get a list of the data contained in a histogram bin. I am using numpy, and Matplotlib. I know how to traverse the data and check the bin edges. However, I want to do this for a 2D histogram and the code to do this is rather ugly. Does numpy have any constructs to make this easier? For the 1D case, I can use searchsorted()....

Sum of Square Differences (SSD) in numpy/scipy

I'm trying to use Python and Numpy/Scipy to implement an image processing algorithm. The profiler tells me a lot of time is being spent in the following function (called often), which tells me the sum of square differences between two images def ssd(A,B): s = 0 for i in range(3): s += sum(pow(A[:,:,i] - B[:,:,i],2)) ...

Wrong results with Python multiply() and prod()

Hi, Can anyone explain the following? I'm using Python 2.5 Consider 1*3*5*7*9*11 ... *49. If you type all that from within IPython(x,y) interactive console, you'll get 58435841445947272053455474390625L, which is correct. (why odd numbers: just the way I did it originally) Python multiply.reduce() or prod() should yield the same resul...

Beginner extending C with Python (specifically Numpy)

I am working on a real time audio processing dynamically linked library where I have a 2 dimensional C array of floating point data which represents the audio buffer. One dimension is time (samples) and the other is channel. I would like to pass this to a python script as a numpy array for the DSP processing and then I would like to pass...

Fitting a line in 3D

Are there any algorithms that will return the equation of a straight line from a set of 3D data points? I can find plenty of sources which will give the equation of a line from 2D data sets, but none in 3D. Thanks. ...

Would you use numpy if you were just manipulating a list of binary binary values?

Is there any advantage to using numpy when you're doing a large number of operations on lists of binary values? How about integers within a small range (like just the numbers 1,2, and 3?) ...

Potential use of Python decorator or other refactorization: iterative optimization

Forgive me for yet another question on Python decorators. I did read through many of them, but I wonder what the best solution to the specific following problem is. I have written several functions that do some form of gradient descent in numpy/scipy. Given a matrix X, I try to iteratively minimize some distance, d(X, AS), as functions ...

Numpy masked array modification

Hi, Currently I have a code that checks if given element in array is equal = 0 and if so then set the value to 'level' value (temp_board is 2D numpy array, indices_to_watch contains 2D coordinates that should be watched for zeros). indices_to_watch = [(0,1), (1,2)] for index in indices_to_watch: if temp_board[index] == 0...

speed-up python function to process files with data segments separated by a blank space

I need to process files with data segments separated by a blank space, for example: 93.18 15.21 36.69 33.85 16.41 16.81 29.17 21.69 23.71 26.38 63.70 66.69 0.89 39.91 86.55 56.34 57.80 98.38 0.24 17.19 75.46 [...] 1.30 73.02 56.79 39.28 96.39 18.77 55.03 99.95 28.88 90.90 26.70 62.37 86.58 65.05 25.16 32.61 17.47 4.23 34.82 26.63 5...

Slicing at runtime

Hello, can someone explain me how to slice a numpy.array at runtime? I don't know the rank (number of dimensions) at 'coding time'. A minimal example: import numpy as np a = np.arange(16).reshape(4,4) # 2D matrix targetsize = [2,3] # desired shape b_correct = dynSlicing(a, targetsize) b_wrong = np.resize(a, targetsize) print a [[ 0 ...

How to improve performance when interpolating on 3d data with SciPy

Hello there, I have 3d-data representing the atmosphere. Now I want to interpolate this data to a common Z coordinate (what I mean by that should be clear from the function's doctring). The following code works fine, but I was wondering if there were a way to improve the performance ... def interpLevel(grid,value,data,interp='linear'):...

Simple question: In numpy how do you make a multidimensional array of arrays?

Right, perhaps I should be using the normal Python lists for this, but here goes: I want a 9 by 4 multidimensional array/matrix (whatever really) that I want to store arrays in. These arrays will be 1-dimensional and of length 4096. So, I want to be able to go something like column = 0 #column to ins...