numpy

Python: Fastest way to iterate this through a large file

Right, I'm iterating through a large binary file I need to minimise the time of this loop: def NB2(self, ID_LEN): r1=np.fromfile(ReadFile.fid,dTypes.NB_HDR,1) num_receivers=r1[0][0] num_channels=r1[0][1] num_samples=r1[0][5] blockReturn = np.zeros((num_samples,num_receivers,num_channels)) for rec in range(0,nu...

.Net Array or IList<T> from NumPy array in IronPython?

Imagine I have a .Net application that supports user extensions in the form of Python modules by embedding IronPython. Using Ironclad, I can allow users to make use of the NumPy and SciPy packages from within their modules. How good is the interop provided by Ironclad? My question is: can I use a NumPy array of type T provided by the use...

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

Return common element indices between two numpy arrays

Hi all, I have two arrays, a1 and a2. Assume len(a2) >> len(a1), and that a1 is a subset of a2. I would like a quick way to return the a2 indices of all elements in a1. The time-intensive way to do this is obviously: from operator import indexOf indices = [] for i in a1: indices.append(indexOf(a2,i)) This of course takes a long...

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

Numpy array memory issue

I believe I am having a memory issue using numpy arrays. The following code is being run for hours on end: new_data = npy.array([new_x, new_y1, new_y2, new_y3]) private.data = npy.row_stack([private.data, new_data]) where new_x, new_y1, new_y2, new_y3 are floats. After about 5 hours of recording this data every second (more t...

Accessing Lower Triangle of a Numpy Matrix?

Okay, so basically lets say i have a matrix: matrix([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]) Is it possible to get the area below the diagonal easily when working with numpy matrixs? I looked around and could not find anything. I can do the standard, for loo...

Custom data types in numpy arrays

I'm creating a numpy array which is to be filled with objects of a particular class I've made. I'd like to initialize the array such that it will only ever contain objects of that class. For example, here's what I'd like to do, and what happens if I do it. class Kernel: pass >>> L = np.empty(4,dtype=Kernel) TypeError: data type n...

Building an interleaved buffer for pyopengl and numpy

I'm trying to batch up a bunch of vertices and texture coords in an interleaved array before sending it to pyOpengl's glInterleavedArrays/glDrawArrays. The only problem is that I'm unable to find a suitably fast enough way to append data into a numpy array. Is there a better way to do this? I would have thought it would be quicker t...

numpy.matrix manipulations

Hi everyone, I have a question about numpy.matrix class. How can I perform such basic manipulations with matrices as adding, deleting and replacing rows and columns? p.s. I apologize for the lame question.. ...

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

numpy: extending arrays along a new axis?

Surely there must be a way to do this... I can't work it out. I have a (9,4) array, and I want to repeat it along a 3rd axis 4096 times... So it becomes simply (9,4,4096), with each value from the 9,4 array simply repeated 4096 times down the new axis. If my dubious 3D diagram makes sense (the diagonal is a z-axis) 4| /off to 4096 3...

Can't import numpy into embedded ironpython engine.

From an ipy console I can import ironclad import numpy with no problem. However when I try the same imports with an embedded interpreter the numpy import fails. My Python engines is set up like this: public PyEngine() { //Frames option needed to use sys.__getframe //used in Numpy and others. Dictiona...

Solving Sparse Linear Problem With Some Known Boundary Values

I'm trying to solve a Poisson equation on a rectangular domain which ends up being a linear problem like Ax=b but since I know the boundary conditions, there are nodes where I have the solution values. I guess my question is... How can I solve the sparse system Ax=b if I know what some of the coordinates of x are and the undetermin...

Matplotlib runs out of memory when plotting in a loop - EDITED

I have a fairly simple plotting routine that looks like this: from __future__ import division import datetime import matplotlib matplotlib.use('Agg') from matplotlib.pyplot import figure, plot, show, legend, close, savefig, rcParams import numpy from globalconstants import * def plotColumns(columnNumbers, t, out, showFig=False, fil...

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

How do I calculate percentiles with python/numpy?

Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy array? I am looking for something similar to Excel's percentile function. I looked in NumPy's statistics reference, and couldn't find this. All I could find is the median (50th percentile), but not something more specific. ...

Make distutils look for numpy header files in the correct place

In my installation, numpy's arrayobject.h is located at …/site-packages/numpy/core/include/numpy/arrayobject.h. I wrote a trivial Cython script that uses numpy: cimport numpy as np def say_hello_to(name): print("Hello %s!" % name) I also have the following distutils setup.py (copied from the Cython user guide): from distutils.c...