How to Reshape Size of an Array in Numpy

To reshape an array in numpy, we can use numpy.reshape function.

numpy.reshape:

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

Notice:

this function will not change array data, it means that an array contains 8 elements, the elements in new array is also 0 after it is reshaped.

For example:

>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6,7,8])
>>> b = np.reshape(a,(2,3)) # it error, because the size of array b is 2*3 = 6, not 8
>>> b = np.reshape(a,(2,4)) # correct
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> c = np.reshape(a,(2,1,4)) # correct, the size of c is 2*1*3 = 8
>>> c
array([[[1, 2, 3, 4]],

[[5, 6, 7, 8]]])

,