MLP 神经网络算法函数位于 neural_network 神经网络模块,函数名是 MLPClassifier,接口是
MLPClassifier(hidden_layer_size = (100,),activation = 'relu',solver = 'adam',alpha = 0.0001,batch_size = 'auto',learning_rate = 'constant',learning_rate_init = 0.001,power_t = 0.5,max_iter = 200,shuffle = True,random_state = None,tol = 0.0001,verbose = False,warm_start = False,momentum = 0.9,nesterovs_momentum = True,early_stopping = False,validation_fraction = 0.1,beta_1 = 0.9,beta_2 = 0.999,epsilon = 1e-08)
建模、预测
#2
print('\n2# 建模')
mx =zai.mx_MLP(x_train.values,y_train.values)
#3
print('\n3# 预测')
y_pred = mx.predict(x_test.values)
df9['y_predsr']=y_pred
df9['y_test'],df9['y_pred']=y_test,y_pred
df9['y_pred']=round(df9['y_predsr']).astype(int)
mx_MLP() 函数
def mx_MLP(train_x,train_y):
mx = MLPClassifier()
mx.fit(train_x,train_y)
return mx
保存数据结果并显示信息
#4
df9.to_csv('tmp/iris_9.csv',index=False)
print('\n4# df9')
print(df9.tail())
输出
4# df9
x1 x2 x3 x4 y_predsr y_test y_pred
33 6.4 2.8 5.6 2.1 1 1 1
34 5.8 2.8 5.1 2.4 1 1 1
35 5.3 3.7 1.5 0.2 2 2 2
36 5.5 2.3 4.0 1.3 3 3 3
37 5.2 3.4 1.4 0.2 2 2 2
检验测试结果
#5
dacc=zai.ai_acc_xed(df9,1,False)
print('\n5# mx:mx_sum,kok:{0:.2f}%'.format(dacc))
输出
5# mx:mx_sum,kok:94.74%
不过,MLP 神经网络算法每次运行的结果是不同的
再运行一次,结果
4# df9
x1 x2 x3 x4 y_predsr y_test y_pred
33 6.4 2.8 5.6 2.1 1 1 1
34 5.8 2.8 5.1 2.4 1 1 1
35 5.3 3.7 1.5 0.2 2 2 2
36 5.5 2.3 4.0 1.3 3 3 3
37 5.2 3.4 1.4 0.2 2 2 2
5# mx:mx_sum,kok:97.37%
MLP_reg 神经网络算法函数位于 neural_network 神经网络模块,函数名是 MLPRegressor,接口是
MLPRegressor(hidden_layer_size = (100,),activation = 'relu',solver = 'adam',alpha = 0.0001,batch_size = 'auto',learning_rate = 'constant',learning_rate_init = 0.001,power_t = 0.5,max_iter = 200,shuffle = True,random_state = None,tol = 0.0001,verbose = False,warm_start = False,momentum = 0.9,nesterovs_momentum = True,early_stopping = False,validation_fraction = 0.1,beta_1 = 0.9,beta_2 = 0.999,epsilon = 1e-08)
建模、预测
#2
print('\n2# 建模')
mx =zai.mx_MLP_reg(x_train.values,y_train.values)
#3
print('\n3# 预测')
y_pred = mx.predict(x_test.values)
df9['y_predsr']=y_pred
df9['y_test'],df9['y_pred']=y_test,y_pred
df9['y_pred']=round(df9['y_predsr']).astype(int)
mx_MLP_reg() 函数
def mx_MLP_reg(train_x,train_y):
mx = MLPRegressor()
mx.fit(train_x,train_y)
return mx
保存数据结果并显示信息
#4
df9.to_csv('tmp/iris_9.csv',index=False)
print('\n4# df9')
print(df9.tail())
输出
4# df9
x1 x2 x3 x4 y_predsr y_test y_pred
33 6.4 2.8 5.6 2.1 2.571980 1 3
34 5.8 2.8 5.1 2.4 2.323973 1 2
35 5.3 3.7 1.5 0.2 1.488991 2 1
36 5.5 2.3 4.0 1.3 2.100562 3 2
37 5.2 3.4 1.4 0.2 1.469982 2 1
检验测试结果
#5
dacc=zai.ai_acc_xed(df9,1,False)
print('\n5# mx:mx_sum,kok:{0:.2f}%'.format(dacc))
输出
5# mx:mx_sum,kok:44.74%
不过,MLP_reg 神经网络算法每次运行的结果是不同的
再运行一次,结果
4# df9
x1 x2 x3 x4 y_predsr y_test y_pred
33 6.4 2.8 5.6 2.1 2.373282 1 2
34 5.8 2.8 5.1 2.4 2.322128 1 2
35 5.3 3.7 1.5 0.2 1.929890 2 2
36 5.5 2.3 4.0 1.3 1.909459 3 2
37 5.2 3.4 1.4 0.2 1.855990 2 2
5# mx:mx_sum,kok:34.21%
Python机器学习入门源代码和数据集 请点这里