概述
学习目标:
结合矩阵去学习 Python 中 numpy学习内容:
- 矩阵
- Python 的 numpy 的基础
学习时间:
2020.11.09学习产出:
1. 矩阵和 numpy
1.1 创建一维矩阵
In [1]: import numpy as np
In [2]: vector_row = np.array([1, 2, 3])
In [3]: vector_column = np.array([[1], [2], [3]])
# 注意两个 中括号
In [4]: vector_row
Out[4]: array([1, 2, 3])
In [5]: vector_column
Out[5]:
array([[1],
[2],
[3]])
1.2 创建二维矩阵
In [9]: matrix_2d = np.array([[1, 2, 4], [3, 6.5, 9], [3, 2, 4]])
# 注意两个 中括号
In [10]: matrix_2d
Out[10]:
array([[1. , 2. , 4. ],
[3. , 6.5, 9. ],
[3. , 2. , 4. ]])
1.3 创建稀疏矩阵
1.4 选择元素
In [17]: vector_column
Out[17]:
array([[1],
[2],
[3]])
In [18]: vector_row
Out[18]: array([1, 2, 3])
In [19]: matrix_2d
Out[19]:
array([[1. , 2. , 4. ],
[3. , 6.5, 9. ],
[3. , 2. , 4. ]])
#############################################
# 元素选择 从 0 开始
In [20]: vector_column[1]
Out[20]: array([2])
In [21]: matrix_2d[0, 0]
Out[21]: 1.0
In [22]: matrix_2d[0][0]
Out[22]: 1.0
# Select all elements of a vector
In [23]: matrix_2d[:]
Out[23]:
array([[1. , 2. , 4. ],
[3. , 6.5, 9. ],
[3. , 2. , 4. ]])
# Select the elements of the 2nd column
In [24]: matrix_2d[:, 1]
Out[24]: array([2. , 6.5, 2. ])
# Select the elements of the 1st row
In [25]: matrix_2d[0, :]
Out[25]: array([1., 2., 4.])
# Select the elements of the 2nd and 3rd column in the 1st row
# 选择第 1 行第 2,3 列的元素
In [26]: matrix_2d[0, 1:3]
Out[26]: array([2., 4.])
1.5 矩阵的描述
In [29]: matrix_2d = np.array([[1,2,3], [2,3.3,4], [6, 6, 6], [9, 9.9, 90]])
In [30]: matrix_2d
Out[30]:
array([[ 1. ,
2. ,
3. ],
[ 2. ,
3.3,
4. ],
[ 6. ,
6. ,
6. ],
[ 9. ,
9.9, 90. ]])
In [31]: matrix_2d.shape
Out[31]: (4, 3)
# 行 × 列
In [32]: matrix_2d.size
Out[32]: 12
# 行 × 列
In [34]: matrix_2d.ndim
Out[34]: 2
# 二维矩阵
1.6 矩阵的操作符
# all elements add 10
In [30]: matrix_2d
Out[30]:
array([[ 1. ,
2. ,
3. ],
[ 2. ,
3.3,
4. ],
[ 6. ,
6. ,
6. ],
[ 9. ,
9.9, 90. ]])
In [35]: mat_10 = matrix_2d + 10
# add directly
In [36]: mat_10
Out[36]:
array([[ 11. ,
12. ,
13. ],
[ 12. ,
13.3,
14. ],
[ 16. ,
16. ,
16. ],
[ 19. ,
19.9, 100. ]])
# all elements divied by 2
In [37]: mat_d_2 = matrix_2d / 2
In [38]: mat_d_2
Out[38]:
array([[ 0.5 ,
1.
,
1.5 ],
[ 1.
,
1.65,
2.
],
[ 3.
,
3.
,
3.
],
[ 4.5 ,
4.95, 45.
]])
1.7 查找最大值和最小值
In [39]: mat_1 = matrix_2d
In [40]: mat_1
Out[40]:
array([[ 1. ,
2. ,
3. ],
[ 2. ,
3.3,
4. ],
[ 6. ,
6. ,
6. ],
[ 9. ,
9.9, 90. ]])
In [44]: np.max(mat_1)
Out[44]: 90.0
In [45]: np.min(mat_1)
Out[45]: 1.0
# Find maximum element in each column
每列最大值
In [46]: np.max(mat_1, axis=0)
Out[46]: array([ 9. ,
9.9, 90. ])
# Find minimum element in each row 每行最大值
In [47]: np.min(mat_1, axis=1)
Out[47]: array([1., 2., 6., 9.])
1.8 计算期望,总体方差,总体标准差
期
望
:
E
(
X
)
=
1
n
∑
i
=
1
n
(
x
i
)
期望: E(boldsymbol{X}) = {frac{1}{n}}sum_{i=1}^{n}(x_i)
期望:E(X)=n1i=1∑n(xi)
总
体
方
差
:
D
(
X
)
=
E
(
X
−
E
(
X
)
)
=
1
n
∑
i
=
1
n
(
x
i
−
x
ˉ
)
2
总体方差:D(boldsymbol{X}) = E(boldsymbol{X} - E(boldsymbol{X})) = {frac{1}{n}}sum_{i=1}^{n}(x_i - bar{x})^2
总体方差:D(X)=E(X−E(X))=n1i=1∑n(xi−xˉ)2
总
体
标
准
差
:
σ
=
D
(
X
)
=
1
n
∑
i
=
1
n
(
x
i
−
x
ˉ
)
2
总体标准差:sigma = sqrt{D(boldsymbol{X})}= sqrt{{frac{1}{n}}sum_{i=1}^{n}(x_i - bar{x})^2}
总体标准差:σ=D(X)=n1i=1∑n(xi−xˉ)2
In [48]: matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [49]: matrix
Out[49]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# 下面的操作会把整个矩阵看作是一维的:
X = array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
# Average
In [50]: np.mean(matrix)
Out[50]: 5.0
# Total Variance
In [51]: np.var(matrix)
Out[51]: 6.666666666666667
# Total Standard Deviation
In [52]: np.std(matrix)
Out[52]: 2.581988897471611
1.9 将矩阵 reshape
In [56]: matrix_2d
Out[56]:
array([[ 1. ,
2. ,
3. ],
[ 2. ,
3.3,
4. ],
[ 6. ,
6. ,
6. ],
[ 9. ,
9.9, 90. ]])
# make matrix_2d (4 × 3) to (12 × 1)
In [58]: matrix_2d.reshape(12,1)
Out[58]:
array([[ 1. ],
[ 2. ],
[ 3. ],
[ 2. ],
[ 3.3],
[ 4. ],
[ 6. ],
[ 6. ],
[ 6. ],
[ 9. ],
[ 9.9],
[90. ]])
# Parameters -1 represent any number
In [61]: matrix_2d
Out[61]:
array([[ 1. ,
2. ,
3. ],
[ 2. ,
3.3,
4. ],
[ 6. ,
6. ,
6. ],
[ 9. ,
9.9, 90. ]])
In [62]: matrix
Out[62]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [61]: matrix_2d
Out[61]:
array([[ 1. ,
2. ,
3. ],
[ 2. ,
3.3,
4. ],
[ 6. ,
6. ,
6. ],
[ 9. ,
9.9, 90. ]])
In [62]: matrix
Out[62]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [63]: matrix_2d.reshape(1, -1)
# equals (1 × 12)
Out[63]:
array([[ 1. ,
2. ,
3. ,
2. ,
3.3,
4. ,
6. ,
6. ,
6. ,
9. ,
9.9, 90. ]])
In [64]: matrix.reshape(1, -1)
# equals (1 × 9)
Out[64]: array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
1.10 矩阵的转置
In [65]: matrix_2d
Out[65]:
array([[ 1. ,
2. ,
3. ],
[ 2. ,
3.3,
4. ],
[ 6. ,
6. ,
6. ],
[ 9. ,
9.9, 90. ]])
In [66]: matrix_2d.T
Out[66]:
array([[ 1. ,
2. ,
6. ,
9. ],
[ 2. ,
3.3,
6. ,
9.9],
[ 3. ,
4. ,
6. , 90. ]])
###########################################
In [67]: mat_1 = matrix_2d.reshape(1, -1)
In [68]: mat_1
Out[68]:
array([[ 1. ,
2. ,
3. ,
2. ,
3.3,
4. ,
6. ,
6. ,
6. ,
9. ,
9.9,
90. ]])
In [69]: mat_1.T
# 行矩阵的转置是列矩阵
Out[69]:
array([[ 1. ],
[ 2. ],
[ 3. ],
[ 2. ],
[ 3.3],
[ 4. ],
[ 6. ],
[ 6. ],
[ 6. ],
[ 9. ],
[ 9.9],
[90. ]])
1.11 将一个矩阵展平
In [71]: matrix
Out[71]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [72]: matrix.flatten()
Out[72]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
## 注意 用 reshape 会有两个中括号,遍历调用数据需要用两个 for
In [73]: matrix.reshape(1, -1)
Out[73]: array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
1.12 寻找矩阵的秩
In [76]: matrix
# 2
Out[76]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [77]: matrix2
# 1
Out[77]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
In [78]: np.linalg.matrix_rank(matrix)
Out[78]: 2
In [79]: np.linalg.matrix_rank(matrix2)
Out[79]: 1
1.13 计算行列式的值
In [80]: matrix
Out[80]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [81]: np.linalg.det(matrix)
# 会发现不是 0 ,原因是 10^-16 -> 0,计算机浮点型计算的精度就是这样的。
Out[81]: 6.66133814775094e-16
In [84]: print("{d:10.7f}".format(d=np.linalg.det(matrix)))
# 将精度控制一下就行
0.0000000
1.14 获取对角元素
In [86]: matrix
Out[86]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [87]: matrix.diagonal()
Out[87]: array([1, 5, 9])
1.15 计算矩阵的本征值
H ^ Ψ = E Ψ 。 ( Ψ 是 需 要 求 的 矩 阵 , E 是 本 征 值 , 有 很 多 个 — — ( E 1 , E 2 , E 3 , ⋯ , E n ) ) boldsymbol{hat{H}} boldsymbol{Psi} = E boldsymbol{Psi} 。( boldsymbol{Psi} 是需要求的矩阵, E 是本征值,有很多个——(E_1, E_2, E_3, cdots, E_n)) H^Ψ=EΨ。(Ψ是需要求的矩阵,E是本征值,有很多个——(E1,E2,E3,⋯,En))
In [88]: matrix
Out[88]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [89]: eigenvalues, eigenvectors = np.linalg.eig(matrix)
In [90]: eigenvalues
# 第三个值为 -1e-15 -> 0,其实就是 0.
Out[90]: array([ 1.61168440e+01, -1.11684397e+00, -1.30367773e-15])
In [91]: eigenvectors
Out[91]:
array([[-0.23197069, -0.78583024,
0.40824829],
[-0.52532209, -0.08675134, -0.81649658],
[-0.8186735 ,
0.61232756,
0.40824829]])
1.16 计算矩阵的迹(对角元素之和,也是本征值之和)
# 先看 1.15
In [88]: matrix
Out[88]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [89]: eigenvalues, eigenvectors = np.linalg.eig(matrix)
In [90]: eigenvalues
Out[90]: array([ 1.61168440e+01, -1.11684397e+00, -1.30367773e-15])
In [91]: eigenvectors
Out[91]:
array([[-0.23197069, -0.78583024,
0.40824829],
[-0.52532209, -0.08675134, -0.81649658],
[-0.8186735 ,
0.61232756,
0.40824829]])
#####
矩阵的迹
In [92]: matrix.trace()
Out[92]: 15
# 本征值之和
In [93]: np.sum(eigenvalues)
Out[93]: 14.999999999999998
# 14.999999999999998 -> 15
# 本征值之和 == 矩阵的迹,在计算机里,需要考虑精度范围
1.17 计算向量的内积
主要讲的一维向量的内积是,多维的以后用上再写
r e s u l t = a ⃗ ⋅ b ⃗ = ∑ i = 1 n a i b i result = vec{a} cdot vec{b} = sum_{i=1}^{n}a_i b_i result=a⋅b=i=1∑naibi
In [99]: a
Out[99]: array([1, 2, 3, 4, 5, 6])
In [100]: b
Out[100]: array([ 6,
7,
8,
9, 10, 11])
In [101]: np.dot(a,b)
Out[101]: 196
1.18 矩阵相乘
(m, n) × (n,l)= (m, l),就是线性代数中的矩阵相乘
In [105]: a
# 3 × 2
Out[105]:
array([[1, 3],
[4, 3],
[5, 6]])
In [106]: b
# 2 × 3
Out[106]:
array([[9, 8, 7],
[1, 2, 4]])
In [107]: np.dot(a,b) # 结果为 3 × 3
Out[107]:
array([[12, 14, 19],
[39, 38, 40],
[51, 52, 59]])
1.19 矩阵求逆
# 前提:满秩矩阵————即矩阵对应行列式不为 0
In [112]: n
Out[112]:
array([[1, 2],
[3, 4]])
In [113]: np.linalg.inv(n)
Out[113]:
array([[-2. ,
1. ],
[ 1.5, -0.5]])
#
求其行列式的值
In [114]: np.linalg.det(n)
Out[114]: -2.0000000000000004
## 如果行列式为 0 ,求逆会报 Singular matrix 错误
In [119]: m
Out[119]:
array([[12, 14, 19],
[39, 38, 40],
[51, 52, 59]])
In [120]: np.linalg.det(m)
Out[120]: 0.0
In [121]: np.linalg.inv(m)
---------------------------------------------------------------------------
LinAlgError
Traceback (most recent call last)
<ipython-input-121-83af191aaacb> in <module>
----> 1 np.linalg.inv(m)
<__array_function__ internals> in inv(*args, **kwargs)
C:ProgramDataAnaconda3libsite-packagesnumpylinalglinalg.py in inv(a)
545
signature = 'D->D' if isComplexType(t) else 'd->d'
546
extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 547
ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
548
return wrap(ainv.astype(result_t, copy=False))
549
C:ProgramDataAnaconda3libsite-packagesnumpylinalglinalg.py in _raise_linalgerror_singular(err, flag)
95
96 def _raise_linalgerror_singular(err, flag):
---> 97
raise LinAlgError("Singular matrix")
98
99 def _raise_linalgerror_nonposdef(err, flag):
LinAlgError: Singular matrix
1.19 numpy 产生随机数据行列式
可以用于产生随机数据进行学习
具体看官网:
https://docs.scipy.org/doc/numpy-1.15.0/reference/routines.random.html
# Set seed
In [122]: np.random.seed(0)
# Generate three random floats between 0.0 and 1.0
In [123]: np.random.random(3)
Out[123]: array([0.5488135 , 0.71518937, 0.60276338])
#
In [124]: np.random.normal(0.0, 1.0, 3)
Out[124]: array([-2.2683282 ,
1.33354538, -0.84272405])
1.20 向量的点乘和叉乘
# 空间中三个不共线的向量,可以构成一个平行六面体
# 它的体积可以由: V = a1 点乘 (a2 叉乘 a3)
a1 = np.array([4.09278, 0, 0])
a2 = np.array([-2.04639, 3.54445365, 0])
a3 = np.array([0, 0, 20])
# 求分母,也就是体积
V = np.dot(a1, np.cross(a2, a3))
print(V)
# 结果: 290.13338019294
最后
以上就是傻傻缘分为你收集整理的numpy 基础——Vectors, Matrices and Arrays学习目标:学习内容:学习时间:学习产出:的全部内容,希望文章能够帮你解决numpy 基础——Vectors, Matrices and Arrays学习目标:学习内容:学习时间:学习产出:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复