Home > OS >  Numpy array reshape customization python
Numpy array reshape customization python

Time:10-21

I have an np array n and the shape is (250,250). after that I converted (final array w)it into (250,250,3) because I need to do some operation on it.

is it possible to convert the shape of w to (250,250)?

thanks in advance. I tried some reshaping operation of Numpy but it does not work!

Numpy reshaping array

Comparing two NumPy arrays for equality, element-wise

Numpy reshaping array

CodePudding user response:

numpy.reshape

Gives a new shape to an array without changing its data.

so this is not right to convert array with shape of (250,250,3) into array with shape of (250,250) as 1st does have 187500 cells and 2nd does have 62500 cells.

You probably should use slicing, consider following example

import numpy as np
arr = np.array([[[0,1],[2,3]],[[4,5],[6,7]]])  # has shape (2,2,2)
arr2 = arr[:,:,0]  # get certain cross-section, check what will happend if you use 1 inplace of 0 and 2 inplace of 0
print("arr2")
print(arr2)
print("arr2.shape",arr2.shape)

output

arr2
[[0 2]
 [4 6]]
arr2.shape (2, 2)
  • Related