Home > Mobile >  Change the dimension of an array
Change the dimension of an array

Time:10-19

I have a number of images that have shapes (10,1134,1135). I am trying to change the shape to (10,1134,1134). I converted the image into NumPy and use the array. reshape but I get an error saying cannot reshape the array of size 12870900 into shape (10,1134,1134). Is there an alternate way to do this?

CodePudding user response:

Since you need to shrink the size of your array you need a to drop a vector on some axis of data. There are a few ways to do this through slicing.

Example Array:

arr = np.zeros((10,1134,1135))
np.shape(arr)

#output
(10, 1134, 1135)

Drop First:

new_arr=arr[:,:,1:]
np.shape(new_arr)

#output
(10, 1134, 1134)

Drop Last:

new_arr=arr[:,:,0:1134]
np.shape(new_arr)

#output
(10, 1134, 1134)

Drop Chosen:

arr_not=list(range(0, np.shape(arr)[2]))
arr_not.remove(9) #choose by index

new_arr=arr[:,:,arr_not]
np.shape(new_arr)

#output
(10, 1134, 1134)

You can also use np.delete() as follows:

new_arr=np.delete(arr, [134], 2) #choose by index
np.shape(new_arr)

#output
(10, 1134, 1134)

np.delete() documentation

  • Related