numpy

Open source alternative to MATLAB's fmincon function?

Is there an open-source alternative to MATLAB's fmincon function for constrained linear optimization? I'm rewriting a MATLAB program to use Python / NumPy / SciPy and this is the only function I haven't found an equivalent to. A NumPy-based solution would be ideal, but any language will do. ...

python.array versus numpy.array

If you are creating a 1d array in Python is there any benefit to using the NumPy package? ...

How do you use the ellipsis slicing syntax in Python?

This came up in Hidden features of Python, but I can't see good documentation or examples that explain how the feature works. ...

How to create a numpy record array from C

On the Python side, I can create new numpy record arrays as follows: numpy.zeros((3,), dtype=[('a', 'i4'), ('b', 'U5')]) How do I do the same from a C program? I suppose I have to call PyArray_SimpleNewFromDescr(nd, dims, descr), but how do I construct a PyArray_Descr that is appropriate for passing as the third argument to PyArray_S...

Removing a sequence of characters from a large binary file using python

I would like to trim long sequences of the same value from a binary file in python. A simple way of doing it is simply reading in the file and using re.sub to replace the unwanted sequence. This will of course not work on large binary files. Can it be done in something like numpy? ...

Adding a dimension to every element of a numpy.array

I'm trying to transform each element of a numpy array into an array itself (say, to interpret a greyscale image as a color image). In other words: >>> my_ar = numpy.array((0,5,10)) [0, 5, 10] >>> transformed = my_fun(my_ar) # In reality, my_fun() would do something more useful array([ [ 0, 0, 0], [ 5, 10, 15], [10,...

Is there a good NumPy clone for Jython?

I'm a relatively new convert to Python. I've written some code to grab/graph data from various sources to automate some weekly reports and forecasts. I've been intrigued by the Jython concept, and would like to port some Python code that I've written to Jython. In order to do this quickly, I need a NumPy clone for Jython (or Java). I...

Running numpy from cygwin

I am running a windows machine have installed Python 2.5. I also used the windows installer to install NumPy. This all works great when I run the Python (command line) tool that comes with Python. However, if I run cygwin and then run Python from within, it cannot find the numpy package. What environment variable do I need to set? W...

How do I add a guard ring to a matrix in NumPy?

Using NumPy, a matrix A has n rows and m columns, and I want add a guard ring to matrix A. That guard ring is all zero. What should I do? Use Reshape? But the element is not enough to make a n+1 m+1 matrix. Or etc.? Thanks in advance I mean an extra ring of cells that always contain 0 surround matrix A.Basically there is a Matrix B h...

How to truncate matrix using NumPy (Python)

Hi, just a quick question, if I have a matrix has n rows and m columns, how can I cut off the 4 sides of the matrix and return a new matrix? (the new matrix would have n-2 rows m-2 columns). Thanks in advance ...

How do I build a numpy array from a generator?

How can I build a numpy array out of a generator object? Let me illustrate the problem: >>> import numpy >>> def gimme(): ... for x in xrange(10): ... yield x ... >>> gimme() <generator object at 0x28a1758> >>> list(gimme()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> numpy.array(xrange(10)) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> numpy...

PIL and numpy

Alright, I'm toying around with converting a PIL image object back and forth to a numpy array so I can do some faster pixel by pixel transformations than PIL's PixelAccess object would allow. I've figured out how to place the pixel information in a useful 3D numpy array by way of: pic = Image.open("foo.jpg") pix = numpy.array(pic.getda...

How can I use Numerical Python with Python 2.6

I'm forced to upgrade to Python 2.6 and am having issues using Numerical Python (Numpy) with Python 2.6 in windows. I'm getting the following error... Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> from numpy.core.numeric import array,dot,all File "C:\svn\svn_urbansim\UrbanSimDev\Builds\working\urban...

Python: Numpy array help. Is there a function to return the index of something in an array?

I know there is a method for python list to return the first index of something l = list(1,2,3) l.index(2) >>> 1 Is there something like that for numpy arrays? Thanks for your help :) ...

How do I use a 2-d boolean array to select from a 1-d array on a per-row basis in numpy?

Let me illustrate this question with an example: import numpy matrix = numpy.identity(5, dtype=bool) #Using identity as a convenient way to create an array with the invariant that there will only be one True value per row, the solution should apply to any array with this invariant base = numpy.arange(5,30,5) #This could be any 1-d arra...

Can I get the matrix determinant by Numpy?

Hi, All, This confused me a lot. I read the manual of Numpy that there is function det(M) that can calculate the determinant. However, I can't find the det() in Numpy. By the way, I use Python 2.5. There should be no compatabile problem with Numpy. Thanks very much for your help! ...

deleting rows of a numpy array based on uniqueness of a value

let's say I have a bi-dimensional array like that numpy.array([[0,1,1.2,3],[1,5,3.2,4],[3,4,2.8,4], [2,6,2.3,5]]) I want to have an array formed eliminating whole rows based on uniqueness of values of last column, selecting the row to keep based on value of third column. e.g. in this case i would like to keep only one of the rows with ...

Is there a way to install the scipy special module without the rest of scipy?

I'm writing some Python numerical code and would like to use some functions from the special module. So far, my code only depends on numpy, which I've found very easy to install in a variety of Python environments. Installing scipy, on the other hand, has generally been an exercise in frustration. Is there a way to get just the special ...

Identifying numeric and array types in numpy

Is there an existing function in numpy that will tell me if a value is either a numeric type or a numpy array? I'm writing some data-processing code which needs to handle numbers in several different representations (by "number" I mean any representation of a numeric quantity which can be manipulated using the standard arithmetic operato...

numpy, PIL adding an image

I'm trying to add two images together using numpy and PIL. The way I would do this in matlab would be something like: >> M1 = imread('_1.jpg'); >> M2 = imread('_2.jpg'); >> resM = M1 + M2; >> imwrite(resM, 'res.jpg'); I get something like this: Using a compositing program and adding the images the matlab result seems to be right. ...