Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. #Libraries needed to run the tool
  2. import numpy as np
  3. import pandas as pd
  4. from sklearn.neural_network import MLPRegressor
  5. from sklearn.neural_network import MLPClassifier
  6. from sklearn import preprocessing #to normalize the values
  7. from sklearn.model_selection import train_test_split
  8. from sklearn.model_selection import cross_val_score
  9. import matplotlib.pyplot as plt
  10.  
  11. #Ask for file name and read the file
  12. #file_name = raw_input("Name of file:")
  13. file_name = 'HW8_data'
  14. data = pd.read_csv(file_name + '.csv', header=0)
  15.  
  16. analysis_type = input("Analysis Type 'R' or 'C': ")
  17.  
  18. #Print number of rows and colums read
  19. print("{0} rows and {1} columns".format(len(data.index), len(data.columns)))
  20. print("")
  21.  
  22. #Defining X1, X2, and all the data X
  23. X1 = data.AvgHW
  24. X2 = data.AvgQuiz
  25. X3 = data.AvgLab
  26. X4 = data.MT1
  27. X5 = data.MT2
  28. X6 = data.Final
  29. X7 = data.Participation
  30. X_raw = np.column_stack((X1, X2, X3, X4, X5, X6, X7))
  31.  
  32. #Normalizing or not the data
  33. min_max_scaler = preprocessing.MinMaxScaler()
  34. X = min_max_scaler.fit_transform(X_raw)
  35. #X = X_raw
  36. #print(X)
  37.  
  38. #Defining Y variables depending on whether we have a regression or classification problem
  39. if analysis_type == 'R':
  40. Y = data.Grade
  41. else:
  42. Y = data.Letter
  43.  
  44. #Using Built in train test split function in sklearn
  45. X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size = 0.8)
  46.  
  47.  
  48. if analysis_type == 'R':
  49. #Fit the neural network for Regression purposes (i.e., you expect a continuous variable out)
  50. #Note that 'sgd' and 'adam' require a batch_size and the function is not as clear
  51. acti = ['logistic', 'tanh', 'relu', 'identity']
  52. algo = ['lbfgs', 'sgd', 'adam']
  53. learn = ['constant', 'invscaling', 'adaptive']
  54. neural = MLPRegressor(activation=acti[0], solver=algo[0], batch_size = 1, learning_rate = learn[0], hidden_layer_sizes=(7,))
  55.  
  56. #Cross validation
  57. neural_scores = cross_val_score(neural, X_train, Y_train, cv=5)
  58. print("Cross Validation Accuracy: {0} (+/- {1})".format(neural_scores.mean().round(2), (neural_scores.std() * 2).round(2)))
  59. print("")
  60.  
  61. #Fitting final neural network
  62. neural.fit(X_train, Y_train)
  63. neural_score = neural.score(X_test, Y_test)
  64. print("Shape of neural network: {0}".format([coef.shape for coef in neural.coefs_]))
  65. print("Coefs: ")
  66. print("")
  67. print(neural.coefs_[0].round(2))
  68. print("")
  69. print(neural.coefs_[1].round(2))
  70. print("")
  71. print("Intercepts: {0}".format(neural.intercepts_))
  72. print("")
  73. print("Loss: {0}".format(neural.loss_))
  74. print("")
  75. print("Iteration: {0}".format(neural.n_iter_))
  76. print("")
  77. print("Layers: {0}".format(neural.n_layers_))
  78. print("")
  79. print("Outputs: {0}".format(neural.n_outputs_))
  80. print("")
  81. print("Output Activation: {0}".format(neural.out_activation_)) #identity because we are looking for a value
  82. print("")
  83.  
  84. #Assess the fitted Neural Network
  85. print("Y test and predicted")
  86. print(Y_test.values)
  87. print(neural.predict(X_test).round(1))
  88. print("")
  89. print("Accuracy as Pearson's R2: {0}".format(neural_score.round(4)))
  90. print("")
  91.  
  92. else:
  93. #Fit the neural network for Classification purposes (i.e., you don't expect a continuous variable out).
  94. #Note that 'sgd' and 'adam' require a batch_size and the function is not as clear
  95. acti = ['logistic', 'tanh', 'relu', 'identity']
  96. algo = ['lbfgs', 'sgd', 'adam']
  97. learn = ['constant', 'invscaling', 'adaptive']
  98. neural = MLPClassifier(activation=acti[0], solver=algo[0], batch_size = 1, learning_rate = learn[1], hidden_layer_sizes=(7,))
  99.  
  100. #Cross validation
  101. neural_scores = cross_val_score(neural, X_train, Y_train, cv=5)
  102. print("Cross Validation Accuracy: {0} (+/- {1})".format(neural_scores.mean().round(2), (neural_scores.std() * 2).round(2)))
  103. print("")
  104.  
  105. #Fitting final neural network
  106. neural.fit(X_train, Y_train)
  107. neural_score = neural.score(X_test, Y_test)
  108. print("Classes: {0}".format(neural.classes_))
  109. print("")
  110. print("Shape of neural network: {0}".format([coef.shape for coef in neural.coefs_]))
  111. print("")
  112. print("Coefs: ")
  113. print(neural.coefs_[0].round(2))
  114. print("")
  115. print(neural.coefs_[1].round(2))
  116. print("")
  117. print("Intercepts: {0}".format(neural.intercepts_))
  118. print("")
  119. print("Loss: {0}".format(neural.loss_))
  120. print("")
  121. print("Iteration: {0}".format(neural.n_iter_))
  122. print("")
  123. print("Layers: {0}".format(neural.n_layers_))
  124. print("")
  125. print("Outputs: {0}".format(neural.n_outputs_))
  126. print("")
  127. print("Output Activation: {0}".format(neural.out_activation_)) #softmax to get a probability between 0 and 1
  128. print("")
  129.  
  130. #Assess the fitted Neural Network
  131. print("Y test and predicted")
  132. print(Y_test.values)
  133. print(neural.predict(X_test))
  134. print("")
  135. print("Mean Accuracy: {0}".format(neural_score.round(4)))
  136. print("")
  137.  
  138.  
  139.  
  140. Activity
  141. No Activity Yet
  142. Comment and @mention people to notify them.
  143. Comment(optional)
  144. Write a comment
  145.  
  146. @mention users to notify them.
  147. 0 items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement