The Shape and Reshaping of NumPy Array
- Dimensions of NumPy array
- Shape of NumPy array
- Size of NumPy array
- Reshaping a NumPy array
- Flattening a NumPy array
- Transpose of a NumPy array
Program
import numpy as np
#Two-Dimensional Array
mtrx = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
print("Matrix is:")
print(mtrx)
#Dimensions of NumPy Array
print("Dimensions of NumPy array :",mtrx.ndim)
#Shape of matrix
print("Shape of matrix is :",mtrx.shape)
#Size of NumPy Array
print("Size of matrix :",mtrx.size)
#Reshaping a NumPy Array
mtrx2 = mtrx.reshape(4,3)
print("Reshaped matrix is:")
print(mtrx2)
#Flattening a NumPy array
print("Flatten Row-major :",mtrx.flatten('C'))
print("Flatten Column-major :",mtrx.flatten('F'))
#Transpose of a NumPy array
print("Transpose of matrix:")
print(mtrx.transpose())
#Two-Dimensional Array
mtrx = np.array([[1,2,3,4],[4,5,6,7],[7,8,9,10]])
print("Matrix is:")
print(mtrx)
#Dimensions of NumPy Array
print("Dimensions of NumPy array :",mtrx.ndim)
#Shape of matrix
print("Shape of matrix is :",mtrx.shape)
#Size of NumPy Array
print("Size of matrix :",mtrx.size)
#Reshaping a NumPy Array
mtrx2 = mtrx.reshape(4,3)
print("Reshaped matrix is:")
print(mtrx2)
#Flattening a NumPy array
print("Flatten Row-major :",mtrx.flatten('C'))
print("Flatten Column-major :",mtrx.flatten('F'))
#Transpose of a NumPy array
print("Transpose of matrix:")
print(mtrx.transpose())
Output
Matrix is:
[[ 1 2 3 4]
[ 4 5 6 7]
[ 7 8 9 10]]
Dimensions of NumPy array : 2
Shape of matrix is : (3, 4)
Size of matrix : 12
Reshaped matrix is:
[[ 1 2 3]
[ 4 4 5]
[ 6 7 7]
[ 8 9 10]]
Flatten Row-major : [ 1 2 3 4 4 5 6 7 7 8 9 10]
Flatten Column-major : [ 1 4 7 2 5 8 3 6 9 4 7 10]
Transpose of matrix:
[[ 1 4 7]
[ 2 5 8]
[ 3 6 9]
[ 4 7 10]]
[[ 1 2 3 4]
[ 4 5 6 7]
[ 7 8 9 10]]
Dimensions of NumPy array : 2
Shape of matrix is : (3, 4)
Size of matrix : 12
Reshaped matrix is:
[[ 1 2 3]
[ 4 4 5]
[ 6 7 7]
[ 8 9 10]]
Flatten Row-major : [ 1 2 3 4 4 5 6 7 7 8 9 10]
Flatten Column-major : [ 1 4 7 2 5 8 3 6 9 4 7 10]
Transpose of matrix:
[[ 1 4 7]
[ 2 5 8]
[ 3 6 9]
[ 4 7 10]]