Keras之MLP:利用MLP【Input(8)→(12)(relu)→O(sigmoid+二元交叉)】模型实现预测新数据(利用糖尿病数据集的八个特征实现二分类预测

输出结果

Keras之MLP:利用MLP【Input(8)→(12)(relu)→O(sigmoid+二元交叉)】模型实现预测新数据(利用糖尿病数据集的八个特征实现二分类预测


实现代码

 

# load and prepare the dataset

dataset = numpy.loadtxt("data/pima-indians-diabetes.csv", delimiter=",")

X = dataset[:,0:8]

Y = dataset[:,8]

# 1. define the network

model = Sequential()

model.add(Dense(12, input_dim=8, activation='relu'))

model.add(Dense(1, activation='sigmoid'))

# 2. compile the network

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# 3. fit the network

history = model.fit(X, Y, epochs=100, batch_size=10)

# 4. evaluate the network

loss, accuracy = model.evaluate(X, Y)

print("\nLoss: %.2f, Accuracy: %.2f%%" % (loss, accuracy*100))

# 5. make predictions

probabilities = model.predict(X)

predictions = [float(numpy.round(x)) for x in probabilities]  #numpy.round

accuracy = numpy.mean(predictions == Y)

print("Prediction Accuracy: %.2f%%" % (accuracy*100))


上一篇:Keras之DNN:利用DNN算法【Input(8)→12+8(relu)→O(sigmoid)】利用糖尿病数据集训练、评估模型(利用糖尿病数据集中的八个参数特征预测一个0或1结果)


下一篇:Swift教程_零基础学习Swift完整实例(二)_swift基础(简单值、控制流、方法和闭包)