Professional Documents
Culture Documents
import numpy as np
newarr = arr.reshape(4, 3)
print(newarr)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Example
Try converting 1D array with 8 elements to a 2D array with 3 elements in
each dimension (will raise an error):
import numpy as np
newarr = arr.reshape(3, 3)
print(newarr)
numpy.resize() function
The resize() function is used to create a new array with the specified shape.
If the new array is larger than the original array, then the new array is filled
with repeated copies of a.
Syntax:
numpy.resize(a, new_shape)
Return value:
reshaped_array : ndarray - The new array is formed from the data in the old
array, repeated if necessary to fill out the required number of elements. The
data are repeated in the order that they are stored in memory.
Example-1: numpy.resize() function
>>> import numpy as np
array([[1, 2],
[3, 4],
[1, 2]])
array([[1, 2, 3],
[4, 1, 2]])