第1章 Python入门
1.1 NumPy
np.array() 接收Python列表作为参数,生成NumPy数组。
>>> import numpy as np
>>> x = np.array([1.0, 2.0, 3.0])
>>> x
array([1., 2., 3.])
>>> type(x)
<class 'numpy.ndarray'>
1.1.1 广播
>>> x = np.array([1.0, 2.0 ,3.0])
>>> x / 2.0
array([0.5, 1. , 1.5])
1.1.2 矩阵
>>> x = np.array([[1, 2], [3, 4]])
>>> x.shape
(2, 2)
>>> x.dtype
dtype('int64')
shape 显示矩阵的形状
dtype 显示矩阵的元素类型
>>> A = np.array([[1, 2], [3, 4]])
>>> B = np.array([10, 20])
>>> A * B
array([[10, 40],
[30, 80]])
B是一维数组,扩展成了二维数组,和A的形状一致。
1.1.3 访问元素
下标访问
>>> X = np.array([[51, 55], [14, 19], [0, 4]])
>>> X
array([[51, 55],
[14, 19],
[ 0, 4]])
>>> X[0]
array([51, 55])
>>> X[0][1]
55
for方式访问
>>> for row in X:
... print(row)
...
[51 55]
[14 19]
[0 4]
1.1.4 拍扁成一维数组
>>> X = X.flatten()
>>> X
array([51, 55, 14, 19, 0, 4])
1.1.5 数组访问元素
>>> X[np.array([0, 2, 4])]
array([51, 14, 0])
抽出大于15的元素
>>> X > 15
array([ True, True, False, True, False, False])
>>> X[X > 15]
array([51, 55, 19])
1.2 Matplotlib
使用matplotlib.pyplot
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(0, 6, 0.1) # 以0.1为单位,生成0到6的数据
>>> y = np.sin(x)
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x11be611f0>]
>>> plt.show()
pyplot的丰富功能
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x = np.arange(0, 6, 0.1)
>>> y1 = np.sin(x)
>>> y2 = np.cos(x)
>>> plt.plot(x, y1, label = "sin")
[<matplotlib.lines.Line2D object at 0x11c498790>]
>>> plt.plot(x, y2, linestyle = "--", label = "cos")
[<matplotlib.lines.Line2D object at 0x11c498d90>]
>>> plt.xlabel("x")
Text(0.5, 0, 'x')
>>> plt.ylabel("y"
Text(0, 0.5, 'y')
>>> plt.title('sin & cos')
Text(0.5, 1.0, 'sin & cos')
>>> plt.legend()
<matplotlib.legend.Legend object at 0x11c4853d0>
>>> plt.show()
使用matplotlib.image的imread
>>> import matplotlib.pyplot as plt
>>> from matplotlib.image import imread
>>> img = imread('./Desktop/auto_man.jpeg')
>>> plt.imshow(img)
<matplotlib.image.AxesImage object at 0x11b35dee0>
>>> plt.show()
第2章 感知机
2.1 感知机是什么
感知机接收多个输入信号,输出一个信号。