输出结果
实现代码
# coding:utf-8
#Keras之DNN::基于Keras(sigmoid+binary_crossentropy+predict_classes)利用DNN实现二分类——DIY二分类数据集&预测新数据点
# 生成二分类数据集
X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1)
print(X,y)
Xa=[]
Xb=[]
for i in range(0,len(X)):
Xa.append(X[i][0])
Xb.append(X[i][1])
print('a',Xa)
print('b',Xb)
plt.scatter(Xa,Xb,marker='o',c='',edgecolors='g')#edgecolors是控制圆圈的边缘颜色,c是控制圆心的颜色,c=''就是空心
scalar = MinMaxScaler()
scalar.fit(X)
X = scalar.transform(X)
# 定义并拟合最终模型
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(X, y, epochs=500, verbose=0)
# #T1、单个预测,新的未知数据实例
# Xnew = array([[0.89337759, 0.65864154]])
# # 作出预测
# ynew = model.predict_classes(Xnew)
# print(Xnew,ynew)
# plt.scatter(Xnew[0][0],Xnew[0][1],marker='^',c='',edgecolors='b')#edgecolors是控制圆圈的边缘颜色,c是控制圆心的颜色,c=''就是空心
# plt.title('Keras-DNN—Single: Binary classification——Jason Niu')
# plt.show()
#T2、多个预测,新的未知数据实例
Xnew, _ = make_blobs(n_samples=3, centers=2, n_features=2, random_state=1)
Xnew = scalar.transform(Xnew)
print(Xnew)
# 作出预测
ynew = model.predict_classes(Xnew)
# 显示输入和输出
Xnew_x=[]
Xnew_y=[]
for i in range(len(Xnew)):
print("X=%s, Predicted=%s" % (Xnew[i], ynew[i]))
Xnew_x.append(Xnew[i][0])
Xnew_y.append(Xnew[i][1])
plt.scatter(Xnew_x,Xnew_y,marker='.',c='',edgecolors='r')#edgecolors是控制圆圈的边缘颜色,c是控制圆心的颜色,c=''就是空心
plt.title('Keras-DNN—Multiple: Binary classification——Jason Niu')
plt.show()