复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114import numpy as np import pandas as pd import matplotlib.pyplot as plt class BpNeuralNetwork: def __init__(self,x,label,input_neuron_num,output_neuron_num,hidden_neuron_num,learning_rate,iteration): """ x:input data y:input label d:number of input_neuron_num l:number of output_neuron_num q:number of hidden_neuron_num theta_j:output layer threshold r_h:hidden layer threshold v_ih:input layer and hidden layer weight w_hj:hidden layer and output layer weight """ self.x = x self.label = label self.d = input_neuron_num self.l = output_neuron_num self.q = hidden_neuron_num self.theta_j = np.random.random(self.l) self.r_h = np.random.random(self.q) self.v_ih = np.random.rand(self.q,self.d) self.w_hj = np.random.rand(self.l,self.q) self.learning_rate = learning_rate self.maxIter = iteration def activate_function(self,x): sigmoid = 1 / (1 + np.exp(-x)) return sigmoid def output_function(self,x): alpha_h = np.sum(np.array(self.v_ih) * np.array(x,dtype=float)) self.b_h = self.activate_function(np.array(alpha_h - self.r_h,dtype=float)) # hidden layer neuron output beta_j = np.sum(self.w_hj * self.b_h) #ouput latyer neuron input predict_y = self.activate_function(np.array(beta_j - self.theta_j,dtype=float)) return predict_y def update_param(self,x,predict_y): x = x.astype(float) predict_y = self.output_function(x) g_j = predict_y * (1 - predict_y) * (self.y - predict_y) g_j = np.array(g_j, dtype=float) self.w_hj += self.learning_rate * g_j * self.b_h self.theta_j += - self.learning_rate * g_j e_h = self.b_h * (1 - self.b_h) * np.sum(self.w_hj * g_j) e_h = np.array(e_h,dtype=float) self.v_ih += self.learning_rate * np.dot(e_h.reshape(self.q,1),x.reshape(1,self.d)) self.r_h += - self.learning_rate * e_h def objective_function(self,predict_y): Ek = np.sum((predict_y - self.y) ** 2) / 2 return Ek def train_process(self): E = [] for n in range(self.maxIter): Ek = [] for i in range(self.x.shape[0]): self.y = self.label[i] predict_y = self.output_function(self.x[i]) Ek.append(self.objective_function(predict_y)) self.update_param(self.x[i],predict_y) E.append(sum(Ek) / self.x.shape[0]) # if n % 10 == 0: # print('{}th iteration, cost is {}'.format(n,E[n])) def predict(self,x): alpha_h = np.sum(np.array(self.v_ih) * np.array(x,dtype=float)) self.b_h = self.activate_function(np.array(alpha_h - self.r_h,dtype=float)) # hidden layer neuron output beta_j = np.sum(self.w_hj * self.b_h) #ouput latyer neuron input predict_y = self.activate_function(np.array(beta_j - self.theta_j,dtype=float)) return predict_y if __name__ == "__main__": data = pd.read_excel("F:/pythonpj/西瓜3.0.xlsx").values x =data[:,7:-1] y = data[:,-1] bp = BpNeuralNetwork(x,y,x.shape[1],1,10,0.1,5000) bp.train_process() for i in range(17): print(bp.predict(x[i]))
代码里面的公式标识按照书里面的来的,这里只用了密度和含糖率
结果:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18[0.79391396] [0.79249972] [0.7680025] [0.79187919] [0.71656208] [0.78923283] [0.34058789] [0.77318892] [0.0010799] [0.79373239] [0.08965418] [0.21713429] [0.03211429] [0.29668821] [0.79392006] [0.00090568] [0.00104169]
结果不太好,后续会改进,基本逻辑实现是这样的
前8个正例有1个预测成反例
后9个反例有2个预测成正例
精度:14/17
错误率:3/17
查准率:7/9
查全率:7/8
最后
以上就是激情小伙最近收集整理的关于机器学习第五章 5.5 标准BP PYTHON的全部内容,更多相关机器学习第五章内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复