Advertisement
dfracz3

HW 8 Neural Netowrks second 3 ii dan's version

Mar 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. #
  2. ## -*- coding: utf-8 -*-
  3. #"""
  4. #Created on Sun Mar 11 15:41:00 2018
  5. #
  6. #@author: UIMCC-Staff
  7. #"""
  8. #
  9. ##Libraries needed to run the tool
  10. #import numpy as np
  11. #import pandas as pd
  12. #from sklearn.neural_network import MLPRegressor
  13. #from sklearn.neural_network import MLPClassifier
  14. #from sklearn import preprocessing #to normalize the values
  15. #from sklearn.model_selection import train_test_split
  16. #from sklearn.model_selection import cross_val_score
  17. #import matplotlib.pyplot as plt
  18. #
  19. ##Ask for file name and read the file
  20. ##file_name = raw_input("Name of file:")
  21. #file_name = 'forestfiresw8'
  22. #data = pd.read_csv(file_name + '.csv', header=0)
  23. #
  24. #analysis_type = input("Analysis Type 'R' or 'C': ")
  25. #
  26. ##Print number of rows and colums read
  27. #print("{0} rows and {1} columns".format(len(data.index), len(data.columns)))
  28. #print("")
  29. #
  30. #data["month"] = data["month"].replace("jan",1)
  31. #data["month"] = data["month"].replace("feb",2)
  32. #data["month"] = data["month"].replace("mar",3)
  33. #data["month"] = data["month"].replace("apr",4)
  34. #data["month"] = data["month"].replace("may",5)
  35. #data["month"] = data["month"].replace("jun",6)
  36. #data["month"] = data["month"].replace("jul",7)
  37. #data["month"] = data["month"].replace("aug",8)
  38. #data["month"] = data["month"].replace("sep",9)
  39. #data["month"] = data["month"].replace("oct",10)
  40. #data["month"] = data["month"].replace("nov",11)
  41. #data["month"] = data["month"].replace("dec",12)
  42. #
  43. #
  44. #
  45. ##Defining X1, X2, and all the data X
  46. #X1 = data.FFMC
  47. #X2 = data.DMC
  48. #X3 = data.DC
  49. #X4 = data.ISI
  50. #X5 = data.temp
  51. #X6 = data.RH
  52. #X7 = data.wind
  53. #X8 = data.rain
  54. #X_raw = np.column_stack((X1, X2, X3, X4, X5, X6, X7, X8))
  55. #
  56. ##Normalizing or not the data
  57. #min_max_scaler = preprocessing.MinMaxScaler()
  58. #X = min_max_scaler.fit_transform(X_raw)
  59. ##X = X_raw
  60. ##print(X)
  61. #
  62. ##Defining Y variables depending on whether we have a regression or classification problem
  63. #if analysis_type == 'R':
  64. # Y = data.month
  65. #
  66. #
  67. ##Using Built in train test split function in sklearn
  68. #X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size = 0.8)
  69. #
  70. #if analysis_type == 'R':
  71. # neuron_count = []
  72. # for i in range (1,201):
  73. #
  74. # #Fit the neural network for Regression purposes (i.e., you expect a continuous variable out)
  75. # #Note that 'sgd' and 'adam' require a batch_size and the function is not as clear
  76. # acti = ['logistic', 'tanh', 'relu', 'identity']
  77. # algo = ['lbfgs', 'sgd', 'adam']
  78. # learn = ['constant', 'invscaling', 'adaptive']
  79. # neural = MLPRegressor(activation=acti[0], solver=algo[0], batch_size = 1, learning_rate = learn[0], hidden_layer_sizes=(i,))
  80. #
  81. # #Cross validation
  82. # neural_scores = cross_val_score(neural, X_train, Y_train, cv=5)
  83. ## print("Cross Validation Accuracy: {0} (+/- {1})".format(neural_scores.mean().round(2), (neural_scores.std() * 2).round(2)))
  84. ## print("")
  85. # rounded = neural_scores.mean().round(2)
  86. # neuron_count.append(rounded)
  87. #
  88. # #Cross validation
  89. # neural_scores = cross_val_score(neural, X_train, Y_train, cv=15)
  90. # print("Cross Validation Accuracy: {0} (+/- {1})".format(neural_scores.mean().round(2), (neural_scores.std() * 2).round(2)))
  91. ## print("")
  92. #
  93. # #Fitting final neural network
  94. # neural.fit(X_train, Y_train)
  95. # neural_score = neural.score(X_test, Y_test)
  96. ## print("Shape of neural network: {0}".format([coef.shape for coef in neural.coefs_]))
  97. ## print("Coefs: ")
  98. ## print("")
  99. ## print(neural.coefs_[0].round(2))
  100. ## print("")
  101. ## print(neural.coefs_[1].round(2))
  102. ## print("")
  103. ## print("Intercepts: {0}".format(neural.intercepts_))
  104. ## print("")
  105. ## print("Loss: {0}".format(neural.loss_))
  106. ## print("")
  107. ## print("Iteration: {0}".format(neural.n_iter_))
  108. ## print("")
  109. ## print("Layers: {0}".format(neural.n_layers_))
  110. ## print("")
  111. ## print("Outputs: {0}".format(neural.n_outputs_))
  112. ## print("")
  113. ## print("Output Activation: {0}".format(neural.out_activation_)) #identity because we are looking for a value
  114. ## print("")
  115. #
  116. # #Assess the fitted Neural Network
  117. # print("Y test and predicted")
  118. # print(Y_test.values)
  119. # print(neural.predict(X_test).round(1))
  120. # print("")
  121. # print("Accuracy as Pearson's R2: {0}".format(neural_score.round(4)))
  122. # print("")
  123. #
  124. ##fix, ax = plt.subplots()
  125. ##ax.scatter(Y_test.values, neural.predict(X_test), edgecolors=(0, 0, 0))
  126. ##ax.plot([Y.min(0), Y.max(100)], [X.min(0), X.max(100)], 'k--', lw=3)
  127. ##ax.set_xlabel('Measured Grade')
  128. ##ax.set_xlabel('Predicted Grade')
  129. #
  130. #
  131. #fix, ax = plt.subplots()
  132. #ax.scatter(neural.predict(X_test), Y_test.values)
  133. #plt.axis([0, 100, 0, 100])
  134. #plt.grid(True)
  135. #ax.set_xlabel('Number of Neurons')
  136. #ax.set_ylabel('Accuracy')
  137. ##else:
  138. ## #Fit the neural network for Classification purposes (i.e., you don't expect a continuous variable out).
  139. ## #Note that 'sgd' and 'adam' require a batch_size and the function is not as clear
  140. ## acti = ['logistic', 'tanh', 'relu', 'identity']
  141. ## algo = ['lbfgs', 'sgd', 'adam']
  142. ## learn = ['constant', 'invscaling', 'adaptive']
  143. ## neural = MLPClassifier(activation=acti[2], solver=algo[2], batch_size = 1, learning_rate = learn[2], hidden_layer_sizes=(7,500))
  144. ##
  145. ## #Cross validation
  146. ## neural_scores = cross_val_score(neural, X_train, Y_train, cv=5)
  147. ## print("Cross Validation Accuracy: {0} (+/- {1})".format(neural_scores.mean().round(2), (neural_scores.std() * 2).round(2)))
  148. ## print("")
  149. ##
  150. ## #Fitting final neural network
  151. ## neural.fit(X_train, Y_train)
  152. ## neural_score = neural.score(X_test, Y_test)
  153. ## print("Classes: {0}".format(neural.classes_))
  154. ## print("")
  155. ## print("Shape of neural network: {0}".format([coef.shape for coef in neural.coefs_]))
  156. ## print("")
  157. ## print("Coefs: ")
  158. ## print(neural.coefs_[0].round(2))
  159. ## print("")
  160. ## print(neural.coefs_[1].round(2))
  161. ## print("")
  162. ## print("Intercepts: {0}".format(neural.intercepts_))
  163. ## print("")
  164. ## print("Loss: {0}".format(neural.loss_))
  165. ## print("")
  166. ## print("Iteration: {0}".format(neural.n_iter_))
  167. ## print("")
  168. ## print("Layers: {0}".format(neural.n_layers_))
  169. ## print("")
  170. ## print("Outputs: {0}".format(neural.n_outputs_))
  171. ## print("")
  172. ## print("Output Activation: {0}".format(neural.out_activation_)) #softmax to get a probability between 0 and 1
  173. ## print("")
  174. ##
  175. ## #Assess the fitted Neural Network
  176. ## print("Y test and predicted")
  177. ## print(Y_test.values)
  178. ## print(neural.predict(X_test))
  179. ## print("")
  180. ## print("Mean Accuracy: {0}".format(neural_score.round(4)))
  181. ## print("")
  182. ##
  183. ###fix, ax = plt.subplots()
  184. ###ax.scatter(Y_test, X_test, edgecolors=(0, 0, 0))
  185. ###ax.plot([y.min(0), y.max(100)], [x.min(0), x.max(100)], 'k--', lw=4)
  186. ###ax.set_xlabel('Measured')
  187. ###ax.set_xlabel('Predicted')
  188. ###
  189. ####fig, ax =plt.subplots
  190. ####ax.scatter(y_predicted, edgecolors=(0, 0, 0))
  191. ###ax.plot
  192. ###plt.xlabel('X values')
  193. ###plt.ylabel('Area')
  194. ###plt.savefig(file_name, dpi=300)
  195. ###plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement