NumPy Matrix Library (Matrix)
NumPy contains a matrix library, numpy.matlib, whose functions return a matrix rather than an ndarray object.
numpy.matlib is dedicated to creating and manipulating matrix objects. Although NumPy's core supports multidimensional arrays (ndarray), numpy.matlib provides convenient functions that specifically return matrix types, making it easy for users to perform traditional linear algebra operations.
A A matrix is a rectangular array formed by arranging
row (row)
and column (column) elements.
The elements in a matrix can be numbers, symbols, or mathematical expressions. The following is a matrix with 2 rows and 3 columns formed by 6 numeric elements:
Transposing a matrix
In NumPy, in addition to using the numpy.transpose function to swap the dimensions of an array, you can also use the T attribute..
For example, a matrix with m rows and n columns can be converted to a matrix with n rows and m columns using the t() function.

Example
import
numpy
as
np
a
=
np
.
arange
(
12
)
.
reshape
(
3
,
4
)
(
'
Original array:
'
)
(
a
)
(
'
\n
'
)
(
'
Transposed array:
'
)
(
a
.
T
)
The output is as follows:
原数组:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Transpose 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: An integer or tuple of integers defining the shape of the new matrix
- Dtype: Optional, data type
- order: C (row-major order) or F (column-major order)
Example
import
numpy
.
matlib
import
numpy
as
np
(
np
.
matlib
.
empty
(
(
2
,
2
)
)
)
Filled with random data
The output is:
[[-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 0s.
Example
import
numpy
.
matlib
import
numpy
as
np
(
np
.
matlib
.
zeros
(
(
2
,
2
)
)
)
The output is:
[[0. 0.]
[0. 0.]]
numpy.matlib.ones()
The numpy.matlib.ones() function creates a matrix filled with 1s.
Example
import
numpy
.
matlib
import
numpy
as
np
(
np
.
matlib
.
ones
(
(
2
,
2
)
)
)
The output is:
[[1. 1.]
[1. 1.]]
numpy.matlib.eye()
The numpy.matlib.eye() function returns a matrix with 1s on the diagonal and zeros elsewhere.
numpy.matlib.eye(n, M,k, dtype)
Parameter Description:
- n: Returns the number of rows in the matrix
- M: Returns the number of columns in the matrix, defaults to n
- k: The index of the diagonal
- dtype: Data type
Example
import
numpy
.
matlib
import
numpy
as
np
(
np
.
matlib
.
eye
(
n
=
3
,
M
=
4
,
k
=
0
,
dtype
=
float
)
)
The output is:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
numpy.matlib.identity()
The numpy.matlib.identity() function returns an identity matrix of a given size.
An identity matrix is a square matrix whose elements on the diagonal from top-left to bottom-right (called the main diagonal) are all 1, and all others are 0.

Example
import
numpy
.
matlib
import
numpy
as
np
Size of 5, type float
(
np
.
matlib
.
identity
(
5
,
dtype
=
float
)
)
The output is:
[[ 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 a given size with randomly filled data.
Example
import
numpy
.
matlib
import
numpy
as
np
(
np
.
matlib
.
rand
(
3
,
3
)
)
The output is:
[[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. The two objects are interchangeable.
Example
import
numpy
.
matlib
import
numpy
as
np
i
=
np
.
matrix
(
'
1,2;3,4
'
)
(
i
)
The output is:
[[1 2]
[3 4]]
Example
import
numpy
.
matlib
import
numpy
as
np
j
=
np
.
asarray
(
i
)
(
j
)
The output is:
[[1 2]
[3 4]]
Example
import
numpy
.
matlib
import
numpy
as
np
k
=
np
.
asmatrix
(
j
)
(
k
)
The output is:
[[1 2]
[3 4]]
The following functions also exist in the NumPy namespace, but they return matrix objects.
| Function name | Parameter Description | Description |
|---|---|---|
| matrix(data[, dtype, copy]) | data: array-like or string, dtype: data type, copy: whether to copy | Create a matrix object from an array or string |
| asmatrix(data[, dtype]) | data: input data, dtype: data type (optional) | Convert input to a matrix object |
| bmat(obj[, ldict, gdict]) | obj: string or nested sequence, ldict, gdict: namespace dictionaries (optional) | Build a matrix from a string, nested sequence, or array |
| empty(shape[, dtype, order]) | shape: shape, dtype: data type, order: storage order | Create a matrix with uninitialized elements |
| zeros(shape[, dtype, order]) | shape: shape, dtype: data type, order: storage order | Create an all-zeros matrix |
| ones(shape[, dtype, order]) | shape: shape, dtype: data type, order: storage order | Create an all-ones matrix |
| eye(n[, M, k, dtype, order]) | n: number of rows, M: number of columns, k: diagonal offset, dtype: data type, order: storage order | Create a matrix with 1s on the diagonal and 0s elsewhere |
| identity(n[, dtype]) | n: size, dtype: data type (optional) | Create a square identity matrix |
| repmat(a, m, n) | a: array or matrix, m, n: repetition counts | Repeat an array or matrix m rows and n columns |
| **rand(args) | args: Specify the matrix shape | Generate a uniformly distributed random matrix of a given shape |
| **randn(args) | args: specify the matrix shape | Generate a normally distributed random matrix of a given shape |
Notes
- Although
numpy.matlibConvenient, but NumPy officially recommends usingndarrayand@operator to perform matrix operations, becausematrixthe type has some limitations and may be deprecated in future versions. - New code is recommended to prefer using
ndarray, for better compatibility and flexibility.