numpy

Reducing size of a character array in Numpy

Given a character array: In [21]: x = np.array(['a ','bb ','cccc ']) One can remove the whitespace using: In [22]: np.char.strip(x) Out[22]: array(['a', 'bb', 'cccc'], dtype='|S8') but is there a way to also shrink the width of the column to the minimum required size, in the above case |S4? ...

compare two following values in numpy array

What is the best way to touch two following values in an numpy array? example: npdata = np.array([13,15,20,25]) for i in range( len(npdata) ): print npdata[i] - npdata[i+1] this looks really messed up and additionally needs exception code for the last iteration of the loop. any ideas? Thanks! ...

Numpy Matrix keeps giving me an Error,

Okay this is werid, i keep getting the error, randomly. ValueError: matrix must be 2-dimensional So i tracked it down, and cornered it to basically something like this: a_list = [[(1,100) for _ in range(32)] for _ in range(32)] numpy.matrix(a_list) Whats wrong with this? If i print a_list it is clearly a 2d matrix of tuples, howeve...

Get information about a function in python, looking at source code

Hi, the following code comes from the matplotlib gallery: #!/usr/bin/env python from pylab import * x = array([10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]) y = array([8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]) I am new to python, and would like to change the content of x and y from an input file. I have two short ...

A 3-D grid of regularly spaced points

I want to create a list containing the 3-D coords of a grid of regularly spaced points, each as a 3-element tuple. I'm looking for advice on the most efficient way to do this. In C++ for instance, I simply loop over three nested loops, one for each coordinate. In Matlab, I would probably use the meshgrid function (which would do it in ...

Why won't numpy matrix let me print its rows?

Okay this is probably a really dumb question, however it's really starting to hurt. I have a numpy matrix, and basically I print it out row by row. However I want to make each row be formatted and separated properly. >>> arr = numpy.matrix([[x for x in range(5)] for y in range(5)]) >>> arr matrix([[0, 1, 2, 3, 4], [0, 1, 2, 3,...

Using numpy.apply

What's wrong with this snippet of code? import numpy as np from scipy import stats d = np.arange(10.0) cutoffs = [stats.scoreatpercentile(d, pct) for pct in range(0, 100, 20)] f = lambda x: np.sum(x > cutoffs) fv = np.vectorize(f) # why don't these two lines output the same values? [f(x) for x in d] # => [0, 1, 2, 2, 3, 3, 4, 4, 5, 5]...

polynomial surface fit numpy

I have had that problem before and I couldn't solve it how do I fit a 2d surface z=f(x,y) with a polynomial in numpy (full crossterms). Thanks in advance, Wolfgang ...

Scipy sparse... arrays?

Hey, folks. So, I'm doing some Kmeans classification using numpy arrays that are quite sparse-- lots and lots of zeroes. I figured that I'd use scipy's 'sparse' package to reduce the storage overhead, but I'm a little confused about how to create arrays, not matrices. I've gone through this tutorial on how to create sparse matrices: h...

how to handle an asymptote/discontinuity with Matplotlib

Hello all. Firstly - thanks again for all your help. Sorry not to have accepted the responses to my previous questions as I did not know how the system worked (thanks to Mark for pointing that out!). I have since been back and gratefully acknowledged the kind help I have received. My question: when plotting a graph with a discontinuity/...

compiling numpy with sunperf atlas libraries

I would like to use the sunperf libraries when compiling scipy and numpy. I tried using setupscons.py which seems to check from SUNPERF libraries, but it didnt recognize where mine are: here is a listing of /pkg/linux/SS12/sunstudio12.1 (thats where the sunperf library lives): wkerzend@mosura:/home/wkerzend>ls /pkg/linux/SS12/sunstudio1...

matlab-like variable editor in python

is there a data viewer in python/ipython like the variable editor in MATLAB? ...

Better use a tuple or numpy array for storing coordinates

Hi, I'm porting an C++ scientific application to python, and as I'm new to python, some problems come to my mind: 1) I'm defining a class that will contain the coordinates (x,y). These values will be accessed several times, but they only will be read after the class instantiation. Is it better to use an tuple or an numpy array, both in...

find nearest value in numpy array

Hi, is there a numpy-thonic way, e.g. function, to find the 'nearest value' in an array? example: np.find_nearest( array, value ) thanks in advance! ...

Numpy ‘smart’ symmetric matrix

Is there a smart and space-efficient symmetric matrix in numpy which automatically (and transparently) fills the position at [j][i] when [i][j] is written to? a = numpy.symmetric((3, 3)) a[0][1] = 1 a[1][0] == a[0][1] # True print a # [[0 1 0], [1 0 0], [0 0 0]] assert numpy.all(a == a.T) # for any symmetric matrix An automatic Hermi...

Rewriting a for loop in pure NumPy to decrease execution time

I recently asked about trying to optimise a Python loop for a scientific application, and received an excellent, smart way of recoding it within NumPy which reduced execution time by a factor of around 100 for me! However, calculation of the B value is actually nested within a few other loops, because it is evaluated at a regular grid o...

Numpy: Creating a complex array from 2 real ones?

I swear this should be so easy... Why is it not? :( In fact, I want to combine 2 parts of the same array to make a complex array: Data[:,:,:,0] , Data[:,:,:,1] These don't work: x = np.complex(Data[:,:,:,0], Data[:,:,:,1]) x = complex(Data[:,:,:,0], Data[:,:,:,1]) Am I missing something? Does numpy not like performing array functi...

Detecting periodic repetitions in the data stream

Let's say I have an array of zeros: a = numpy.zeros(1000) I then introduce some repetitive 'events': a[range(0, 1000, 30)] = 1 Question is, how do I detect the 'signal' there? Because it's far from the ideal signal if I do the 'regular' FFT I don't get a clear indication of where my 'true' signal is: f = abs(numpy.fft.rfft(a)) Is t...

Fast math operations on an array in python

I have a fairly simple math operation I'd like to perform on a array. Let me write out the example: A = numpy.ndarray((255, 255, 3), dtype=numpy.single) # .. for i in range(A.shape[0]): for j in range(A.shape[1]): x = simple_func1(i) y = simple_func2(j) A[i, j] = (alpha * x * y + beta * x**2 + gamma * y**2, 1...

sampling integers uniformly efficiently in python using numpy/scipy

I have a problem where depending on the result of a random coin flip, I have to sample a random starting position from a string. If the sampling of this random position is uniform over the string, I thought of two approaches to do it: one using multinomial from numpy.random, the other using the simple randint function of Python standard...