Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import os
  2. import pickle
  3. import matplotlib.pyplot as plt
  4. import matplotlib.cm as cm
  5. import numpy as np
  6.  
  7. print("HELLO WORLDDDD")
  8.  
  9.  
  10. data = pickle.load(open("assignment2.pkl", "rb"))
  11. train_data = data['train_data']
  12. train_labels = data['train_labels']
  13. test1 = data['test1']
  14. test2 = data['test2']
  15. words = data['words']
  16.  
  17. n = 18 # Select the 18th sample
  18. pixels = train_data[n, :]
  19. plt.matshow(np.reshape(pixels, (30, 30), order='F'), cmap=cm.gray)
  20.  
  21.  
  22. def classify(train, train_labels, test):
  23. """Nearest neighbour classification
  24.  
  25. train - matrix of training data (one sample per row)
  26. train_labels - corresponding training data labels
  27. test - matrix of samples to classify
  28.  
  29. returns: labels - vector of test data labels
  30. """
  31. x = np.dot(test, train.transpose())
  32. modtest = np.sqrt(np.sum(test*test, axis=1))
  33. modtrain = np.sqrt(np.sum(train*train, axis=1))
  34. dist = x / np.outer(modtest, modtrain.transpose()) # cosine distance
  35. nearest = np.argmax(dist, axis=1)
  36. labels = train_labels[nearest]
  37. return labels
  38.  
  39.  
  40. # for x in range(0, 225):
  41. # for x in range(0, 899):
  42. # line_of_image = (np.reshape((current_thirty_by_thirty[n, :]), (1, 1), order='F'), cm.gray)
  43. # n = x # Select the xth sample
  44. # pixels = train_data[n, :]
  45. # current_thirty_by_thirty = (np.reshape(pixels, (30, 30), order='F'), cm.gray)
  46.  
  47.  
  48. import matplotlib.pyplot as plt
  49. import matplotlib.cm as cm
  50. import numpy as np
  51. #%matplotlib inline
  52.  
  53. # for x in range(0, 1):
  54. # n = x # Select the 18th sample
  55. # pixels = train_data[n, :]
  56. # plt.matshow(np.reshape(pixels, (30, 30), order='F'), cmap=cm.gray)
  57. # print (train_data[n, :])
  58.  
  59. def getCharacterImage(n):
  60. pixels = train_data[n, :]
  61. # plt.matshow(np.reshape(pixels, (30, 30), order='F'), cmap=cm.gray)
  62.  
  63. characterArray = np.zeros(225)
  64.  
  65. for i in range(0,225):
  66. characterArray[i] = getCharacterImage(i)
  67.  
  68. print(characterArray)
  69.  
  70.  
  71.  
  72.  
  73.  
  74. '''
  75. zero_array = [[]]
  76. width = 15
  77. heigth = 15
  78. zero_array = [[0 for x in range(width)] for y in range(height)]
  79.  
  80. for i in range(0,15):
  81. for j in range(0,15):
  82. zero_array[i][j] = "1"
  83.  
  84. print(zero_array)
  85.  
  86. '''
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. # Use first 599 samples of train_data for training
  95. train1_data = train_data[0:599, :]
  96. train1_labels = train_labels[0:599]
  97.  
  98. # Use last 100 samples of train_data for testing
  99. test1_data = train_data[599:, :]
  100. test1_labels = train_labels[599:]
  101.  
  102. # Count how many samples in the test set
  103. n_test = test1_labels.shape[0]
  104.  
  105. # Classify the test set to get an array of guessed labels
  106. test1_guessed = classify(train1_data, train1_labels, test1_data)
  107.  
  108. # Compare guesses against true labels and compute percent correct
  109. pcor = np.sum(test1_guessed == test1_labels) * 100.0 / n_test
  110.  
  111. print(pcor) # This should give 92% correct
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement