1、矩阵和向量积
In [1]: import numpy as np
In [2]: np.dot(3,4)
Out[2]: 12
In [3]: np.dot([1,2,3],[4,5,6])
Out[3]: 32
知识点:对于一维数组,其结果等于两向量的内积:设向量
a=(x1,y1),向量b=(x2,y2),结果等于x1*x2+y1*y2
In [5]: a = np.array([[1,2],[3,4],[2,5]])
...: b = np.array([[2,3,1],[4,5,2]])
...: np.dot(a,b)
...:
Out[5]:
array([[10, 13, 5],
[22, 29, 11],
[24, 31, 12]])
知识点:矩阵乘法,第一矩阵A的行数必须等于第二个矩阵B的列数;矩阵A乘以矩阵B得到的结果C,其第m行n列元素等于矩阵A的第m行元素乘以矩阵B第n列对应元素之和
In [3]: import numpy as np
In [4]: a = np.array(range(12)).reshape(2,3,1,2)
...: b = np.array(range(12)).reshape(3,2,2)
...:
In [5]: a
Out[5]:
array([[[[ 0, 1]],
[[ 2, 3]],
[[ 4, 5]]],
[[[ 6, 7]],
[[ 8, 9]],
[[10, 11]]]])
In [6]: b
Out[6]:
array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]]])
In [7]: np.dot(a,b)
Out[7]:
array([[[[[ 2, 3],
[ 6, 7],
[ 10, 11]]],
[[[ 6, 11],
[ 26, 31],
[ 46, 51]]],
[[[ 10, 19],
[ 46, 55],
[ 82, 91]]]],
[[[[ 14, 27],
[ 66, 79],
[118, 131]]],
[[[ 18, 35],
[ 86, 103],
[154, 171]]],
[[[ 22, 43],
[106, 127],
[190, 211]]]]])
In [8]: np.dot(a,b).shape
Out[8]: (2, 3, 1, 3, 2)
计算过程:
In [9]: np.dot(np.array([[[[0,1]]]]),np.array([[[0,1],[2,3]]]))
Out[9]: array([[[[[2, 3]]]]])
In [10]: np.dot(np.array([[[[0,1]]]]),np.array([[[4,5],[6,7]]]))
Out[10]: array([[[[[6, 7]]]]])
In [11]: np.dot(np.array([[[[0,1]]]]),np.array([[[8,9],[10,11]]]))
Out[11]: array([[[[[10, 11]]]]])
In [1]: import numpy as np
...: a = np.array([[1, 4], [5, 6]])
...: b = np.array([[4, 1], [2, 2]])
...: np.vdot(a,b)
...:
Out[1]: 30
a、b数组扁平化即将多维数组转换成一维数组,可以使用ravel函数处理
In [2]: np.vdot(a.ravel(), b.ravel())
Out[2]: 30
②参数a、b为复数
In [3]: a = np.array([1+2j,3+4j])
...: b = np.array([5+6j,7+8j])
...: np.vdot(a,b)
...:
Out[3]: (70-8j)
In [4]: np.vdot(b,a)
Out[4]: (70+8j)
通过上述结果可知:np.vdot(a,b)和np.vdot(b,a)计算出来的结果刚好互为共轭复数关系(实部相同,虚部互为相反数),其计算结果为取vdot函数中的第一个参数的共轭复数与另外一个参数点积。
In [5]: import numpy as np
...: a = np.array([1+2j,3+4j])
...: b = np.array([5+6j,7+8j])
...: c = np.array([1-2j,3-4j])
...: d = np.array([5-6j,7-8j])
...: np.dot(c,b)
...:
Out[5]: (70-8j)
In [6]: np.dot(a,d)
Out[6]: (70+8j)
2、求解方程与求逆矩阵
In [1]: import numpy as np
...: from numpy.linalg import inv
...: a = np.array([[1., 2.], [3., 4.]])
...: inv(a)
...:
Out[1]:
array([[-2. , 1. ],
[ 1.5, -0.5]])
知识点:矩阵与其逆矩阵点积等于同阶单位矩阵,求解逆矩阵的方法有:待定系数法、伴随矩阵法、初等变换法
In [2]: np.dot(a,inv(a))
Out[2]:
array([[ 1.00000000e+00, 1.11022302e-16],
[ 0.00000000e+00, 1.00000000e+00]])
比较两数组:numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)
In [3]: np.allclose(np.dot(a,inv(a)),np.eye(2))
Out[3]: True
In [13]: import numpy as np
...: from numpy.linalg import lstsq
...: X1 = np.array([[1, 6, 2], [1, 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0
...: ]])
...: y1= np.array([[7], [9], [13], [17.5], [18]])
...: np.linalg.lstsq(X1, y1)
...:
Out[13]:
(array([[ 1.1875 ],
[ 1.01041667],
[ 0.39583333]]),
array([ 8.22916667]),
3,
array([ 26.97402951, 2.46027806, 0.59056212]))
从上述结果可知:返回元组,元组中四个元素,第一元素表示所求的最小二乘解,第二个元素表示残差总和,第三个元素表示X1矩阵秩,第四个元素表示X1的奇异值
In [14]: import numpy as np
...: from numpy.linalg import lstsq
...: X1 = np.array([[1, 6, 2], [1, 8, 1], [1, 10, 0], [1, 14, 2], [1, 18, 0
...: ]])
...: y1= np.array([[7,8], [9,7], [13,10], [17.5,16], [18,17]])
...: np.linalg.lstsq(X1, y1)
...:
Out[14]:
(array([[ 1.1875 , -1.125 ],
[ 1.01041667, 1.02083333],
[ 0.39583333, 1.29166667]]),
array([ 8.22916667, 2.91666667]),
3,
array([ 26.97402951, 2.46027806, 0.59056212]))
通过上面两个结果对比分析:参数b维度增加,第一个、第二个元素数组维度也变化,其对应的第K列分别表示对b数组中第k列的最小二乘法求解、残差总和