07 numpy.array 中的矩阵运算
首先我们看一下python中运算
给定一个数组(向量),让数组中每一个数乘以2
n = 10
L = [i for i in range(n)]
L*2
这样是把列表拷贝了一份,不是每个元素 x 2
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
如果要实现 每个元素乘以 2 通常要这样做
A = []
for e in L:
A.append(2*e)
但是这样的效率是怎样的呢, 我们假设数据很大
n = 1000000
L = [i for i in range(n)]
%%time
A = []
for e in L:
A.append(2*e)
CPU times: user 149 ms, sys: 15.3 ms, total: 164 ms
Wall time: 163 ms
使用列表推到式的效率会更高
%%time
A = [2*e for e in L]
CPU times: user 75.7 ms, sys: 18.1 ms, total: 93.8 ms
Wall time: 92.4 ms
import numpy as np
L = np.arange(n)
%%time
A = np.array(2*e for e in L)
CPU times: user 8.44 ms, sys: 3.3 ms, total: 11.7 ms
Wall time: 12.5 ms
%%time
A = 2 * L
CPU times: user 3.4 ms, sys: 3.01 ms, total: 6.41 ms
Wall time: 5.67 ms
n = 10
L = np.arange(n)
2 * L
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
''numpy中向量或者矩阵的运算叫Universal Functions
NumPy’s UFuncs (Universal Functions)
X = np.arange(1, 16).reshape((3, 5))
X
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
X + 1
array([[ 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16]])
X - 1
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
X * 2
array([[ 2, 4, 6, 8, 10],
[12, 14, 16, 18, 20],
[22, 24, 26, 28, 30]])
X / 2
array([[0.5, 1. , 1.5, 2. , 2.5],
[3. , 3.5, 4. , 4.5, 5. ],
[5.5, 6. , 6.5, 7. , 7.5]])
X // 2
array([[0, 1, 1, 2, 2],
[3, 3, 4, 4, 5],
[5, 6, 6, 7, 7]])
X ** 2
array([[ 1, 4, 9, 16, 25],
[ 36, 49, 64, 81, 100],
[121, 144, 169, 196, 225]])
X % 2
array([[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1]])
1 / X
array([[1. , 0.5 , 0.33333333, 0.25 , 0.2 ],
[0.16666667, 0.14285714, 0.125 , 0.11111111, 0.1 ],
[0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667]])
np.abs(X)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
np.sin(X)
array([[ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427],
[-0.2794155 , 0.6569866 , 0.98935825, 0.41211849, -0.54402111],
[-0.99999021, -0.53657292, 0.42016704, 0.99060736, 0.65028784]])
np.cos(X)
array([[ 0.54030231, -0.41614684, -0.9899925 , -0.65364362, 0.28366219],
[ 0.96017029, 0.75390225, -0.14550003, -0.91113026, -0.83907153],
[ 0.0044257 , 0.84385396, 0.90744678, 0.13673722, -0.75968791]])
np.tan(X)
array([[ 1.55740772e+00, -2.18503986e+00, -1.42546543e-01,
1.15782128e+00, -3.38051501e+00],
[-2.91006191e-01, 8.71447983e-01, -6.79971146e+00,
-4.52315659e-01, 6.48360827e-01],
[-2.25950846e+02, -6.35859929e-01, 4.63021133e-01,
7.24460662e+00, -8.55993401e-01]])
np.arctan(X)
array([[0.78539816, 1.10714872, 1.24904577, 1.32581766, 1.37340077],
[1.40564765, 1.42889927, 1.44644133, 1.46013911, 1.47112767],
[1.48013644, 1.48765509, 1.49402444, 1.49948886, 1.50422816]])
np.exp(X)
array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01,
1.48413159e+02],
[4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03,
2.20264658e+04],
[5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06,
3.26901737e+06]])
3的x此方
np.power(3, X)
array([[ 3, 9, 27, 81, 243],
[ 729, 2187, 6561, 19683, 59049],
[ 177147, 531441, 1594323, 4782969, 14348907]])
以e为底
np.log(X)
array([[0. , 0.69314718, 1.09861229, 1.38629436, 1.60943791],
[1.79175947, 1.94591015, 2.07944154, 2.19722458, 2.30258509],
[2.39789527, 2.48490665, 2.56494936, 2.63905733, 2.7080502 ]])
np.log2(X)
array([[0. , 1. , 1.5849625 , 2. , 2.32192809],
[2.5849625 , 2.80735492, 3. , 3.169925 , 3.32192809],
[3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]])
np.log10(X)
array([[0. , 0.30103 , 0.47712125, 0.60205999, 0.69897 ],
[0.77815125, 0.84509804, 0.90308999, 0.95424251, 1. ],
[1.04139269, 1.07918125, 1.11394335, 1.14612804, 1.17609126]])
"numpy更强大的功能
矩阵与矩阵运算
A = np.arange(4).reshape(2, 2)
A
array([[0, 1],
[2, 3]])
B = np.full((2, 2), 10)
B
array([[10, 10],
[10, 10]])
A + B
array([[10, 11],
[12, 13]])
A - B
array([[-10, -9],
[ -8, -7]])
点乘
A * B
array([[ 0, 10],
[20, 30]])
A.dot(B)
array([[10, 10],
[50, 50]])
A.T
array([[0, 2],
[1, 3]])
C = np.full((3, 3), 666)
A + C
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-c3cb66996588> in <module>()
----> 1 A + C
ValueError: operands could not be broadcast together with shapes (2,2) (3,3)
向量和矩阵的运算
v = np.array([1, 2])
v + A
array([[1, 3],
[3, 5]])
v + A 是可以的, 。有兴趣的同学可以查询资料自学numpy.array的broadcast
" [ ]传入列表 v 然后落到A的0维度(行) 将[1,2 ]叠加了2次
np.vstack([v] * A.shape[0])
array([[1, 2],
[1, 2]])
"这样就可以和相乘运算了
np.vstack([v] * A.shape[0]) + A
array([[1, 3],
[3, 5]])
"tile(太哦) 用于堆叠, 传入的元组行堆叠两次 列 1次
np.tile(v, (2, 1))
array([[1, 2],
[1, 2]])
np.tile(v, (2, 1)) + A
array([[1, 3],
[3, 5]])
np.tile(v, (2, 2))
array([[1, 2, 1, 2],
[1, 2, 1, 2]])
乘法
v * A
array([[0, 2],
[2, 6]])
v.dot(A)
array([4, 7])
"numpy 智能判断是行向量还是列向量,这里是列向量 将v转换成 2 x1
A.dot(v)
array([2, 8])
矩阵的逆
" 读音 :俪嗯奥格 / 因我司
"线性代数. 逆
np.linalg.inv(A)
array([[-1.5, 0.5],
[ 1. , 0. ]])
invA = np.linalg.inv(A)
A乘以A逆 为单位矩阵
A.dot(invA)
array([[1., 0.],
[0., 1.]])
反过来也成立
invA.dot(A)
array([[1., 0.],
[0., 1.]])
2 x 8 的矩阵不行 必须是方阵
X = np.arange(16).reshape((2, 8))
invX = np.linalg.inv(X)
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-55-aea614a92c0f> in <module>()
----> 1 invX = np.linalg.inv(X)
/anaconda3/lib/python3.6/site-packages/numpy/linalg/linalg.py in inv(a)
525 a, wrap = _makearray(a)
526 _assertRankAtLeast2(a)
--> 527 _assertNdSquareness(a)
528 t, result_t = _commonType(a)
529
/anaconda3/lib/python3.6/site-packages/numpy/linalg/linalg.py in _assertNdSquareness(*arrays)
213 m, n = a.shape[-2:]
214 if m != n:
--> 215 raise LinAlgError('Last 2 dimensions of the array must be square')
216
217 def _assertFinite(*arrays):
LinAlgError: Last 2 dimensions of the array must be square
"那这样的情况怎么算呢,很多时候要求逆, 数学中解决办法是求伪逆
"p是pseudo // 素 逗
矩阵的伪逆
很接近单位矩阵
pinvX = np.linalg.pinv(X)
pinvX
array([[-1.35416667e-01, 5.20833333e-02],
[-1.01190476e-01, 4.16666667e-02],
[-6.69642857e-02, 3.12500000e-02],
[-3.27380952e-02, 2.08333333e-02],
[ 1.48809524e-03, 1.04166667e-02],
[ 3.57142857e-02, -1.04083409e-17],
[ 6.99404762e-02, -1.04166667e-02],
[ 1.04166667e-01, -2.08333333e-02]])
X.dot(pinvX)
array([[ 1.00000000e+00, -2.49800181e-16],
[ 0.00000000e+00, 1.00000000e+00]])
矩阵的伪逆又被称为“广义逆矩阵”,有兴趣的同学可以翻看线性教材课本查看更多额广义逆矩阵相关的性质。中文wiki链接: https://zh.wikipedia.org/wiki/%E5%B9%BF%E4%B9%89%E9%80%86%E9%98%B5