导入numpy模块
创建数组序列的方法
1 2 3 4 5 6 7 8 9 10
| import numpy as np
a0 = np.array([[1,100],[20,3]]) print(a0) a=np.arange(start=1,stop=10,step=2,dtype = float) print(a) b=np.linspace(1,13,num=20,endpoint = True) print(b) c=np.logspace(1,10,num = 10,endpoint = True,base = 10) print(c)
|
[[ 1 100]
[ 20 3]]
[1. 3. 5. 7. 9.]
[ 1. 1.63157895 2.26315789 2.89473684 3.52631579 4.15789474
4.78947368 5.42105263 6.05263158 6.68421053 7.31578947 7.94736842
8.57894737 9.21052632 9.84210526 10.47368421 11.10526316 11.73684211
12.36842105 13. ]
[1.e+01 1.e+02 1.e+03 1.e+04 1.e+05 1.e+06 1.e+07 1.e+08 1.e+09 1.e+10]
arrange(strat,stop,step,dtype) 用于创建一个ndarray数组
linspace(strat,stop,num,endpoint=True) 用于创建一个有num个数的在[strat,stop]范围内的等间距序列,endpoint=True时,结果的最后一个点会被包括进去
lospace(start,stop,num,endpoint=True,base=10) $[base^{start},base^{stop}]范围内找到num个形成等比数组$
对数组大小修改,修改数组元素与切片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import numpy as np a = np.arange(16).reshape(4,4) print(a) b = a[2:4,2:4] print(b) b[0][0] = 100 print(b) print(a)
a1 = a c = a.copy() c[0,0] = 1 print(c) print(a)
a1[0,0] = 1 print(a) print(a1)
|
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[10 11]
[14 15]]
[[100 11]
[ 14 15]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 100 11]
[ 12 13 14 15]]
[[ 1 1 2 3]
[ 4 5 6 7]
[ 8 9 100 11]
[ 12 13 14 15]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 100 11]
[ 12 13 14 15]]
[[ 1 1 2 3]
[ 4 5 6 7]
[ 8 9 100 11]
[ 12 13 14 15]]
[[ 1 1 2 3]
[ 4 5 6 7]
[ 8 9 100 11]
[ 12 13 14 15]]
使用reshape方法可以重新修改数组的大小
数组可以切片
但是切片之后得到的对象还是原来数组对应的空间,如果不想对原数据进行操作的话,也要进行一个copy
创建特殊的数组的方法
1 2 3 4 5 6 7 8 9 10 11 12 13
| import numpy as np a = np.ones((4,3),dtype = float) b = np.zeros((4,3),dtype = float) c = np.empty(3) d = np.eye(3) e = np.eye(3,k = 1) f = np.zeros_like(a) print(f"a = \n{a}") print(f"b = \n{b}") print(f"c = \n{c}") print(f"d = \n{d}") print(f"e = \n{e}") print(f"f = \n{f}")
|
a =
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
b =
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
c =
[1. 1. 1.]
d =
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
e =
[[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
f =
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
矩阵合并与分割
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import numpy as np a = np.arange(16).reshape(4,4) print(f"a = \n{a}") b1 = 5*np.random.random((2,4)) b2 = np.floor(b1) print(f"b1 =\n{b1}") print(f"b2 =\n{b2}")
c1 = 6*np.random.random((4,2)) c2 = np.ceil(c1) print(f"c1 = \n{c1}") print(f"c2 = \n{c2}")
d1 = np.vstack([a,b1]) print(f"d1 = \n {d1}")
d2 = np.hstack([a,c1]) print(f"d2 = \n{d2}")
|
a =
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
b1 =
[[2.14777241 2.15394632 1.58349984 0.16490097]
[1.46675274 0.43018034 3.77262286 3.63291719]]
b2 =
[[2. 2. 1. 0.]
[1. 0. 3. 3.]]
c1 =
[[2.37532468 0.04491147]
[0.44639029 2.00104121]
[3.4275526 0.80855631]
[0.75634035 3.02794756]]
c2 =
[[3. 1.]
[1. 3.]
[4. 1.]
[1. 4.]]
d1 =
[[ 0. 1. 2. 3. ]
[ 4. 5. 6. 7. ]
[ 8. 9. 10. 11. ]
[12. 13. 14. 15. ]
[ 2.14777241 2.15394632 1.58349984 0.16490097]
[ 1.46675274 0.43018034 3.77262286 3.63291719]]
d2 =
[[ 0. 1. 2. 3. 2.37532468 0.04491147]
[ 4. 5. 6. 7. 0.44639029 2.00104121]
[ 8. 9. 10. 11. 3.4275526 0.80855631]
[12. 13. 14. 15. 0.75634035 3.02794756]]
1 2 3 4 5 6 7 8 9 10 11 12
| import numpy as np a=np.arange(16).reshape(4,4) print(f"a = \n{a}") b=np.vsplit(a,2) print(f"b = \n{b}") print(f"----b[0] = ----\n{b[0]}") print(f"-----b[1] = ----\n{b[1]}")
c = np.hsplit(a,2) print(f"c = \n{c}") print(f"-----c[0] = ----\n {c[0]}") print(f"-----c[1] = ----\n {c[1]}")
|
a =
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
b =
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]
----b[0] = ----
[[0 1 2 3]
[4 5 6 7]]
-----b[1] = ----
[[ 8 9 10 11]
[12 13 14 15]]
c =
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]
-----c[0] = ----
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
-----c[1] = ----
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
一个矩阵的简单运算(元素求和)
1 2 3 4 5 6 7 8 9 10
| inport numpy as np a = 5*np.random.random((2,4)) a0 = np.floor(a) print(f"a0 = \n{a0}") a0_sum = a0.sum() print(f"a0_sum = {a0_sum}")
c2 = np.sum(a, axis = 0) c3 = np.sum(a,axis = 0,keepdims = True)
|
$$
\overset{axis = 1}{\left[
\begin{matrix}
a_1 & a_2 & a_3 \
a_4 & a_5 & a_6 \
a_7 & a_8 & a_9
\end{matrix}
\right]}
$$
矩阵之间简单运算(加减乘除幂)
基本原理:先广播到同一个维度大小,然后各位置元素对应运算
矩阵之间线性代数运算 numpy.linalg模块
求范数
1 2 3 4 5 6 7 8 9 10 11
| import numpy as np
a = np.array([[0,3,4],[1,6,4]]) print(f"a = \n{a}") b = np.linalg.norm(a,axis=0) b_keepdim = np.linalg.norm(a,axis=0,keepdims = True) c = np.linalg.norm(a,axis=1) print(f"b=\n{b}") print(f"b_keepdim = \n{b_keepdim}") print(f"c = \n{c}") print(f"b(保留四位有效数据,四舍五入) = \n {np.round(b,4)}")
|
a =
[[0 3 4]
[1 6 4]]
b=
[1. 6.70820393 5.65685425]
b_keepdim =
[[1. 6.70820393 5.65685425]]
c =
[5. 7.28010989]
b(保留四位有效数据,四舍五入) =
[1. 6.7082 5.6569]
求线性方程组的唯一解
知识回顾:什么时候具有唯一解,什么时候没有唯一解
$$Ax=b,b\neq 0,解的结构$$
$$取决于 R(A)与R(A|b)的关系 :$$
$1. R(A)=R(A|b)=r<n(未知数个数) -> 有无穷多个解 而且解空间由 n-r个向量张成$
$2. R(A)\neq R(A|b) -> 无解$
$3. R(A)=R(A|b)=r = n -> 只有唯一解 $
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import numpy as np
A = np.array([[3,1],[1,2]]) b = np.array([[9],[8]])
Ab = np.hstack([A,b]) a_rank = np.linalg.matrix_rank(A) Ab_rank = np.linalg.matrix_rank(Ab) print(f"a_rank = {a_rank},Ab_rank = {Ab_rank}")
x1 = np.linalg.solve(A,b) x1_anotherway = np.linalg.inv(A)@b print(x1) print(x1_anotherway)
|
a_rank = 2,Ab_rank = 2
[[2.]
[3.]]
[[2.]
[3.]]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import numpy as np A = np.array([[3,1],[1,2],[1,1]]) b = np.array([[9,8,6]]).T print(f"A = \n{A}") print(f"b = \n{b}") Ab = np.hstack([A,b]) print(f"Ab = \n{Ab}") A_rank = np.linalg.matrix_rank(A) Ab_rank = np.linalg.matrix_rank(Ab) print(f"A_rank = {A_rank}, Ab_rank = {Ab_rank}")
A_pinv = np.linalg.pinv(A) print(f"A_pinv = \n{A_pinv}") x = np.linalg.pinv(A) @ b print(f"x = \n{x}")
|
A =
[[3 1]
[1 2]
[1 1]]
b =
[[9]
[8]
[6]]
Ab =
[[3 1 9]
[1 2 8]
[1 1 6]]
A_rank = 2, Ab_rank = 3
A_pinv =
[[ 0.4 -0.2 0. ]
[-0.23333333 0.53333333 0.16666667]]
x =
[[2. ]
[3.16666667]]
可以看出此时为R(A)<R(A|b) 理论上来说是没有解的嘞,但是可以通过求最小二乘解的形式求出一个近似解
求特征值与特征向量
1 2 3 4 5 6
| import numpy as np a = np.eye(4) b = np.rot90(a) c,d = np.linalg.eig(b) print(c) print(d)
|
[ 1. -1. 1. -1.]
[[ 0.70710678 0.70710678 0. 0. ]
[ 0. 0. 0.70710678 -0.70710678]
[ 0. 0. 0.70710678 0.70710678]
[ 0.70710678 -0.70710678 -0. 0. ]]