Delete slice from a 3D numpy.MaskedArray - python

I have a 3D numpy.MaskedArray and I want to delete the 3rd slice. If I was a numpy.array I could just use the numpy.delete function, e.g. np.delete(arr, obj=3, axis=0). However, this function is not available for np.MaskedArrays. How can I do this in a pythonic way and without changing the array type?

My memory of np.delete code is that in your case it would do:
np.ma.vstack([ arr[:3], arr[4:])

Related

Efficiently unwrap in multiple dimensions with numpy

Let's assume I have an array of phases (from complex numbers)
A = np.angle(np.random.uniform(-1,1,[10,10,10]) + 1j*np.random.uniform(-1,1,[10,10,10]))
I would now like to unwrap this array in ALL dimensions. In the above 3D case I would do
A_unwrapped = np.unwrap(np.unwrap(np.unwrap(A,axis=0), axis=1),axis=2)
While this is still feasible in the 3D case, in case of higher dimensionality, this approach seems a little cumbersome to me. Is there a more efficient way to do this with numpy?
You could use np.apply_over_axes, which is supposed to apply a function over each dimension of an array in turn:
np.apply_over_axes(np.unwrap, A, np.arange(len(A.shape)))
I believe this should do it.
I'm not sure if there is a way to bypass performing the unwrap operation along each axis. Obviously if it acted on individual elements you could use vectorization, but that doesn't seem to be an option here. What you can do that will at least make the code cleaner is create a loop over the dimensions:
for dim in range(len(A.shape)):
A = np.unwrap(A, axis=dim)
You could also repeatedly apply a function that takes the dimension on which to operate as a parameter:
reduce(lambda A, axis: np.unwrap(A, axis=axis), range(len(A.shape)), A)
Remember that in Python 3 reduce needs to be imported from functools.

Numpy maskedarray missing stack function

I'm using Numpy 1.12.1.
According to the documentation for vstack
This function continues to be supported for backward compatibility, but you should prefer np.concatenate or np.stack. The np.stack function was added in NumPy 1.10.
But there is no numpy.ma.stack function. The np.stack function does not function correctly when trying to stack masked arrays.
Should I continue using numpy.ma.vstack or is there another way to achieve the same functionality without relying on a seemingly deprecated function?
I think that deprecation statement overstates the usefulness of stack. No one is going to stop using vstack or hstack. But these are all front ends of concatenate. I encourage everyone to look at the source code for these functions to see how they manipulate dimensions prior to using `concatenate.
I see stack as more of a generalization of np.array. When given a list of 2d arrays, np.array joins them on a new axis at the front, producing a 3d array. np.stack lets you join them on 2 other new axes.
np.stack can replace vstack when given a list of 1d arrays. but not if given a mix of 1 and 2d.
Masked arrays at a bit of backwater, and don't get new features as quickly. Use the functions it provides, and don't worry about the stack docs.
ma.vstack does (where `func= np.vstack):
_d = func(tuple([np.asarray(a) for a in x]), *args, **params)
_m = func(tuple([getmaskarray(a) for a in x]), *args, **params)
return masked_array(_d, mask=_m)
It does a vstack on the .data and mask parts, and then creates a new masked array. Looks like it could easily be extended to work with np.stack.

python array initialisation (preallocation) with nans

I want to initialise an array that will hold some data. I have created a random matrix (using np.empty) and then multiplied it by np.nan. Is there anything wrong with that? Or is there a better practice that I should stick to?
To further explain my situation: I have data I need to store in an array. Say I have 8 rows of data. The number of elements in each row is not equal, so my matrix row length needs to be as long as the longest row. In other rows, some elements will not be filled. I don't want to use zeros since some of my data might actually be zeros.
I realise I can use some value I know my data will never, but nans is definitely clearer. Just wondering if that can cause any issues later with processing. I realise I need to use nanmax instead of max and so on.
I have created a random matrix (using np.empty) and then multiplied it by np.nan. Is there anything wrong with that? Or is there a better practice that I should stick to?
You can use np.full, for example:
np.full((100, 100), np.nan)
However depending on your needs you could have a look at numpy.ma for masked arrays or scipy.sparse for sparse matrices. It may or may not be suitable, though. Either way you may need to use different functions from the corresponding module instead of the normal numpy ufuncs.
A way I like to do it which probably isn't the best but it's easy to remember is adding a 'nans' method to the numpy object this way:
import numpy as np
def nans(n):
return np.array([np.nan for i in range(n)])
setattr(np,'nans',nans)
and now you can simply use np.nans as if it was the np.zeros:
np.nans(10)

The efficient way of Array transformation by using numpy

How to change the ARRAY U(Nz,Ny, Nx) to U(Nx,Ny, Nz) by using numpy? thanks
Just numpy.transpose(U) or U.T.
In general, if you want to change the order of data in numpy array, see http://docs.scipy.org/doc/numpy-1.10.1/reference/routines.array-manipulation.html#rearranging-elements.
The np.fliplr() and np.flipud() functions can be particularly useful when the transpose is not actually what you want.
Additionally, more general element reordering can be done by creating an index mask, partially explained here

append versus resize for numpy array

I would like to append a value at the end of my numpy.array.
I saw numpy.append function but this performs an exact copy of the original array adding at last my new value. I would like to avoid copies since my arrays are big.
I am using resize method and then set the last index available to the new value.
Can you confirm that resize is the best way to append a value at the end?
Is it not moving memory around someway?
oldSize = myArray,shape(0)
myArray.resize( oldSize + 1 )
myArray[oldSize] = newValue
My simple timing experiment of append vs. resizing showed that resizing is about 3x faster and its the fastest way that I can think of to do this. Also, the answer to this question seems to imply that resizing the array is the way to go because it is in-place.
Verdict:
Use resize
P.S. You also might want to check out this discussion from a numpy mailing list.

Categories