Guest User

Untitled

a guest
Jul 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. from keras.models import Sequential
  2. from keras.layers import Dense
  3. import numpy
  4. # fix random seed for reproducibility
  5. numpy.random.seed(7)
  6.  
  7. #import / load datasets
  8. #Download the dataset and place it in your local working directory, the same as your python file. Save it with the file name:
  9. #pima-indians-diabetes.csv
  10. #You can now load the file directly using the NumPy function loadtxt().
  11. #There are eight input variables and one output variable (the last column).
  12. #Once loaded we can split the dataset into input variables (X) and the output class variable (Y).
  13. # load pima indians dataset
  14. dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
  15. # split into input (X) and output (Y) variables
  16. X = dataset[:,0:8]
  17. Y = dataset[:,8]
  18. #2. Define model
  19.  
  20. # create model
  21. model = Sequential()
  22. model.add(Dense(12, input_dim=8, activation='relu'))
  23. model.add(Dense(8, activation='relu'))
  24. model.add(Dense(1, activation='sigmoid'))
  25.  
  26. #3. Compile model
  27.  
  28. # Compile model
  29. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  30.  
  31. #4. Fit models
  32. # Fit the model
  33. model.fit(X, Y, epochs=150, batch_size=10)
  34.  
  35. #5. Evaluate model
  36. # evaluate the model
  37. scores = model.evaluate(X, Y)
  38. print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
  39.  
  40. #6. Tie it all together
  41. # Create your first MLP in Keras
  42. from keras.models import Sequential
  43. from keras.layers import Dense
  44. import numpy
  45. # fix random seed for reproducibility
  46. numpy.random.seed(7)
  47. # load pima indians dataset
  48. dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
  49. # split into input (X) and output (Y) variables
  50. X = dataset[:,0:8]
  51. Y = dataset[:,8]
  52. # create model
  53. model = Sequential()
  54. model.add(Dense(12, input_dim=8, activation='relu'))
  55. model.add(Dense(8, activation='relu'))
  56. model.add(Dense(1, activation='sigmoid'))
  57. # Compile model
  58. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  59. # Fit the model
  60. model.fit(X, Y, epochs=150, batch_size=10)
  61. # evaluate the model
  62. scores = model.evaluate(X, Y)
  63. print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
  64.  
  65. output :
  66. #...
  67. Epoch 145/150
  68. 768/768 [==============================] - 0s - loss: 0.5105 - acc: 0.7396
  69. Epoch 146/150
  70. 768/768 [==============================] - 0s - loss: 0.4900 - acc: 0.7591
  71. Epoch 147/150
  72. 768/768 [==============================] - 0s - loss: 0.4939 - acc: 0.7565
  73. Epoch 148/150
  74. 768/768 [==============================] - 0s - loss: 0.4766 - acc: 0.7773
  75. Epoch 149/150
  76. 768/768 [==============================] - 0s - loss: 0.4883 - acc: 0.7591
  77. Epoch 150/150
  78. 768/768 [==============================] - 0s - loss: 0.4827 - acc: 0.7656
  79. 32/768 [>.............................] - ETA: 0s
  80. acc: 78.26%*//
  81.  
  82. 7.Make prediction :
  83.  
  84. # Create first network with Keras
  85. from keras.models import Sequential
  86. from keras.layers import Dense
  87. import numpy
  88. # fix random seed for reproducibility
  89. seed = 7
  90. numpy.random.seed(seed)
  91. # load pima indians dataset
  92. dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
  93. # split into input (X) and output (Y) variables
  94. X = dataset[:,0:8]
  95. Y = dataset[:,8]
  96. # create model
  97. model = Sequential()
  98. model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
  99. model.add(Dense(8, init='uniform', activation='relu'))
  100. model.add(Dense(1, init='uniform', activation='sigmoid'))
  101. # Compile model
  102. model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
  103. # Fit the model
  104. model.fit(X, Y, epochs=150, batch_size=10, verbose=2)
  105. # calculate predictions
  106. predictions = model.predict(X)
  107. # round predictions
  108. rounded = [round(x[0]) for x in predictions]
  109. print(rounded)
  110.  
  111. #Running this modified example now prints the predictions for each input pattern.
  112. #We could use these predictions directly in our application if needed
  113. #output of predictions
  114. #predicted value in the form of array
Add Comment
Please, Sign In to add comment