Delete Dimension of mutlidimensional array - python

i have an array of shape
(512, 240, 1, 3, 3)
How can I get red of the last dimension. Should work with
np.delete
but cant figure out how exactly. all the examples are in 2D...
I think I misformulated the question. What I looked for was
A = A[:,:,:,0]
Sorry

I believe (512, 240, 1, 3, 3) is a tuple (data you're working with) and not a shape of your array. To remove the last dimension (3 in your case) with numpy.delete you can do the following:
>>> import numpy as np
>>> a=np.array((512, 240, 1, 3, 3))
>>> a=np.delete(a,4)
>>> a
array([512, 240, 1, 3])
Remember that numpy.delete returns a new array and the original one stays unchanged. That's why I did a=np.delete(...).

I'm not exactly sure what you mean by delete the last dimension, but if you want to merge the two last dimensions, you can use np.reshape(yourArray, (512, 240, 1, 3*3))

Related

Get average of the numpy ndarray

I have a shape of A = (8, 64, 64, 64, 1) numpy.ndarray. We can use np.means or np.average to calculate the means of a numpy array. But I want to get the means of the 8 (64,64,64) arrays. That is, i only want 8 values, calculated from the means of the (64,64,64). Of course I can use a for loop, or use [np.means(A[i]) for i in range(A.shape[0])]. I am wondering if there is any numpy method to do this
You can use np.means axis kwarg:
np.mean(A, (1, 2, 3, 4))
The same works with np.average, too.

Add a specifc value as a new dimension of a numpy array

I have a numpy array of shape (224,224,3) after reading a image. However I would like to convert this into a shape of (4,224,224,3).
I would like to kind of repeat the same values.
I am trying to append like shown below this but it doesn't work.
np.append(image,[[[4]]],axis=1)
Instead it throws the below error
ValueError: all the input arrays must have same number of dimensions
I expect my output shape to be (4,224,224,3)
Can you guide me on how to do this?
You could use np.repeat setting axis to 0:
out = np.repeat([image], 4, axis=0)
out.shape
# (4, 224, 224, 3)

Programmatically cropping an array along all its axes in Numpy

I want to (uniformly) reduce the dimensions of a numpy array (matrix) in each direction. The code below works.
array = np.array([3, 2323, 212, 2321, 54])
padding = 1
array[padding:-padding]
Output:
[2323, 12, 2321]
But I want this to be done another way. My array will be 50-dimensional and I want to apply the last line to each dimension of the array, but I don't want to write much code.
Maybe something like
array[padding: -padding for i in range(50)]
But it doesn't work.
You can produce the relevant slices directly;
array[array.ndim * [slice(1, -1)]]
For instance,
In [31]: array = np.zeros((3, 4, 5, 6))
In [32]: array[array.ndim * [slice(1, -1)]].shape
Out[32]: (1, 2, 3, 4)

How to transform a list of 1-D ndarray to 2-D ndarray (mxnet ndarray)

In this example, I have a list of 1-d ndarray, with length 9, the list has 9 elements, and each one has shape=(2048,), so totally 9 * (2048,), I get these ndarray from mxnet so that each of the ndarray is <NDArray 2048 #cpu(0)> the array dtype=numpy.float32
If I use np.asarray to transform this list, it becomes the following result
shape=<class 'tuple'>: (9, 2048, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
Obviously, I want a 2-D array, with shape=(9, 2048), how to solve this problem?
ps: I discover this problem by saving a npy file and load it. I directly saved the list before converting it to a ndarray (so the np.save would transform the list to the ndarrary automatically) and after I loaded it, I found the shape has become something above, which is really abnormal
The answer below, np.vstack and np.array both works for the common list to ndarray problem but could not solve mine, so I doubt it is some special case of mxnet
You can use np.vstack. Here's an example:
import numpy as np
li = [np.zeros(2048) for _ in range(9)]
result = np.vstack(li)
print(result.shape)
This outputs (9, 2048) as desired.
Since the guy who gives the correct answer as comment solve my problem but did not post an answer, I would post his answer here for the others who may also encounter this problem
In fact, the np.array and mxnet.ndarray are not exactly the same, so it is dangerous to directly call numpy methods on mxnet.ndarray. To use numpy method in mxnet.ndarray, we should first transform the array to np.array, which is
mx_ndarray = mxnet.ndarray.zeros(5)
np_array = mx_ndarray.asnumpy()
Then numpy methods could be used on np_array
Since the above answer is more general(np.vstack()), I accept it and just post this answer as a reference, also, np.array() does the same thing in the above example with np.vstack()

Splitting a numpy array into two subsets of different sizes

I have a numpy array of a shape (400, 3, 3, 3) and I want to split it into two parts, so I would get arrays like (100, 3, 3, 3) and (300, 3, 3, 3).
I was playing with numpy split methods, e.g.:
subsets = np.array_split(arr, 2)
which gives me what I want, but it divides the original array into two halves the same size and I don't know how to specify these sizes. It'd be probably easy with some indexing (I guess) but I'm not sure how to do it.
As mentioned in my comment, you can use the Ellipsis notation to specify all axes:
x, y = arr[:100, ...], arr[100:, ...]

Categories

Resources