SHARE
TWEET

Untitled

a guest Dec 13th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3. from sklearn.linear_model import LogisticRegression
  4.  
  5. from keras.datasets import fashion_mnist
  6. from sklearn.metrics import accuracy_score
  7. from sklearn.metrics import confusion_matrix
  8.  
  9. import seaborn as sns;
  10.  
  11. (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
  12. x_train = x_train[len(x_train)-1000:]
  13. y_train = y_train[len(y_train)-1000:]
  14.  
  15. x_test = x_test[len(x_test)-1000:]
  16. y_test = y_test[len(y_test)-1000:]
  17.  
  18. images_train =  []
  19. for image_train in x_train:
  20.     images_train.append(image_train.flatten())
  21.  
  22. images_test = []
  23.  
  24. for image_test in x_test:
  25.     images_test.append(image_test.flatten())
  26.  
  27. images_train = np.array(images_train)
  28. images_test = np.array(images_test)
  29.  
  30. print(len(x_test))
  31. print("Starting grid search\n")
  32. from sklearn.model_selection import GridSearchCV
  33. from sklearn.neural_network import MLPClassifier
  34.  
  35. parameters = {'solver': ['sgd'], 'max_iter': [1000], \
  36.               'hidden_layer_sizes': [(15,10,5), (30, 20, 10,)], \
  37.               'random_state': [1], 'tol': [1e-7], 'batch_size': [20], 'shuffle': [True, False], \
  38.               'momentum': [0.95], 'activation': ['logistic'], 'alpha': [1e-5]}
  39.  
  40. grid_search = GridSearchCV(MLPClassifier(), parameters, n_jobs=-1, verbose=1, cv=2)
  41. grid_search.fit(images_train, y_train)
  42.  
  43. print(grid_search.score(images_test, y_test))
  44. print(grid_search.best_params_)
  45.  
  46. print("Starting MLPClassifier\n")
  47.  
  48.  
  49. neural_network = MLPClassifier(hidden_layer_sizes=(30,20,10),random_state=1)
  50.  
  51. neural_network.fit(images_train, y_train)
  52. #neural_network.fit(preprocessing.StandardScaler().fit_transform(images_train), y_train)
  53.  
  54. conf_matrix_neural_network = confusion_matrix(y_test, \
  55.                                               neural_network.predict(images_test))
  56.  
  57. #print("Confusion_matrix:")
  58. #print(conf_matrix_neural_network)
  59.  
  60. sns.heatmap(conf_matrix_neural_network)
  61.  
  62. acc = accuracy_score(y_test, neural_network.predict(images_test))
  63. print("Neural network model accuracy is {0:0.2f}".format(acc))
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