Guest User

Untitled

a guest
Feb 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. # Import `tensorflow`
  2. import tensorflow as tf
  3.  
  4. # Initialize two constants
  5. x1 = tf.constant([1,2,3,4])
  6. x2 = tf.constant([5,6,7,8])
  7.  
  8. # Multiply
  9. result = tf.multiply(x1, x2)
  10.  
  11. # Initialize Session and run `result`
  12. with tf.Session() as sess:
  13. output = sess.run(result)
  14. print(output)
  15. # Import `tensorflow`
  16. import tensorflow as tf
  17.  
  18. # Initialize two constants
  19. x1 = tf.constant([1,2,3,4])
  20. x2 = tf.constant([5,6,7,8])
  21.  
  22. # Multiply
  23. result = tf.multiply(x1, x2)
  24.  
  25. # Intialize the Session
  26. sess = tf.Session()
  27.  
  28. # Print the result
  29. print(sess.run(result))
  30.  
  31. # Close the session
  32. sess.close()
  33. # Setup the pipeline
  34. steps = [('scaler', StandardScaler()),
  35. ('SVM', SVC())]
  36.  
  37. pipeline = Pipeline(steps)
  38.  
  39. # Specify the hyperparameter space
  40. parameters = {'SVM__C':[1, 10, 100],
  41. 'SVM__gamma':[0.1, 0.01]}
  42.  
  43. # Create train and test sets
  44. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=21)
  45.  
  46. # Instantiate the GridSearchCV object: cv
  47. cv = GridSearchCV(pipeline, parameters)
  48.  
  49. # Fit to the training set
  50. cv.fit(X_train, y_train)
  51.  
  52. # Predict the labels of the test set: y_pred
  53. y_pred = cv.predict(X_test)
  54.  
  55. # Compute and print metrics
  56. print("Accuracy: {}".format(cv.score(X_test, y_test)))
  57. print(classification_report(y_test, y_pred))
  58. print("Tuned Model Parameters: {}".format(cv.best_params_))
  59. # Import necessary modules
  60. from sklearn.preprocessing import Imputer
  61. from sklearn.pipeline import Pipeline
  62. from sklearn.svm import SVC
  63.  
  64. # Setup the pipeline steps: steps
  65. steps = [('imputation', Imputer(missing_values='NaN', strategy='most_frequent', axis=0)),
  66. ('SVM', SVC())]
  67.  
  68. # Create the pipeline: pipeline
  69. pipeline = Pipeline(steps)
  70.  
  71. # Create training and test sets
  72. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
  73.  
  74. # Fit the pipeline to the train set
  75. pipeline.fit(X_train, y_train)
  76.  
  77. # Predict the labels of the test set
  78. y_pred = pipeline.predict(X_test)
  79.  
  80. # Compute metrics
  81. print(classification_report(y_test, y_pred))
  82.  
  83. # Import the Imputer module
  84. from sklearn.svm import SVC
  85. from sklearn.preprocessing import Imputer
  86.  
  87. # Setup the Imputation transformer: imp
  88. imp = Imputer(missing_values='NaN', strategy='most_frequent', axis=0)
  89.  
  90. # Instantiate the SVC classifier: clf
  91. clf = SVC()
  92.  
  93. # Setup the pipeline with the required steps: steps
  94. steps = [('imputation', imp),
  95. ('SVM', clf)]
  96. # Import necessary modules
  97. from sklearn.model_selection import cross_val_score
  98. from sklearn.linear_model import Ridge
  99.  
  100. # Instantiate a ridge regressor: ridge
  101. ridge = Ridge(alpha=0.5, normalize=True)
  102.  
  103. # Perform 5-fold cross-validation: ridge_cv
  104. ridge_cv = cross_val_score(ridge, X, y, cv=5)
  105.  
  106. # Print the cross-validated scores
  107. print(ridge_cv)
  108. # Create dummy variables: df_region
  109. df_region = pd.get_dummies(df)
  110.  
  111. # Print the columns of df_region
  112. print(df_region.columns)
  113.  
  114. # Create dummy variables with drop_first=True: df_region
  115. df_region = pd.get_dummies(df, drop_first=True)
  116.  
  117. # Print the new columns of df_region
  118. print(df_region.columns)
  119. # Import pandas
  120. import pandas as pd
  121.  
  122. # Read 'gapminder.csv' into a DataFrame: df
  123. df = pd.read_csv('gapminder.csv')
  124.  
  125. # Create a boxplot of life expectancy per region
  126. df.boxplot('life', 'Region', rot=60)
  127.  
  128. # Show the plot
  129. plt.show()
  130. # Import necessary modules
  131. from sklearn.model_selection import cross_val_score
  132. from sklearn.metrics import roc_auc_score
  133.  
  134. # Compute predicted probabilities: y_pred_prob
  135. y_pred_prob = logreg.predict_proba(X_test)[:,1]
  136.  
  137. # Compute and print AUC score
  138. print("AUC: {}".format(roc_auc_score(y_test, y_pred_prob)))
  139.  
  140. # Compute cross-validated AUC scores: cv_auc
  141. cv_auc = cross_val_score(logreg, X, y, cv=5, scoring='roc_auc')
  142.  
  143. # Print list of AUC scores
  144. print("AUC scores computed using 5-fold cross-validation: {}".format(cv_auc))
  145. # Import necessary modules
  146. from sklearn.metrics import roc_curve
  147.  
  148. # Compute predicted probabilities: y_pred_prob
  149. y_pred_prob = logreg.predict_proba(X_test)[:,1]
  150.  
  151. # Generate ROC curve values: fpr, tpr, thresholds
  152. fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob)
  153.  
  154. # Plot ROC curve
  155. plt.plot([0, 1], [0, 1], 'k--')
  156. plt.plot(fpr, tpr)
  157. plt.xlabel('False Positive Rate')
  158. plt.ylabel('True Positive Rate')
  159. plt.title('ROC Curve')
  160. plt.show()
  161. # Setup arrays to store train and test accuracies
  162. neighbors = np.arange(1, 9)
  163. train_accuracy = np.empty(len(neighbors))
  164. test_accuracy = np.empty(len(neighbors))
  165.  
  166. # Loop over different values of k
  167. for i, k in enumerate(neighbors):
  168. # Setup a k-NN Classifier with k neighbors: knn
  169. knn = KNeighborsClassifier(n_neighbors=k)
  170.  
  171. # Fit the classifier to the training data
  172. knn.fit(X_train, y_train)
  173.  
  174. #Compute accuracy on the training set
  175. train_accuracy[i] = knn.score(X_train, y_train)
  176.  
  177. #Compute accuracy on the testing set
  178. test_accuracy[i] = knn.score(X_test, y_test)
  179.  
  180. # Generate plot
  181. plt.title('k-NN: Varying Number of Neighbors')
  182. plt.plot(neighbors, test_accuracy, label = 'Testing Accuracy')
  183. plt.plot(neighbors, train_accuracy, label = 'Training Accuracy')
  184. plt.legend()
  185. plt.xlabel('Number of Neighbors')
  186. plt.ylabel('Accuracy')
  187. plt.show()
  188. # Import necessary modules
  189. from sklearn.model_selection import train_test_split
  190. from sklearn.metrics import mean_squared_error
  191. from sklearn.linear_model import LinearRegression
  192.  
  193. # Create training and test sets
  194. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state=42)
  195.  
  196. # Create the regressor: reg_all
  197. reg_all = LinearRegression()
  198.  
  199. # Fit the regressor to the training data
  200. reg_all.fit(X_train, y_train)
  201.  
  202. # Predict on the test data: y_pred
  203. y_pred = reg_all.predict(X_test)
  204.  
  205. # Compute and print R^2 and RMSE
  206. print("R^2: {}".format(reg_all.score(X_test, y_test)))
  207. rmse = np.sqrt(mean_squared_error(y_test, y_pred))
  208. print("Root Mean Squared Error: {}".format(rmse))
  209. By default, scikit-learn's cross_val_score() function uses $R_2R_2$ as the metric of choice for regression. Since you are performing 5-fold cross-validation, the function will return 5 scores. Your job is to compute these 5 scores and then take their average.
  210.  
  211. # Import the necessary modules
  212. from sklearn.linear_model import LinearRegression
  213. from sklearn.model_selection import cross_val_score
  214.  
  215. # Create a linear regression object: reg
  216. reg = LinearRegression()
  217.  
  218. # Compute 5-fold cross-validation scores: cv_scores
  219. cv_scores = cross_val_score(reg, X, y, cv=5)
  220.  
  221. # Print the 5-fold cross-validation scores
  222. print(cv_scores)
  223.  
  224. # Print the average 5-fold cross-validation score
  225. print("Average 5-Fold CV Score: {}".format(np.mean(cv_scores)))
  226. # Import necessary modules
  227. from sklearn.metrics import classification_report
  228. from sklearn.metrics import confusion_matrix
  229.  
  230. # Create training and test set
  231. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42)
  232.  
  233. # Instantiate a k-NN classifier: knn
  234. knn = KNeighborsClassifier(n_neighbors=6)
  235.  
  236. # Fit the classifier to the training data
  237. knn.fit(X_train, y_train)
  238.  
  239. # Predict the labels of the test data: y_pred
  240. y_pred = knn.predict(X_test)
  241.  
  242. # Generate the confusion matrix and classification report
  243. print(confusion_matrix(y_test, y_pred))
  244. print(classification_report(y_test, y_pred))
Add Comment
Please, Sign In to add comment