scipy

Numpy: convert index in one dimension into many dimensions

Many array methods return a single index despite the fact that the array is multidimensional. For example: a = rand(2,3) z = a.argmax() For two dimensions, it is easy to find the matrix indices of the maximum element: a[z/3, z%3] But for more dimensions, it can become annoying. Does Numpy/Scipy have a simple way of returning the in...

Integrate stiff ODEs with Python

Hi all, I'm looking for a good library that will integrate stiff ODEs in Python. The issue is, scipy's odeint gives me good solutions sometimes, but the slightest change in the initial conditions causes it to fall down and give up. The same problem is solved quite happily by MATLAB's stiff solvers (ode15s and ode23s), but I can't use it...

Pythonic way to create a numpy array from a list of numpy arrays

Hello, I generate a list of one dimensional numpy arrays in a loop and later convert this list to a 2d numpy array. I would've preallocated a 2d numpy array if i knew the number of items ahead of time, but I don't, therefore I put everything in a list. The mock up is below: >>> list_of_arrays = map(lambda x: x*ones(2), range(5)) >>> l...

saving python variable to .mat file with scipy.io.savemat

Hi all, Here is my python code. >>import numpy as np >>import scipy.io >>exon = [ np.array([[1, 2], [3, 4], [5, 6]]), np.array([[7, 8], [9, 10]]) ] >>obj_arr = np.zeros((2,), dtype=np.object) >>obj_arr[0] = exon[0] >>obj_arr[1] = exon[1] >>scipy.io.savemat('/tmp/out.mat', mdict={'exon': obj_arr}, format='5') But I am getting an err...

assigning points to bins

What is a good way to bin numerical values into a certain range? For example, suppose I have a list of values and I want to bin them into N bins by their range. Right now, I do something like this: from scipy import * num_bins = 3 # number of bins to use values = # some array of integers... min_val = min(values) - 1 max_val = max(value...

Mac 10.6 Universal Binary scipy: cephes/specfun "_aswfa_" symbol not found

Hi folks, I can't get scipy to function in 32 bit mode when compiled as a i386/x86_64 universal binary, and executed on my 64 bit 10.6.2 MacPro1,1. My python setup With the help of this answer, I built a 32/64 bit intel universal binary of python 2.6.4 with the intention of using the arch command to select between the architectures. ...

Finding the length of a cubic B-spline

Using scipy's interpolate.splprep function get a parametric spline on parameter u, but the domain of u is not the line integral of the spline, it is a piecewise linear connection of the input coordinates. I've tried integrate.splint, but that just gives the individual integrals over u. Obviously, I can numerically integrate a bunch of ...

Improving Numpy Performance

I'd like to improve the performance of convolution using python, and was hoping for some insight on how to best go about improving performance. I am currently using scipy to perform the convolution, using code somewhat like the snippet below: import numpy import scipy import scipy.signal import timeit a=numpy.array ( [ range(1000000)...

Installing scipy with pip

It is possible to install numpy with pip using pip install numpy. Is there a similar possibility with scipy? (Doing pip install scipy does not work) Update The package scipy is now available to be installed with pip, so the question is not relevant anymore. ...

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)) ...

Interpolating 2d data that is piecewise constant on faces

I have an irregular mesh which is described by two variables - a faces array that stores the indices of the vertices that constitute each face, and a verts array that stores the coordinates of each vertex. I also have a function that is assumed to be piecewise constant over each face, and it is stored in the form of an array of values pe...

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'):...

How to calculate the statistics "t-test" with numpy

I'm looking to generate some statistics about a model I created in python. I'd like to generate the t-test on it, but was wondering if there was an easy way to do this with numpy/scipy. Are there any good explanations around? For example, I have three related datasets that look like this: [55.0, 55.0, 47.0, 47.0, 55.0, 55.0, 55.0, 6...

MATLAB to Python Code conversion (NumPy, SciPy, MatplotLib?)

I'm trying to convert the following code to Python from MATLAB for an EEG Project (partly because Python's slightly cheaper!) Hopefully someone can point me in the right direction: I've started to alter it but got bogged down: Particularly trying to find equivalent functions. Tried scipy.org (NumPy_for_Matlab_Users etc.) but I'm not s...

unladen-swallow with numpy/scipy

has anyone used unladen-swallow with numpy/scipy for numeric/scientific applications? Is it significantly faster in your experience? Any opinions would be great. ...

In SciPy, using ix_() with sparse matrices doesn't seem to work so what else can I use?

In Numpy, ix_() is used to grab rows and columns of a matrix, but it doesn't seem to work with sparse matrices. For instance, this code works because it uses a dense matrix: >>> import numpy as np >>> x = np.mat([[1,0,3],[0,4,5],[7,8,0]]) >>> print x [[1 0 3] [0 4 5] [7 8 0]] >>> print x[np.ix_([0,2],[0,2])] [[1 3] [7 0]] I used ix...

Importing sound files into Python as NumPy arrays (alternatives to audiolab)

I've been using Audiolab to import sound files in the past, and it worked quite well. However: It doesn't support some formats, like mp3, because the underlying libsndfile refuses to support them It doesn't work in Python 2.6 under Windows, and the author is not around to fix it - In [2]: from scikits import audiolab --------------...

2D integrals in scipy

I am trying to integrate a multivariable function in scipy over a 2d area. What would be the equivalent of the following Mathematica code? In[1]:= F[x_, y_] := Cos[x] + Cos[y] In[2]:= Integrate[F[x, y], {x, -\[Pi], \[Pi]}, {y, -\[Pi], \[Pi]}] Out[2]= 0 Looking at the scipy documentation I could only find support for one dimensional...

How can I remove a column from a sparse matrix efficiently?

The title pretty much says it all: If I am using the sparse.lil_matrix format, how can I remove a column from the matrix easily and efficiently? Thanks! ...

Convert list in tuple to numpy array?

I have tuple of lists. One of these lists is a list of scores. I want to convert the list of scores to a numpy array to take advantage of the pre-built stats that scipy provides. In this case the tuple is called 'data' In [12]: type data[2] -------> type(data[2]) Out[12]: <type 'list'> In [13]: type data[2][1] -------> type(data[2][1]...