import pandas as pd
import numpy as np
data = pd.read_csv("MLP_test_data.csv")
data.head()
数据自己用excel生成的…
再处理一下输出:
print(type(data),type(data.loc[:,'x']))
data_x = []
data_y = []
data_lable = []
for index, row in data.iterrows():
# print(type(row),row['x'],row['y'])
data_x.append(row['x'])
data_y.append(row['y'])
if(row['x'] < 5 and row['y'] >5):
data_lable.append(1)
elif(row['x'] > 5 and row['y'] <5):
data_lable.append(1)
elif(row['x'] <5 and row['y'] <5):
data_lable.append(0)
elif(row['x'] >5 and row['y'] >5):
data_lable.append(0)
else:
data_lable.append(2)
pass
# res = pd.Series(data,index=["d","c","b","a"])
print(data_x)
print(data_lable)
data_new = {
'x1':data_x,
'x2':data_y,
'y':data_lable}
df = pd.DataFrame(data_new)
df.head()
X = df.drop(['y'],axis=1)
y = df.loc[:,'y']
y.head()
%matplotlib inline
from matplotlib import pyplot as plt
fig1 = plt.figure(figsize=(5,5))
passed = plt.scatter(X.loc[:,'x1'][y==1],X.loc[:,'x2'][y==1])
failed = plt.scatter(X.loc[:,'x1'][y==0],X.loc[:,'x2'][y==0])
plt.legend((passed,failed),("passed","failed"))
plt.title("raw data")
plt.xlabel('X1')
plt.ylabel('X2')
plt.show()
#分离样本
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)
(236, 2) (117, 2) (353, 2)
#构建模型
from keras.models import Sequential
from keras.layers import Dense,Activation
mlp = Sequential()
mlp.add(Dense(units=20,input_dim=2,activation="sigmoid"))#20个神经元 激活函数digmoid
mlp.add(Dense(units=1,activation="sigmoid"))
mlp.summary()
#配置模型参数
#优化方法 损失函数
mlp.compile(optimizer="adam",loss="binary_crossentropy")#二分类的
#模型训练
mlp.fit(X_train,y_train,epochs=3000)#迭代次数
迭代3000次
#预测 训练集 准确率
y_train_predict = mlp.predict_classes(X_train)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_train,y_train_predict)
print(accuracy)
0.9449152542372882
#预测 测试数据集
y_test_predict = mlp.predict_classes(X_test)
accuracy = accuracy_score(y_test,y_test_predict)
print(accuracy)
0.9145299145299145
y_train_predict_form = pd.Series(i[0] for i in y_train_predict)
print(y_train_predict_form)
#画图的点集
xx , yy = np.meshgrid(np.arange(0,10,0.1),np.arange(0,10,0.1))
x_range = np.c_[xx.ravel(),yy.ravel()]
y_range_predict = mlp.predict_classes(x_range)
print(type(y_range_predict))
#转换格式
y_range_predict_form = pd.Series(i[0] for i in y_range_predict )
%matplotlib inline
fig2 = plt.figure(figsize=(5,5))
passed_predict = plt.scatter(x_range[:,0][y_range_predict_form == 1],x_range[:,1][y_range_predict_form == 1])
failed_predict = plt.scatter(x_range[:,0][y_range_predict_form == 0],x_range[:,1][y_range_predict_form == 0])
passed = plt.scatter(X.loc[:,'x1'][y==1],X.loc[:,'x2'][y==1])
failed = plt.scatter(X.loc[:,'x1'][y==0],X.loc[:,'x2'][y==0])
plt.legend((passed,failed,passed_predict,failed_predict),("passed","failed","passed_predict","failed_predict"))
plt.title("prediction data")
plt.xlabel('X1')
plt.ylabel('X2')
plt.show()