I create a masked array A with shape (8,1,51,120) and a data masked array B with shape (11232,51,120).
Now I make a loop to select bar of B[step,:,:] array and append it at the end of A[foo,0,:,:]. Here is some pseudo-code to describe what I want:
for step in range(B.shape[0]):
foo = select_func(step)
A[foo,A.shape[1]-1,:,:].append(B[step,:,:])
print(A[foo,:,:,:].shape)
Finally I want A[foo,:,:,:].shape = (bar,51,120).
It turns out very hard to append B to A using np.ma.dstack( or np.ma.append( not only because A not change as I do but also it made new masked array.
I try this: Append 2D array to 3D array, extending third dimension, but I want my original A array grow up not to make new array.
Related
I start by initializing:
3dArray = np.zeros(shape=(0,250,2))
Within a loop, I go through a file and pick out sections of data, resulting in multiple 2D arrays of size (250,2).
For each of these sections, I'm trying to stack these 2d arrays into the 3d array, so that the 0th dimension increases by 1 every time, i.e., 3dArray is of shape (1,250,2), then (2,250,2) etc.
I tried using:
3dArray = np.dstack((3dArray, new2DArray))
3DArray = np.vstack((3DArray,new2Darray.reshape(1,250,2)))
Side Note: Python doesn't allow variable names to start with numbers.
I want to reshape this array: np.array(np.arange(15)) to a 3d array that is built from a 3x3 array and a 3x2 array.
I've tried to do it with the reshape method but it didn't work.
I thought that maybe reshape can get a number of tuples maybe.
a=np.array(np.arange(15)).reshape(1,((3,2),(3,3)))
but I then I saw it cant.
How can I reshape it then? is there a nice way?
a multidimensional array can't have dimensions with different size.
but if you want a tuple you will need to split the array in 2 parts, the first that match in size with the 3x3 array and the second that match the 3x2, at this point you'll have 2 one dimensional array, then reshape them
arr1 = arr1.reshape((3,3))
arr2 = arr2.reshape((3,2))
tuple = arr1, arr2
Suppose I have a numpy array A which can be of any dimensions len(A.shape) can be 1,2,3,..etc. and a corresponding array, crop which len(crop) = len(A.shape) and I want to extract the interior values of A using crop. Here is an example for 2D array.
A = np.random.rand(30).reshape([5,6])
crop = np.array([1,2])
Wanted output:
A[crop[0]:-crop[0], crop[1]:-crop[1])
Assuming value of crop will be reasonable with respect to size of A. How do I do this for any dimension of array A ?
Here's one way with slice notation -
A[tuple([slice(i,-i,None) for i in crop])]
Or with the shorthand np.s_ -
A[tuple([np.s_[i:-i] for i in crop])]
If the start and end indices are given for each dimension, we can do something like as shown in Slicing NumPy array given start and end indices for generic dimensions.
I have a numpy boolean vector of shape 1 x N, and an 2d array with shape 160 x N. What is a fast way of subsetting the columns of the 2d array such that for each index of the boolean vector that has True in it, the column is kept, and for each index of the boolean vector that has False in it, the column is discarded?
If you call the vector mask and the array features, i've found the following to be far too slow: np.array([f[mask] for f in features])
Is there a better way? I feel like there has to be, right?
You can try this,
new_array = 2d_array[:,bool_array==True]
So depending on the axes you can select which one you want to remove. In case you get a 1-d array, then you can just reshape it and get the required array. This method will be faster also.
How to create 3 dimensions matrix in numpy , like matlab a(:,:,:) . I try to convert matlab code that create 3d matrix to python by use numpy.array and i don't know how to create 3d matrix/array in numpy
a=np.empty((2,3,5))
creates a 2x3x5 array. (There is also np.zeros if you want the values initialized.)
You can also reshape existing arrays:
a=np.arange(30).reshape(2,3,5)
np.arange(30) creates a 1-d array with values from 0..29. The reshape() method returns an array containing the same data with a new shape.