机器学习算法完整版见fenghaootong-github
皮马印第安人糖尿病
数据集
是对皮马印第安人糖尿病分类
代码
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
Using TensorFlow backend.
数据
# load pima indians dataset
dataset = numpy.loadtxt("../DATA/pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
模型
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
C:\Users\htfeng\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(12, input_dim=8, activation="relu", kernel_initializer="uniform")`
This is separate from the ipykernel package so we can avoid doing imports until
C:\Users\htfeng\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(8, activation="relu", kernel_initializer="uniform")`
after removing the cwd from sys.path.
C:\Users\htfeng\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(1, activation="sigmoid", kernel_initializer="uniform")`
"""
编译模型
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
训练模型
# Fit the model
model.fit(X, Y, nb_epoch=100, batch_size=10)
Epoch 1/100
768/768 [==============================] - 0s 108us/step - loss: 0.4920 - acc: 0.7734
Epoch 2/100
768/768 [==============================] - 0s 102us/step - loss: 0.4862 - acc: 0.7773
Epoch 3/100
10/768 [..............................] - ETA: 0s - loss: 0.4446 - acc: 0.8000
C:\Users\htfeng\Anaconda3\lib\site-packages\keras\models.py:939: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
warnings.warn('The `nb_epoch` argument in `fit` '
768/768 [==============================] - 0s 100us/step - loss: 0.4871 - acc: 0.7682
Epoch 4/100
768/768 [==============================] - 0s 108us/step - loss: 0.4959 - acc: 0.7734
Epoch 5/100
768/768 [==============================] - 0s 94us/step - loss: 0.4904 - acc: 0.7734
Epoch 6/100
768/768 [==============================] - 0s 95us/step - loss: 0.4845 - acc: 0.7708
Epoch 7/100
768/768 [==============================] - 0s 95us/step - loss: 0.4841 - acc: 0.7799
Epoch 8/100
768/768 [==============================] - 0s 94us/step - loss: 0.4834 - acc: 0.7799
Epoch 9/100
768/768 [==============================] - 0s 92us/step - loss: 0.4747 - acc: 0.7839
Epoch 10/100
768/768 [==============================] - 0s 92us/step - loss: 0.4804 - acc: 0.7891
Epoch 11/100
768/768 [==============================] - 0s 93us/step - loss: 0.4763 - acc: 0.7734
Epoch 12/100
768/768 [==============================] - 0s 94us/step - loss: 0.4774 - acc: 0.7747
Epoch 13/100
768/768 [==============================] - 0s 97us/step - loss: 0.4910 - acc: 0.7591
Epoch 14/100
768/768 [==============================] - 0s 94us/step - loss: 0.4827 - acc: 0.7721
Epoch 15/100
768/768 [==============================] - 0s 98us/step - loss: 0.4789 - acc: 0.7904
Epoch 16/100
768/768 [==============================] - 0s 93us/step - loss: 0.4807 - acc: 0.7852
Epoch 17/100
768/768 [==============================] - 0s 96us/step - loss: 0.4715 - acc: 0.7813
Epoch 18/100
768/768 [==============================] - 0s 94us/step - loss: 0.4764 - acc: 0.7747
Epoch 19/100
768/768 [==============================] - 0s 92us/step - loss: 0.4806 - acc: 0.7852
Epoch 20/100
768/768 [==============================] - 0s 93us/step - loss: 0.4729 - acc: 0.7930
Epoch 21/100
768/768 [==============================] - 0s 93us/step - loss: 0.4722 - acc: 0.7708
Epoch 22/100
768/768 [==============================] - 0s 97us/step - loss: 0.4699 - acc: 0.7865
Epoch 23/100
768/768 [==============================] - 0s 94us/step - loss: 0.4959 - acc: 0.7617
Epoch 24/100
768/768 [==============================] - 0s 94us/step - loss: 0.4755 - acc: 0.7852
Epoch 25/100
768/768 [==============================] - 0s 93us/step - loss: 0.4681 - acc: 0.7760
Epoch 26/100
768/768 [==============================] - 0s 93us/step - loss: 0.4814 - acc: 0.7786
Epoch 27/100
768/768 [==============================] - 0s 94us/step - loss: 0.4728 - acc: 0.7891: 0s - loss: 0.4599 - acc: 0.803
Epoch 28/100
768/768 [==============================] - 0s 98us/step - loss: 0.4718 - acc: 0.7878
Epoch 29/100
768/768 [==============================] - 0s 93us/step - loss: 0.4680 - acc: 0.7786
Epoch 30/100
768/768 [==============================] - 0s 92us/step - loss: 0.4791 - acc: 0.7669
Epoch 31/100
768/768 [==============================] - 0s 90us/step - loss: 0.4675 - acc: 0.7878
Epoch 32/100
768/768 [==============================] - 0s 91us/step - loss: 0.4651 - acc: 0.7826
Epoch 33/100
768/768 [==============================] - 0s 92us/step - loss: 0.4703 - acc: 0.7760
Epoch 34/100
768/768 [==============================] - 0s 95us/step - loss: 0.4680 - acc: 0.7708
Epoch 35/100
768/768 [==============================] - 0s 94us/step - loss: 0.4662 - acc: 0.7865
Epoch 36/100
768/768 [==============================] - 0s 95us/step - loss: 0.4691 - acc: 0.7891
Epoch 37/100
768/768 [==============================] - 0s 95us/step - loss: 0.4637 - acc: 0.7930
Epoch 38/100
768/768 [==============================] - 0s 91us/step - loss: 0.4622 - acc: 0.7917
Epoch 39/100
768/768 [==============================] - 0s 96us/step - loss: 0.4684 - acc: 0.7786
Epoch 40/100
768/768 [==============================] - 0s 92us/step - loss: 0.4703 - acc: 0.7799
Epoch 41/100
768/768 [==============================] - 0s 91us/step - loss: 0.4632 - acc: 0.7865
Epoch 42/100
768/768 [==============================] - 0s 91us/step - loss: 0.4665 - acc: 0.7812
Epoch 43/100
768/768 [==============================] - 0s 91us/step - loss: 0.4673 - acc: 0.7917
Epoch 44/100
768/768 [==============================] - 0s 94us/step - loss: 0.4612 - acc: 0.7891
Epoch 45/100
768/768 [==============================] - 0s 92us/step - loss: 0.4576 - acc: 0.8008
Epoch 46/100
768/768 [==============================] - 0s 92us/step - loss: 0.4670 - acc: 0.7734
Epoch 47/100
768/768 [==============================] - 0s 92us/step - loss: 0.4589 - acc: 0.7708
Epoch 48/100
768/768 [==============================] - 0s 91us/step - loss: 0.4692 - acc: 0.7878
Epoch 49/100
768/768 [==============================] - 0s 92us/step - loss: 0.4575 - acc: 0.7747
Epoch 50/100
768/768 [==============================] - 0s 94us/step - loss: 0.4559 - acc: 0.7969
Epoch 51/100
768/768 [==============================] - 0s 94us/step - loss: 0.4635 - acc: 0.7852
Epoch 52/100
768/768 [==============================] - 0s 93us/step - loss: 0.4630 - acc: 0.7930
Epoch 53/100
768/768 [==============================] - 0s 92us/step - loss: 0.4610 - acc: 0.7904
Epoch 54/100
768/768 [==============================] - 0s 94us/step - loss: 0.4643 - acc: 0.7812
Epoch 55/100
768/768 [==============================] - 0s 96us/step - loss: 0.4569 - acc: 0.7865
Epoch 56/100
768/768 [==============================] - 0s 93us/step - loss: 0.4555 - acc: 0.7865: 0s - loss: 0.4327 - acc: 0.807
Epoch 57/100
768/768 [==============================] - 0s 94us/step - loss: 0.4734 - acc: 0.7643
Epoch 58/100
768/768 [==============================] - 0s 96us/step - loss: 0.4617 - acc: 0.7839
Epoch 59/100
768/768 [==============================] - 0s 96us/step - loss: 0.4562 - acc: 0.7852
Epoch 60/100
768/768 [==============================] - 0s 94us/step - loss: 0.4486 - acc: 0.7891
Epoch 61/100
768/768 [==============================] - 0s 98us/step - loss: 0.4519 - acc: 0.7956
Epoch 62/100
768/768 [==============================] - 0s 96us/step - loss: 0.4538 - acc: 0.7891
Epoch 63/100
768/768 [==============================] - 0s 100us/step - loss: 0.4540 - acc: 0.7969
Epoch 64/100
768/768 [==============================] - 0s 94us/step - loss: 0.4529 - acc: 0.7969
Epoch 65/100
768/768 [==============================] - 0s 96us/step - loss: 0.4541 - acc: 0.7891
Epoch 66/100
768/768 [==============================] - 0s 98us/step - loss: 0.4606 - acc: 0.7839
Epoch 67/100
768/768 [==============================] - 0s 93us/step - loss: 0.4547 - acc: 0.7930
Epoch 68/100
768/768 [==============================] - 0s 93us/step - loss: 0.4448 - acc: 0.7969
Epoch 69/100
768/768 [==============================] - 0s 94us/step - loss: 0.4460 - acc: 0.7956
Epoch 70/100
768/768 [==============================] - 0s 94us/step - loss: 0.4469 - acc: 0.7982
Epoch 71/100
768/768 [==============================] - 0s 91us/step - loss: 0.4625 - acc: 0.7891
Epoch 72/100
768/768 [==============================] - 0s 93us/step - loss: 0.4589 - acc: 0.7799
Epoch 73/100
768/768 [==============================] - 0s 92us/step - loss: 0.4570 - acc: 0.7904
Epoch 74/100
768/768 [==============================] - 0s 94us/step - loss: 0.4508 - acc: 0.8060
Epoch 75/100
768/768 [==============================] - 0s 97us/step - loss: 0.4496 - acc: 0.8008
Epoch 76/100
768/768 [==============================] - 0s 102us/step - loss: 0.4633 - acc: 0.7865
Epoch 77/100
768/768 [==============================] - 0s 104us/step - loss: 0.4465 - acc: 0.7826
Epoch 78/100
768/768 [==============================] - 0s 96us/step - loss: 0.4582 - acc: 0.7773
Epoch 79/100
768/768 [==============================] - 0s 100us/step - loss: 0.4453 - acc: 0.8008
Epoch 80/100
768/768 [==============================] - 0s 101us/step - loss: 0.4588 - acc: 0.7865
Epoch 81/100
768/768 [==============================] - 0s 96us/step - loss: 0.4470 - acc: 0.7786
Epoch 82/100
768/768 [==============================] - 0s 96us/step - loss: 0.4477 - acc: 0.7878
Epoch 83/100
768/768 [==============================] - 0s 95us/step - loss: 0.4577 - acc: 0.7943
Epoch 84/100
768/768 [==============================] - 0s 91us/step - loss: 0.4485 - acc: 0.7839
Epoch 85/100
768/768 [==============================] - 0s 96us/step - loss: 0.4456 - acc: 0.7891
Epoch 86/100
768/768 [==============================] - 0s 94us/step - loss: 0.4465 - acc: 0.7917
Epoch 87/100
768/768 [==============================] - 0s 91us/step - loss: 0.4618 - acc: 0.7878
Epoch 88/100
768/768 [==============================] - 0s 91us/step - loss: 0.4530 - acc: 0.7943
Epoch 89/100
768/768 [==============================] - 0s 91us/step - loss: 0.4471 - acc: 0.7799
Epoch 90/100
768/768 [==============================] - 0s 93us/step - loss: 0.4574 - acc: 0.7839
Epoch 91/100
768/768 [==============================] - 0s 91us/step - loss: 0.4453 - acc: 0.8034
Epoch 92/100
768/768 [==============================] - 0s 89us/step - loss: 0.4521 - acc: 0.7839
Epoch 93/100
768/768 [==============================] - 0s 94us/step - loss: 0.4568 - acc: 0.7813
Epoch 94/100
768/768 [==============================] - 0s 94us/step - loss: 0.4650 - acc: 0.7643
Epoch 95/100
768/768 [==============================] - 0s 95us/step - loss: 0.4641 - acc: 0.7904
Epoch 96/100
768/768 [==============================] - 0s 94us/step - loss: 0.4508 - acc: 0.7852
Epoch 97/100
768/768 [==============================] - 0s 95us/step - loss: 0.4380 - acc: 0.7878
Epoch 98/100
768/768 [==============================] - 0s 93us/step - loss: 0.4509 - acc: 0.7930
Epoch 99/100
768/768 [==============================] - 0s 92us/step - loss: 0.4449 - acc: 0.7904
Epoch 100/100
768/768 [==============================] - 0s 93us/step - loss: 0.4421 - acc: 0.7995
<keras.callbacks.History at 0x2133e336208>
评估模型
# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
768/768 [==============================] - 0s 20us/step
acc: 80.99%
预测
# calculate predictions
predictions = model.predict(X)
# round predictions
rounded = [round(x) for x in predictions.ravel()]
print(rounded)
[1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.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