NumPy - 矩阵库
NumPy - 矩阵库 (旧版)
Section titled “NumPy - 矩阵库 (旧版)”NumPy 包含一个 numpy.matlib 模块和一个 numpy.matrix 类。它们主要为熟悉 MATLAB 的用户设计,提供专门的矩阵运算。然而,在现代 NumPy 中,强烈建议对所有数组操作(包括线性代数)使用标准的 numpy.ndarray 对象。 标准数组更通用(支持超过 2 的 N 维度)并且与 NumPy 生态系统的其余部分更一致。
本节介绍 numpy.matlib 函数,仅为历史背景和完整性考虑,但通常您应该优先使用其 np.ndarray 等效项。
matlib.empty()
Section titled “matlib.empty()”matlib.empty() 函数返回一个新的、未初始化的矩阵。其内容不可预测(来自内存的随机数据),并且在实践中很少使用。对于初始化数组,请使用 np.zeros 或 np.ones。
numpy.matlib.empty(shape, dtype=float, order='C')参数:
| 参数 | 描述 |
|---|---|
| shape | 定义新矩阵形状(行数,列数)的整数元组。 |
| dtype | 可选的数据类型。 |
| order | ’C’ (行主序) 或 ‘F’ (列主序) 内存布局。 |
示例 (仅作说明)
Section titled “示例 (仅作说明)”演示 matlib.empty。注意其任意的初始值。
import numpy.matlibimport numpy as np
# Creates a 2x2 matrix with uninitialized valuesmat = np.matlib.empty((2, 2))print(mat)
# Modern equivalent (initialized with zeros):arr = np.zeros((2, 2))print('\nEquivalent ndarray using np.zeros:')print(arr)可能的输出(第一个矩阵中的值会变化):
[[6.95335581e-310 6.95335581e-310] [6.95335581e-310 6.95335581e-310]]
Equivalent ndarray using np.zeros:[[0. 0.] [0. 0.]]numpy.matlib.zeros()
Section titled “numpy.matlib.zeros()”返回一个填充了零的矩阵。现代等效项是 np.zeros()。
示例:
import numpy.matlibimport numpy as np
print(np.matlib.zeros((2, 2)))
# Modern equivalent:print('\nEquivalent ndarray using np.zeros:')print(np.zeros((2, 2)))输出:
[[0. 0.] [0. 0.]]
Equivalent ndarray using np.zeros:[[0. 0.] [0. 0.]]numpy.matlib.ones()
Section titled “numpy.matlib.ones()”返回一个填充了一的矩阵。现代等效项是 np.ones()。
示例:
import numpy.matlibimport numpy as np
print(np.matlib.ones((2, 2)))
# Modern equivalent:print('\nEquivalent ndarray using np.ones:')print(np.ones((2, 2)))输出:
[[1. 1.] [1. 1.]]
Equivalent ndarray using np.ones:[[1. 1.] [1. 1.]]numpy.matlib.eye()
Section titled “numpy.matlib.eye()”返回一个在指定对角线 (k) 上为一、其余地方为零的矩阵。现代等效项是 np.eye()。
numpy.matlib.eye(n, M=None, k=0, dtype=float)参数:
| 参数 | 描述 |
|---|---|
| n | 行数。 |
| M | 列数(默认为 n)。 |
| k | 对角线的索引(0 = 主对角线,正值 = 上对角线,负值 = 下对角线)。 |
| dtype | 数据类型。 |
import numpy.matlibimport numpy as np
print(np.matlib.eye(n=3, M=4, k=0, dtype=float))
# Modern equivalent:print('\nEquivalent ndarray using np.eye:')print(np.eye(N=3, M=4, k=0, dtype=float))输出:
[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.]]
Equivalent ndarray using np.eye:[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.]]numpy.matlib.identity()
Section titled “numpy.matlib.identity()”返回一个方形单位矩阵(identity matrix,主对角线上为一,其余地方为零)。现代等效项是 np.identity()。
示例:
import numpy.matlibimport numpy as np
print(np.matlib.identity(5, dtype=float))
# Modern equivalent:print('\nEquivalent ndarray using np.identity:')print(np.identity(5, dtype=float))输出:
[[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.]]
Equivalent ndarray using np.identity:[[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()
Section titled “numpy.matlib.rand()”返回一个给定形状的矩阵,填充有来自均匀分布(uniform distribution)[0.0, 1.0) 的随机值。对于现代、可重现的随机数生成(random number generation),请使用 numpy.random.Generator.random()。
示例 (旧版)
Section titled “示例 (旧版)”import numpy.matlibimport numpy as np
# Using matlib (older style)print(np.matlib.rand(3, 3))
# Modern equivalent using default_rngrng = np.random.default_rng() # Create a random number generatorprint('\nEquivalent ndarray using rng.random:')print(rng.random((3, 3)))可能的输出(值会变化):
[[0.78917483 0.04406774 0.3003236 ] [0.47106717 0.80335701 0.94395156] [0.68432136 0.46957171 0.1159008 ]]
Equivalent ndarray using rng.random:[[0.61316899 0.98466865 0.86255636] [0.84031023 0.77267917 0.9284747 ] [0.56171617 0.94888112 0.31174664]]主要区别:np.matrix 对象严格是二维的,而 np.ndarray 对象可以具有任意数量的维度。np.matrix 还重载了乘法运算符 (*) 以表示矩阵乘法,而对于 np.ndarray,* 表示逐元素乘法(与 ndarray 进行矩阵乘法请使用 @ 或 np.matmul())。
在 np.matrix (旧版) 和 np.ndarray (首选) 之间转换。
创建一个矩阵 (旧方法):
import numpy as np
# Create a matrix (legacy)i = np.matrix([[1, 2], [3, 4]]) # Note the use of nested listsprint("Legacy np.matrix:")print(i)print(type(i))输出:
Legacy np.matrix:[[1 2] [3 4]]<class 'numpy.matrix'>将矩阵转换为 ndarray:
import numpy as np
i = np.matrix([[1, 2], [3, 4]])
# Convert matrix to ndarray (preferred type)j = np.asarray(i)print("Converted to np.ndarray:")print(j)print(type(j))输出:
Converted to np.ndarray:[[1 2] [3 4]]<class 'numpy.ndarray'>将 ndarray 转换回矩阵 (很少需要):
import numpy as np
j = np.array([[1, 2], [3, 4]]) # Start with an ndarray
# Convert ndarray to matrix (legacy)k = np.asmatrix(j)print("Converted back to np.matrix:")print(k)print(type(k))输出:
Converted back to np.matrix:[[1 2] [3 4]]<class 'numpy.matrix'>