numpy

plotting histograms whose bar heights sum to 1 in matplotlib

I'd like to plot a normalized histogram from a vector using matplotlib. I tried the following: plt.hist(myarray, normed=True) as well as: plt.hist(myarray, normed=1) but neither option produces a y-axis from [0, 1] such that the bar heights of the histogram sum to 1. I'd like to produce such a histogram -- how can I do it? thanks...

Numpy - why value error for NaN when trying to delete rows

Hi, I have a numpy array: A = array([['id1', '1', '2', 'NaN'], ['id2', '2', '0', 'NaN']]) I also have a list: li = ['id1', 'id3', 'id6'] I wish to iterate over the array and the list and where the first element in each row of the array is not in the list, then delete that entire row from the array. My code to date: fr...

Data type problem using scipy.spatial

Hi all, I want to use scipy.spatial's KDTree to find nearest neighbor pairs in a two dimensional array (essentially a list of lists where the dimension of the nested list is 2). I generate my list of lists, pipe it into numpy's array and then create the KDTree instance. However, whenever I try to run "query" on it, I inevitably get weir...

Numpy sorting help

In Numpy, how do I create an array of indices which can be used return the values of the source array in sorted order? eg: Source: [[4 2 6 7] [1 4 8 9] [3 1 0 3]] Indices: [10 4 9 1 8 11 0 5 2 3 6 7] ...

How to convert an array of strings to an array of floats in numpy?

How to get from ["1.1", "2.2", "3.2"] to [1.1, 2.2, 3.2] in numpy? ...

deleting rows in numpy array

I have an array that might look like this: ANOVAInputMatrixValuesArray = [[ 0.96488889, 0.73641667, 0.67521429, 0.592875, 0.53172222], [ 0.78008333, 0.5938125, 0.481, 0.39883333, 0.]] Notice that one of the rows has a zero value at the end. I want to delete any row that contains a zero, while keeping any row that contains non-zero v...

Tukey five number summary in Python

I have been unable to find this function in any of the standard packages, so I wrote the one below. Before throwing it toward the Cheeseshop, however, does anyone know of an already published version? Alternatively, please suggest any improvements. Thanks. def fivenum(v): """Returns Tukey's five number summary (minimum, lower-hinge...

Arithmetic Progression in Python without storing all the values

Hello, I'm trying to represent an array of evenly spaced floats, an arithmetic progression, starting at a0 and with elements a0, a0 + a1, a0 + 2a1, a0 + 3a1, ... This is what numpy's arange() method does, but it seems to allocate memory for the whole array object and I'd like to do it using an iterator class which just stores a0, a1 and ...

Numpy - add row to array

Hi, How does one add rows to a numpy array? I have an array A: A = array([[0, 1, 2], [0, 2, 0]]) I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition. Numpy arrays do not have a method 'append' like that of lists, or so it seems. If A and X were lists I would merely...

Display array as raster image in python

I've got a numpy array in Python and I'd like to display it on-screen as a raster image. What is the simplest way to do this? It doesn't need to be particularly fancy or have a nice interface, all I need to do is to display the contents of the array as a greyscale raster image. I'm trying to transition some of my IDL code to Python with...

Select cells randomly from NumPy array - without replacement

I'm writing some modelling routines in NumPy that need to select cells randomly from a NumPy array and do some processing on them. All cells must be selected without replacement (as in, once a cell has been selected it can't be selected again, but all cells must be selected by the end). I'm transitioning from IDL where I can find a nice...

How to access numpy array within PyCXX

Hi. I would like to convert numpy array to some double* or stl vector on the c++ side. I'm actually using PyCXX for this and I can't figure out the way to access the data. I'm currently able to access and return the data buffer like this : Py::Object arrayShape(const Py::Tuple& args ){ Py::Object array= args[0]; return arra...

error message when populating cell in 2d numpy array

I am trying to populate data from some csv files into a numpy array with the following code: PreExArray=zeros([len(TestIDs),numColumns],float) for row in reader: if row[1] =='PreEx10SecondsBEFORE': PreExArray[(j-1),0]=[row[2]] However, the last line of code above throws the following error: ValueError: setting an array e...

Apply opencv threshold to a numpy array

I'm trying to apply opencv's Threshold function to a numpy array. I'm using the python bindings for opencv 2.1. It goes like this: import cv import numpy as np a = np.random.rand(1024,768) cv.Threshold(a,a,0.5,1,cv.CV_THRESH_BINARY) and this throws an error: OpenCV Error: Unsupported format or combination of formats () in threshold ...

change dtype of a single column in a 2d numpy array

I am creating a 2d array full of zeros with the following line of code: MyNewArray=zeros([4,12],float) However, the first column will need to be populated with string-type textual data, while all the other columns will need to be populated with numerical data that can be manipulated mathematically. How can I edit the code above so th...

calculating means of many matrices in numpy

I have many csv files which each contain roughly identical matrices. Each matrix is 11 columns by either 5 or 6 rows. The columns are variables and the rows are test conditions. Some of the matrices do not contain data for the last test condition, which is why there are 5 rows in some matrices and six rows in other matrices. My appli...

Plotting mplot3d / axes3D xyz surface plot with log scale?

Hi I've been looking high and low for a solution to this simple problem but I can't find it anywhere! There are a loads of posts detailing semilog / loglog plotting of data in 2D e.g. plt.setxscale('log') however I'm interested in using log scales on a 3d plot(mplot3d). I don't have the exact code to hand and so can't post it here, how...

adding textual column and row headers to numpy array

I am creating a 2d summary matrix from a 3d array using the following code: numTests=len(TestIDs) numColumns=11 numRows=6 SummaryMeansArray = p.array([]) summary3dArray = ma.zeros((numTests,numColumns,numRows)) j=0 for j in range(0,len(TestIDs)): print 'j is: ',j TestID=str(TestIDs[j]) print 'TestID is: ',TestID read...

ndarray field names for both row and column?

I'm a computer science teacher trying to create a little gradebook for myself using NumPy. But I think it would make my code easier to write if I could create an ndarray that uses field names for both the rows and columns. Here's what I've got so far: import numpy as np num_stud = 23 num_assign = 2 grades = np.zeros(num_stud, dtype=[(...

Scipy optimize.curve_fit sometimes won't converge

Hi, I'm trying to use numpy.optimize.curve_fit to estimate the frequency and phase of an on/off sequence. This is the code I'm using: from numpy import * from scipy import optimize row = array([0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0...