Reshape¶
In [1]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4) Note: you may need to restart the kernel to use updated packages.
In [3]:
foo = np.arange(start=1, stop=9)
foo
Out[3]:
array([1, 2, 3, 4, 5, 6, 7, 8])
In [4]:
foo.reshape(2, 4)
Out[4]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [6]:
np.reshape(a=foo, newshape=(2,4))
Out[6]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [8]:
bar = foo.reshape(2,4)
bar
Out[8]:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
In [11]:
bar.reshape((4,2), order='C')
Out[11]:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
In [12]:
bar.reshape((4,2), order='F')
Out[12]:
array([[1, 3],
[5, 7],
[2, 4],
[6, 8]])
In [13]:
bar.reshape((2,2,2), order='C')
Out[13]:
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
In [14]:
bar.reshape((2,2,2), order='F')
Out[14]:
array([[[1, 3],
[2, 4]],
[[5, 7],
[6, 8]]])
In [19]:
bar.reshape(4,-1)
Out[19]:
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
In [22]:
print(bar)
bar.shape = (8, 1)
bar
[[1 2] [3 4] [5 6] [7 8]]
Out[22]:
array([[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]])
In [23]:
bar.T
Out[23]:
array([[1, 2, 3, 4, 5, 6, 7, 8]])
In [24]:
np.transpose(bar)
Out[24]:
array([[1, 2, 3, 4, 5, 6, 7, 8]])