#全部行都能输出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import numpy as np
NumPy 包包含一个 Matrix 库 numpy.matlib 。此模块的函数返回矩阵而不是返回 ndarray对象。
矩阵是一个二维数组
import numpy.matlib
函数返回一个新的矩阵,且不初始化元素。里面的初始化数据无意义
m = np.matlib.empty(5)
m
matrix([[1., 1., 1., 1., 1.]])
m = np.matlib.empty((2,2))
type(m)
m
numpy.matrix
matrix([[1., 2.], [3., 4.]])
m = np.matlib.empty((2,2,4)) # 三维的数组不会是矩阵
type(m)
m
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-67-35e333e4fda2> in <module> ----> 1 m = np.matlib.empty((2,2,4)) # 三维的数组不会是矩阵 2 type(m) 3 m D:\Anaconda3\lib\site-packages\numpy\matlib.py in empty(shape, dtype, order) 47 48 """ ---> 49 return ndarray.__new__(matrix, shape, dtype, order=order) 50 51 def ones(shape, dtype=None, order='C'): D:\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py in __array_finalize__(self, obj) 180 return 181 elif (ndim > 2): --> 182 raise ValueError("shape too large to be a matrix.") 183 else: 184 newshape = self.shape ValueError: shape too large to be a matrix.
返回一个全0的矩阵
np.matlib.zeros(5)
matrix([[0., 0., 0., 0., 0.]])
np.matlib.zeros((3, 4))
matrix([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]])
返回一个全1的矩阵
np.matlib.ones((3, 5))
matrix([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])
这个函数返回一个矩阵,对角线元素为 1,其他位置为零。
np.matlib.eye(3) # 列数默认等于行数
matrix([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
np.matlib.eye(3, 4)
matrix([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.]])
np.matlib.eye(3, 4, 1) # k=1向上偏移1
matrix([[0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]])
np.matlib.eye(3, 4, -1) # k=1向下偏移1
matrix([[0., 0., 0., 0.], [1., 0., 0., 0.], [0., 1., 0., 0.]])
函数返回给定大小的单位矩阵。单位矩阵是主对角线元素都为 1 的方阵。
np.matlib.identity(3)
matrix([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
np.matlib.identity(5)
matrix([[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.]])
这个函数以[0, 1)的数组成的二维数组
np.matlib.rand(4)
matrix([[0.70354477, 0.10355941, 0.10140223, 0.91209582]])
np.matlib.rand((3, 5))
matrix([[0.71320341, 0.17103505, 0.81294741, 0.00272406, 0.14810309], [0.57606954, 0.61993777, 0.98707075, 0.56956267, 0.99584741], [0.83406569, 0.88845715, 0.50835887, 0.59140323, 0.20945002]])
np.matlib.rand((3, 4, 5)) # 不能超出二维
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-79-78c430c99182> in <module> ----> 1 np.matlib.rand((3, 4, 5)) # 不能超出二维 D:\Anaconda3\lib\site-packages\numpy\matlib.py in rand(*args) 261 if isinstance(args[0], tuple): 262 args = args[0] --> 263 return asmatrix(np.random.rand(*args)) 264 265 def randn(*args): D:\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py in asmatrix(data, dtype) 69 70 """ ---> 71 return matrix(data, dtype=dtype, copy=False) 72 73 D:\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py in __new__(subtype, data, dtype, copy) 135 else: 136 intype = N.dtype(dtype) --> 137 new = data.view(subtype) 138 if intype != data.dtype: 139 return new.astype(intype) D:\Anaconda3\lib\site-packages\numpy\matrixlib\defmatrix.py in __array_finalize__(self, obj) 180 return 181 elif (ndim > 2): --> 182 raise ValueError("shape too large to be a matrix.") 183 else: 184 newshape = self.shape ValueError: shape too large to be a matrix.
NumPy 包包含 numpy.linalg 模块,提供线性代数所需的所有功能。
函数返回两个数组的矩阵乘积
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
a
b
array([[1, 2], [3, 4]])
array([[11, 12], [13, 14]])
np.matmul(a, b)
array([[37, 40], [85, 92]])
求矩阵的行列式
只有方阵才有对应的行列式
a = np.array([[1, 2], [3, 4]])
a
array([[1, 2], [3, 4]])
np.linalg.det(a)
-2.0000000000000004
1*4-2*3
-2
a = np.array([[1, 2, 3], [1, 3, 3], [1, 0, 3]])
a
array([[1, 2, 3], [1, 3, 3], [1, 0, 3]])
np.linalg.det(a)
0.0
行列式等于0,说明存在完全的线性相关性
该函数给出了矩阵形式的线性方程的解。
例如,考虑以下线性方程:
x + y + z = 6
2y + 5z = -4
2x + 5y - z = 27
可以使用矩阵表示为:
$
\left[
\begin{matrix}
1 & 1 & 1 \\
0 & 2 & 5 \\
2 & 5 & -1
\end{matrix}
\right]
$
.
$
\left[
\begin{matrix}
x \\
y \\
z
\end{matrix}
\right]
$
=
$
\left[
\begin{matrix}
6 \\
-4 \\
27
\end{matrix}
\right]
$
方程变为:AX = B
A是一个矩阵, 即一个二维数组
B是一个一维的数组
A = np.array([[1, 1, 1], [0, 2, 5], [2, 5, -1]])
A
array([[ 1, 1, 1], [ 0, 2, 5], [ 2, 5, -1]])
B = np.array([6, -4, 27])
B
array([ 6, -4, 27])
np.linalg.solve(A, B)
array([ 5., 3., -2.])
则解出: x=5, y=3, z=-2
可以利用矩阵乘法验证下
a = np.linalg.solve(A, B)
np.matmul(A, a)
array([ 6., -4., 27.])
返回矩阵的逆矩阵
a = np.array([[1,2],[3,4]])
a
array([[1, 2], [3, 4]])
np.linalg.inv(a)
array([[-2. , 1. ], [ 1.5, -0.5]])