1.对投喂数据进行极值获取,构造更多数据做准备
示例
plot_decision_regions(x, y, ppn, resolution=0.02)
def plot_decision_regions(x, y, classifier, resolution=0.02):
marker = ('s', 'x', 'o', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
# y 的种类只有-1 和 1 ,根据相应的种类绘制对应的颜色
cmap = ListedColormap(colors[:len(np.unique(y))])
x1_min = x[:, 0].min() - 1
x1_max = x[:, 0].max()
x2_min = x[:, 1].min() - 1
x2_max = x[:, 1].max()
print("花瓣长度最小值 为 %s, 最大值为 %s" % (x1_min, x1_max))
print("花茎长度最小值 为 %s, 最大值为 %s" % (x2_min, x2_max))
运行结果
花瓣长度最小值 为 3.3, 最大值为 7.0
花茎长度最小值 为 0.0, 最大值为 5.1
2.投喂数据转换为向量,来构造数据,即二维矩阵
# 构造向量,扩展成一个二维矩阵,resolution向量差值
xx1 = np.meshgrid(np.arange(x1_min, x1_max, resolution))
xx2 = np.meshgrid(np.arange(x2_min, x2_max, resolution))
print("向量大小", np.arange(x1_min, x1_max, resolution).shape)
print("向量", np.arange(x1_min, x1_max, resolution))
运行结果
x1向量大小 (185,)
x1向量 [ 3.3 3.32 3.34 3.36 3.38 3.4 3.42 3.44 3.46 3.48 3.5 3.52
3.54 3.56 3.58 3.6 3.62 3.64 3.66 3.68 3.7 3.72 3.74 3.76
3.78 3.8 3.82 3.84 3.86 3.88 3.9 3.92 3.94 3.96 3.98 4.
4.02 4.04 4.06 4.08 4.1 4.12 4.14 4.16 4.18 4.2 4.22 4.24
4.26 4.28 4.3 4.32 4.34 4.36 4.38 4.4 4.42 4.44 4.46 4.48
4.5 4.52 4.54 4.56 4.58 4.6 4.62 4.64 4.66 4.68 4.7 4.72
4.74 4.76 4.78 4.8 4.82 4.84 4.86 4.88 4.9 4.92 4.94 4.96
4.98 5. 5.02 5.04 5.06 5.08 5.1 5.12 5.14 5.16 5.18 5.2
5.22 5.24 5.26 5.28 5.3 5.32 5.34 5.36 5.38 5.4 5.42 5.44
5.46 5.48 5.5 5.52 5.54 5.56 5.58 5.6 5.62 5.64 5.66 5.68
5.7 5.72 5.74 5.76 5.78 5.8 5.82 5.84 5.86 5.88 5.9 5.92
5.94 5.96 5.98 6. 6.02 6.04 6.06 6.08 6.1 6.12 6.14 6.16
6.18 6.2 6.22 6.24 6.26 6.28 6.3 6.32 6.34 6.36 6.38 6.4
6.42 6.44 6.46 6.48 6.5 6.52 6.54 6.56 6.58 6.6 6.62 6.64
6.66 6.68 6.7 6.72 6.74 6.76 6.78 6.8 6.82 6.84 6.86 6.88
6.9 6.92 6.94 6.96 6.98]
x2向量大小 (255,)
x2向量 [ 0. 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2 0.22
0.24 0.26 0.28 0.3 0.32 0.34 0.36 0.38 0.4 0.42 0.44 0.46
0.48 0.5 0.52 0.54 0.56 0.58 0.6 0.62 0.64 0.66 0.68 0.7
0.72 0.74 0.76 0.78 0.8 0.82 0.84 0.86 0.88 0.9 0.92 0.94
0.96 0.98 1. 1.02 1.04 1.06 1.08 1.1 1.12 1.14 1.16 1.18
1.2 1.22 1.24 1.26 1.28 1.3 1.32 1.34 1.36 1.38 1.4 1.42
1.44 1.46 1.48 1.5 1.52 1.54 1.56 1.58 1.6 1.62 1.64 1.66
1.68 1.7 1.72 1.74 1.76 1.78 1.8 1.82 1.84 1.86 1.88 1.9
1.92 1.94 1.96 1.98 2. 2.02 2.04 2.06 2.08 2.1 2.12 2.14
2.16 2.18 2.2 2.22 2.24 2.26 2.28 2.3 2.32 2.34 2.36 2.38
2.4 2.42 2.44 2.46 2.48 2.5 2.52 2.54 2.56 2.58 2.6 2.62
2.64 2.66 2.68 2.7 2.72 2.74 2.76 2.78 2.8 2.82 2.84 2.86
2.88 2.9 2.92 2.94 2.96 2.98 3. 3.02 3.04 3.06 3.08 3.1
3.12 3.14 3.16 3.18 3.2 3.22 3.24 3.26 3.28 3.3 3.32 3.34
3.36 3.38 3.4 3.42 3.44 3.46 3.48 3.5 3.52 3.54 3.56 3.58
3.6 3.62 3.64 3.66 3.68 3.7 3.72 3.74 3.76 3.78 3.8 3.82
3.84 3.86 3.88 3.9 3.92 3.94 3.96 3.98 4. 4.02 4.04 4.06
4.08 4.1 4.12 4.14 4.16 4.18 4.2 4.22 4.24 4.26 4.28 4.3
4.32 4.34 4.36 4.38 4.4 4.42 4.44 4.46 4.48 4.5 4.52 4.54
4.56 4.58 4.6 4.62 4.64 4.66 4.68 4.7 4.72 4.74 4.76 4.78
4.8 4.82 4.84 4.86 4.88 4.9 4.92 4.94 4.96 4.98 5. 5.02
5.04 5.06 5.08]
xx1 二维矩阵大小 (255, 185)
xx1 二维矩阵 [[ 3.3 3.32 3.34 ..., 6.94 6.96 6.98]
[ 3.3 3.32 3.34 ..., 6.94 6.96 6.98]
[ 3.3 3.32 3.34 ..., 6.94 6.96 6.98]
...,
[ 3.3 3.32 3.34 ..., 6.94 6.96 6.98]
[ 3.3 3.32 3.34 ..., 6.94 6.96 6.98]
[ 3.3 3.32 3.34 ..., 6.94 6.96 6.98]]
xx2 二维矩阵大小 (255, 185)
xx2 二维矩阵 [[ 0. 0. 0. ..., 0. 0. 0. ]
[ 0.02 0.02 0.02 ..., 0.02 0.02 0.02]
[ 0.04 0.04 0.04 ..., 0.04 0.04 0.04]
...,
[ 5.04 5.04 5.04 ..., 5.04 5.04 5.04]
[ 5.06 5.06 5.06 ..., 5.06 5.06 5.06]
[ 5.08 5.08 5.08 ..., 5.08 5.08 5.08]]
上述3.3, 3.32, 3,34,其中差值resolution=0.02
3.学习笔记来源
https://www.imooc.com/video/14380
4.Git
https://github.com/sufadi/SimpleNeuronNetworkDemo/blob/master/su/Perceptron.py