daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from sklearn.model_selection import GridSearchCV
  2.  
  3. parameters = {'solver': ['adam', 'sgd'], 'max_iter': [1000], \
  4.               'hidden_layer_sizes':[(100,), (30,20,10,)], \
  5.               'random_state':np.arange(0,2), 'tol':[1e-7], 'batch_size':[20,60], 'shuffle':[True, False],\
  6.               'momentum':[0.95,0.85], 'activation': ['logistic','relu'], 'alpha':[1e-5,1e-6]}
  7.  
  8. grid_search = GridSearchCV(MLPRegressor(), parameters, n_jobs=-1, verbose=1, cv=4)
  9. grid_search.fit(boston_train_data, boston_train_target)
  10.        
  11. print(grid_search.score(boston_test_data, boston_test_target))
  12. print(grid_search.best_params_)
  13.  
  14.  
  15. import numpy as np
  16.  
  17. from sklearn.linear_model import LogisticRegression
  18.  
  19. from keras.datasets import fashion_mnist
  20. from sklearn.metrics import accuracy_score
  21. from sklearn.metrics import confusion_matrix
  22.  
  23. import seaborn as sns;
  24.  
  25. (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
  26.  
  27.  
  28. images_train =  []
  29. for image_train in x_train:
  30.     images_train.append(image_train.flatten())
  31.  
  32. images_test = []
  33.  
  34. for image_test in x_test:
  35.     images_test.append(image_test.flatten())
  36.  
  37. images_train = np.array(images_train)
  38. images_test = np.array(images_test)
  39.  
  40. linear_model = LogisticRegression(verbose=1, max_iter=10, multi_class="multinomial", solver="sag")
  41.  
  42. linear_model.fit(images_train, y_train)
  43.  
  44. conf_matrix = confusion_matrix(y_test, linear_model.predict(images_test))
  45.  
  46. #print("Confusion_matrix:")
  47. #print(conf_matrix)
  48.  
  49. sns.heatmap(conf_matrix)
  50.  
  51. acc = accuracy_score(y_test, linear_model.predict(images_test))
  52. print("Linear model accuracy is {0:0.2f}".format(acc))
  53.  
  54. from sklearn.neural_network import MLPClassifier
  55.  
  56. neural_network = MLPClassifier(hidden_layer_sizes=(200,100,50),random_state=1)
  57.  
  58. #neural_network.fit(images_train, y_train)
  59. neural_network.fit(preprocessing.StandardScaler().fit_transform(images_train), y_train)
  60.  
  61. conf_matrix_neural_network = confusion_matrix(y_test, \
  62.                                               neural_network.predict(preprocessing.StandardScaler().fit_transform(images_test)))
  63.  
  64. #print("Confusion_matrix:")
  65. #print(conf_matrix_neural_network)
  66.  
  67. sns.heatmap(conf_matrix_neural_network)
  68.  
  69. acc = accuracy_score(y_test, neural_network.predict(preprocessing.StandardScaler().fit_transform(images_test)))
  70. print("Neural network model accuracy is {0:0.2f}".format(acc))
  71.  
  72. import matplotlib.pyplot as plt
  73. plt.rcParams['figure.figsize'] = (8.0, 6.0)
  74. plt.plot(neural_network.loss_curve_)
  75. plt.title('Neural network cost function loss')
  76.  
  77. plt.xlabel('epoch'); plt.ylabel('error value'); plt.grid();
  78.  
  79. print("Number of connection between input and first hidden layer:")
  80. print(np.size(neural_network.coefs_[0]))
  81.  
  82. print("Number of connection between first and second hidden layer:")
  83. print(np.size(neural_network.coefs_[1]))
  84.  
  85.  
  86. plt.rcParams['figure.figsize'] = (8.0, 6.0)
  87. plt.imshow(np.transpose(neural_network.coefs_[0]), cmap=plt.get_cmap("gray"), aspect="auto")
  88. plt.ylabel('neurons in first hidden layer'); plt.xlabel('input weights to neural network');
  89.  
  90.  
  91. plt.rcParams['figure.figsize'] = (8.0, 6.0)
  92. plt.imshow(np.transpose(neural_network.coefs_[1]), cmap=plt.get_cmap("gray"), aspect="auto")
  93. plt.ylabel('neurons in second hidden layer'); plt.xlabel('neurons in first hidden layer');
  94.  
  95. plt.rcParams['figure.figsize'] = [10, 60]
  96. m=200
  97. for i in range(0,m):
  98.     plt.subplot(m/2, 20, i+1)
  99.     plt.axis('off')
  100.     hidden_2 = np.transpose(neural_network.coefs_[0])[i]
  101.     plt.imshow(np.reshape(hidden_2, (28,28)), cmap=plt.get_cmap("gray"),  aspect=1)
  102.  
  103.  
  104. import keras
  105. from keras.datasets import mnist
  106. #load mnist dataset
  107. (X_train, y_train), (X_test, y_test) = mnist.load_data()
  108.  
  109. #...
  110.  
  111. print("Datasets size")
  112. print("Train data:", X_train.shape)
  113. print("Test data:", X_test.shape)
  114.  
  115. print("Samples from training data:")
  116. for i in range(0,10):
  117.     plt.subplot(1,10,i+1)
  118.     plt.imshow(X_train[i], cmap=plt.get_cmap("gray"))
  119.     plt.title(y_train[i]);
  120.     plt.axis('off');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top