内容提要
- numpy基础(接笔记(1))
- matplotlib
- 小综合
矩阵运算
1 2 3 4 5 6 7
| import numpy as np n = 10 L = np.arange(n) np.array([2*e for e in L])
L*2
|
+
, -
, *
, /
, //
, %
np.abs
, np.sin
, np.cos
, np.tan
, np.exp
, np.log
, np.power
(n**M
)
1 2 3 4 5 6 7 8 9 10 11 12
| M1 = np.arange(start=1, stop=10).reshape([3,3]) M2 = np.arange(6).reshape([3,2]) M1.dot(M2)
|
1 2 3 4 5 6 7
| M2 = np.arange(6).reshape([3,2]) M2.T
|
1 2 3 4
| A1 = np.arange(2) np.tile(A1, (2,1))
|
1 2 3 4 5
| M1 = np.arange(start=1, stop=10).reshape([3,3]) np.linalg.inv(M1)
|
1 2 3 4 5 6 7
| M1 = np.arange(start=0, stop=10).reshape([2,5]) np.linalg.pinv(M1) array([[-3.20000000e-01, 1.20000000e-01], [-1.80000000e-01, 8.00000000e-02], [-4.00000000e-02, 4.00000000e-02], [ 1.00000000e-01, -1.38777878e-17], [ 2.40000000e-01, -4.00000000e-02]])
|
聚合操作
1 2 3
| big_array = np.random.rand(1000000) %timeit sum(big_array) %timeit np.sum(big_array)
|
1 2 3 4 5 6 7
| M = np.arange(6).reshape([2,3])
np.sum(M, axis=0)
np.sum(M, axis=1)
|
1 2 3
| M = np.arange(6).reshape([2,3]) np.prod(M+1)
|
mean
, median
1 2 3 4 5 6 7 8
| big_array = np.random.rand(10000000) for precent in [0, 25, 50, 75, 100]: print(np.percentile(big_array, q=precent))
|
np.random.shuffle
索引
argmin
, argmax
1 2 3 4 5 6 7
| arr = np.arange(10) np.random.shuffle(arr)
np.sort(arr) np.argsort(arr)
|
- 根据指定坐标访问矩阵(例:访问第0,2行的第1个元素)
1 2 3 4 5 6
| MM = np.arange(12).reshape([3,4]) MM[[0,2], [1]]
|
1 2 3 4
| MM[:MM.shape[0], [1]]
|
- 根据布尔值访问矩阵(例:访问第0,2行的第1个元素)
1 2 3 4 5 6 7 8
| MM = np.arange(12).reshape([3,4]) row = [True, False, True] col = [False, True, False, False] MM[row, col]
|
矩阵大小判断
比较运算符都是支持的,返回的是布尔矩阵
与和或运算 all
, any
统计非0元素 count_nonzero
多个逻辑相叠加
1 2 3 4
| arr = np.arange(12) np.sum((arr > 5) & ~(arr % 2 == 0))
|
numpy综合小实战
取出矩阵中的一些行,满足每一行最后一列的元素值能被3整除
1 2 3 4 5 6 7 8 9 10 11
| XX = np.arange(16).reshape((4,4))
XX[(XX[:XX.shape[0], [-1]] % 3 == 0)[:, 0], :XX.shape[1]]
XX[XX[:, 3] % 3 == 0, :]
|
注:表格数据处理类库 Pandas
绘制折线图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import matplotlib.pyplot as plt import numpy as np X = np.linspace(0, 10, 100) Y = np.sin(X) Y2 = np.cos(X) plt.plot(X, Y, color="red", linestyle="--", label="sin(x)") plt.plot(X, Y2, linestyle="-.", label="cos(x)") plt.xlim(-5, 15) plt.ylim(0, 1.5) plt.xlabel("X axis") plt.ylabel("Y value")
plt.legend() plt.title("Welcome to ML world") plt.show()
|
绘制散点图
1 2 3 4 5 6 7 8
| import matplotlib.pyplot as plt import numpy as np
xx = np.random.normal(0, 10, 100000) yy = np.random.normal(0, 10, 100000)
plt.scatter(xx, yy, alpha=0.5) plt.show()
|
对鸢尾花的数据进行绘制
1 2 3 4 5 6 7 8
| import matplotlib.pyplot as plt from sklearn import datasets iris = datasets.load_iris() X = iris.data[:, :2] y=iris.target plt.scatter(X[y==0, 0], X[y==0, 1], color="red", marker="o") plt.scatter(X[y==1, 0], X[y==1, 1], color="green", marker="+") plt.scatter(X[y==2, 0], X[y==2, 1], color="black", marker="*")
|