How can I calculate matrix mean values along a matrix, but to remove nan values from calculation? (For R people, think na.rm = TRUE). Here is my [non-]working example: With NaNs removed, my expected output would be: Answer I think what you want is a masked array: Edit: Combining all of the timing data Returns:
Tag: numpy
Histogram Matplotlib
So I have a little problem. I have a data set in scipy that is already in the histogram format, so I have the center of the bins and the number of events per bin. How can I now plot is as a histogram. I tried just doing but it didn’t like that. Any recommendations? Answer The object-oriented interface is
Rank items in an array using Python/NumPy, without sorting array twice
I have an array of numbers and I’d like to create another array that represents the rank of each item in the first array. I’m using Python and NumPy. For example: Here’s the best method I’ve come up with: Are there any better/faster methods that avoid sorting the array twice? Answer Use advanced indexing on the left-hand side in the
Discrete Laplacian (del2 equivalent) in Python
I need the Python / Numpy equivalent of Matlab (Octave) discrete Laplacian operator (function) del2(). I tried couple Python solutions, none of which seem to match the output of del2. On Octave I have this gives the result On Python I tried which gives the result I also tried That gives the result So none of the outputs seem to
slicing a 2d numpy array
The following code: Is generating the following error message: I looked up the syntax at this link and I seem to be using the correct syntax to slice. However, when I type into the Python shell, it gives me the following output, which is clearly wrong, and is probably what is throwing the error: Can anyone show me how to
Find indices of elements equal to zero in a NumPy array
NumPy has the efficient function/method nonzero() to identify the indices of non-zero elements in an ndarray object. What is the most efficient way to obtain the indices of the elements that do have a value of zero? Answer numpy.where() is my favorite. The method where returns a tuple of ndarrays, each corresponding to a different dimension of the input. Since
Sum array by number in numpy
Assuming I have a numpy array like: [1,2,3,4,5,6] and another array: [0,0,1,2,2,1] I want to sum the items in the first array by group (the second array) and obtain n-groups results in group number order (in this case the result would be [3, 9, 9]). How do I do this in numpy? Answer There’s more than one way to do
Efficiently detect sign-changes in python
I want to do exactly what this guy did: Python – count sign changes However I need to optimize it to run super fast. In brief I want to take a time series and tell every time it crosses crosses zero (changes sign). I want to record the time in between zero crossings. Since this is real data (32 bit
in numpy what is the multi dimensional equivalent of take
I have this bit of code Where blocks is a 3 dimensional numpy array. What I’d like to do is replace the list comprehension with something like numpy.take, however take seems to only deal with single dimension indices. Is there something like take that will work with multidimensional indices? Also I know you could do this with a transpose, slice
How to write a multidimensional array to a text file?
In another question, other users offered some help if I could supply the array I was having trouble with. However, I even fail at a basic I/O task, such as writing an array to a file. Can anyone explain what kind of loop I would need to write a 4x11x14 numpy array to file? This array consist of four 11