多层感知器MLP

多层感知器MLP

建立训练模型

#建立一个Sequential顺序模型
from keras.models import Sequential
model = Sequential()

#通过.add()叠加各层网络
from keras.layers import Dense
model.add(Dense(units=3, activation=‘sigmoid’, input_dim=3))
model.add(Dense(units=1, activation=‘sigmoid’))

#查看模型结构
model.summary()

#通过.compile()配置模型求解过程参数
model.compile(loss=‘categorical_crossentropy’,
optimizer=‘sgd’,
metrics=[‘accuracy’])

#训练模型
model.fit(x_train, y_train, epochs=5)

应用一

#loada the data
import pandas as pd
import numpy as np
data = pd.read_csv(‘data.csv’)
data.head()

#define the X and y
X = data.drop([‘y’],axis=1)
y = data.loc[:,‘y’]
X.head()

#visualize the data
from matplotlib import pyplot as plt
fig1 = plt.figure(figsize=(5,5))
passed=plt.scatter(X.loc[:,‘x1’][y1],X.loc[:,‘x2’][y1])
failed=plt.scatter(X.loc[:,‘x1’][y0],X.loc[:,‘x2’][y0])
plt.legend((passed,failed),(‘passed’,‘failed’))
plt.xlabel(‘x1’)
plt.ylabel(‘x2’)
plt.title(‘raw data’)
plt.show()

#split the data
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.33,random_state=10)
print(X_train.shape,X_test.shape,X.shape)

#set up the model
from keras.models import Sequential
from keras.layers import Dense, Activation

mlp = Sequential()
mlp.add(Dense(units=20, input_dim=2, activation=‘sigmoid’))
mlp.add(Dense(units=1,activation=‘sigmoid’))
mlp.summary()

#compile the model
mlp.compile(optimizer=‘adam’,loss=‘binary_crossentropy’)

#train the model
mlp.fit(X_train,y_train,epochs=3000)

#make prediction and calculate the accuracy
y_train_predict = mlp.predict_classes(X_train)
from sklearn.metrics import accuracy_score
accuracy_train = accuracy_score(y_train,y_train_predict)
print(accuracy_train)

#make prediction based on the test data
y_test_predict = mlp.predict_classes(X_test)
accuracy_test = accuracy_score(y_test,y_test_predict)
print(accuracy_test)

print(y_train_predict[0:10])
#y_train_predict_form = pd.Series(i[0] for i in y_train_predict)

#print(y_train_predict_form)

#generate new data for plot
xx, yy = np.meshgrid(np.arange(0,1,0.01),np.arange(0,1,0.01))
x_range = np.c_[xx.ravel(),yy.ravel()]
y_range_predict = mlp.predict_classes(x_range)
print(type(y_range_predict))

#format the output
y_range_predict_form = pd.Series(i[0] for i in y_range_predict)

print(y_range_predict_form)

fig2 = plt.figure(figsize=(5,5))
passed_predict=plt.scatter(x_range[:,0][y_range_predict_form1],x_range[:,1][y_range_predict_form1])
failed_predict=plt.scatter(x_range[:,0][y_range_predict_form0],x_range[:,1][y_range_predict_form0])

passed=plt.scatter(X.loc[:,‘x1’][y1],X.loc[:,‘x2’][y1])
failed=plt.scatter(X.loc[:,‘x1’][y0],X.loc[:,‘x2’][y0])
plt.legend((passed,failed,passed_predict,failed_predict),(‘passed’,‘failed’,‘passed_predict’,‘failed_predict’))
plt.xlabel(‘x1’)
plt.ylabel(‘x2’)
plt.title(‘prediction result’)
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值