Skip to content
Advertisement

Tag: numpy

UserWarning: converting a masked element to nan

Executing a python script (way to long to include here) I wrote leads to a warning message. I don’t know at which line in my code this gets raised. How can I get this information? Furthermore, what does this mean exactly? In fact, I didn’t know I was using a masked array of some sort? Answer You can use the

Correct way to generate random numbers in Cython?

What is the most efficient and portable way to generate a random random in [0,1] in Cython? One approach is to use INT_MAX and rand() from the C library: Is it OK to use INT_MAX in this way? I noticed that it’s quite different from the constant you get from Python’s max int: yields: Which is the right “normalization” number

Averaging over every n elements of a numpy array

I have a numpy array. I want to create a new array which is the average over every consecutive triplet of elements. So the new array will be a third of the size as the original. As an example: should return the array: Can anyone suggest an efficient way of doing this? I’m drawing blanks. Answer If your array arr

Reversed array in numpy?

Numpy tentative tutorial suggests that a[ : :-1] is a reversed a. Can someone explain me how we got there? I understand that a[:] means for each element of a (with axis=0). Next : should denote the number of elements to skip (or period) from my understanding. Answer As others have noted, this is a python slicing technique, and numpy

Sliding window of M-by-N shape numpy.ndarray

I have a Numpy array of shape (6,2): I need a sliding window with step size 1 and window size 3 like this: I’m looking for a Numpy solution. If your solution could parametrise the shape of the original array as well as the window size and step size, that’d be great. I found this related answer Using strides for

Is it possible to create a numpy.ndarray that holds complex integers?

I would like to create numpy.ndarray objects that hold complex integer values in them. NumPy does have complex support built-in, but for floating-point formats (float and double) only; I can create an ndarray with dtype=’cfloat’, for example, but there is no analogous dtype=’cint16′. I would like to be able to create arrays that hold complex values represented using either 8-

Check if variable is defined with Numpy array?

Sometimes I have a situation where I want to test whether a variable is 0 or None or not. In pure Python, this is simply but when foo is possibly a Numpy object (such as numpy.ndarray), this does not work anymore and I get the error: and in this case I want a.any(), however this fails on non-iterable objects. Of

Advertisement