NumPy Matrix Library
NumPy includes a matrix library numpy.matlib, which returns a matrix instead of an ndarray object.
A matrix can contain numbers, symbols, or mathematical expressions as elements. Below is a 2-row by 3-column matrix composed of 6 numeric elements:
Transpose Matrix
In NumPy, you can use the numpy.transpose function to swap the dimensions of an array, or you can use the T
attribute.
For example, given a matrix with m rows and n columns, using the t() function will convert it into a matrix with n rows and m columns.
Example
import numpy as np
a = np.arange(12).reshape(3,4)
print ('Original array:')
print (a)
print ('\n')
print ('Transposed array:')
print (a.T)
Output result:
Original array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Transposed array:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
matlib.empty()
The matlib.empty() function returns a new matrix, with the syntax:
numpy.matlib.empty(shape, dtype, order)
Parameter Description:
- shape: Integer or tuple of integers defining the new matrix's shape.
- dtype: Optional, data type.
- order: C (row-major) or F (column-major).
Example
import numpy.matlib
import numpy as np
print (np.matlib.empty((2,2)))
# Filled with random data
Output result:
[[-1.49166815e-154 -1.49166815e-154]
[ 2.17371491e-313 2.52720790e-212]]
numpy.matlib.zeros()
The numpy.matlib.zeros() function creates a matrix filled with zeros.
Example
import numpy.matlib
import numpy as np
print (np.matlib.zeros((2,2)))
Output result:
[[0. 0.]
[0. 0.]]
numpy.matlib.ones()
The numpy.matlib.ones() function creates a matrix filled with ones.
Example
import numpy.matlib
import numpy as np
print (np.matlib.ones((2,2)))
Output result:
[[1. 1.]
[1. 1.]]
numpy.matlib.eye()
The numpy.matlib.eye() function returns a matrix with ones on the diagonal and zeros elsewhere.
numpy.matlib.eye(n, M, k, dtype)
Parameter Description:
- n: Number of rows in the output matrix.
- M: Number of columns in the output matrix, default is n.
- k: Index of the diagonal.
- dtype: Data type.
Example
import numpy.matlib
import numpy as np
print (np.matlib.eye(n = 3, M = 4, k = 0, dtype = float))
Output result:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
numpy.matlib.identity()
The numpy.matlib.identity() function returns the identity matrix of the given size.
The identity matrix is a square matrix with ones on the main diagonal (from the top left to the bottom right) and zeros elsewhere.
Example
import numpy.matlib
import numpy as np
# Size 5, type float
print (np.matlib.identity(5, dtype = float))
Output result:
[[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1.]]
numpy.matlib.rand()
The numpy.matlib.rand() function creates a matrix of the given size filled with random data.
Example
import numpy.matlib
import numpy as np
print (np.matlib.rand(2,2))
Output result:
[[0.82779383 0.00623151]
[0.23265945 0.79915854]]
print(np.matlib.rand(3,3))
Output:
[[0.23966718 0.16147628 0.14162 ]
[0.28379085 0.59934741 0.62985825]
[0.99527238 0.11137883 0.41105367]]
A matrix is always two-dimensional, while an ndarray is an n-dimensional array. Both objects are interchangeable.
Example
import numpy.matlib
import numpy as np
i = np.matrix('1,2;3,4')
print(i)
Output:
[[1 2]
[3 4]]
Example
import numpy.matlib
import numpy as np
j = np.asarray(i)
print(j)
Output:
[[1 2]
[3 4]]
Example
import numpy.matlib
import numpy as np
k = np.asmatrix(j)
print(k)
Output:
[[1 2]
[3 4]]