MLP模型解决二分类

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv("datas/datam.csv")
df.head()
image.png
# 定义x 和 y
x = df.drop(["y"], axis=1)
y = df["y"]

# 可视化
%matplotlib inline
plt.figure()
passed = plt.scatter(x["x1"][y==1], x["x2"][y==1])
failed = plt.scatter(x["x1"][y==0], x["x2"][y==0])
plt.legend((passed, failed), ("passed", "failed"))
plt.xlabel("x1")
plt.ylabel("x2")
plt.show()
image.png
#数据分离
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)

from keras.models import Sequential
from keras.layers import Dense, Activation

#建立一个模型
mlp = Sequential()
#添加网络层
mlp.add(Dense(units=20, activation="sigmoid", input_dim=2))
mlp.add(Dense(units=1, activation="sigmoid"))

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

#通过complie配置模型求解过程
mlp.compile(optimizer="adam", loss="binary_crossentropy")   # 优化器   二分类

#模型训练
mlp.fit(x_train, y_train, epochs=3000)      #3000次迭代

#测试数据预测和准确率
y_test_predict = mlp.predict(x_test)   # 预测结果不是0、1需要进行转换
a = np.ones(136)
b = a / 2
c = np.insert(y_test_predict, 0, b, axis=1)
y_test_predict = np.argmax(c, axis=1)
y_test_predict = y_test_predict.reshape(-1, 1)
accuracy_score(y_test, y_test_predict)

#格式化预测数据
y_train_predict_form = pd.Series(i[0] for i in y_train_predict)
y_test_predict_form = pd.Series(i[0] for i in y_test_predict)

# 生成新的画图点
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(x_range)

#生成的数据的预测结果
a = np.ones(10000)
b = a / 2
c = np.insert(y_range_predict, 0, b, axis=1)
y_range_predict = np.argmax(c, axis=1)
y_range_predict = y_range_predict.reshape(-1, 1)
y_range_predict_form = pd.Series(i[0] for i in y_range_predict)
y_range_predict_form

# 可视化边界
%matplotlib inline
plt.figure()
pass_predict = plt.scatter(x_range[:, 0][y_range_predict_form==1], x_range[:, 1][y_range_predict_form==1])
fail_predict = plt.scatter(x_range[:, 0][y_range_predict_form==0], x_range[:, 1][y_range_predict_form==0])
passed = plt.scatter(x["x1"][y==1], x["x2"][y==1])
failed = plt.scatter(x["x1"][y==0], x["x2"][y==0])
plt.legend((passed, failed, pass_predict, fail_predict), ("passed", "failed", "pass_predict", "fail_predict"))
plt.xlabel("x1")
plt.ylabel("x2")
plt.show()
image.png

你可能感兴趣的:(MLP模型解决二分类)