Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. # # Laborator 3
  4.  
  5. # In[1]:
  6.  
  7.  
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10.  
  11.  
  12. # In[2]:
  13.  
  14. #change this path
  15. dataPath = "C:/Users/Student/Desktop/17lab3/data/data/"
  16.  
  17. #load train images
  18. train_images = np.loadtxt(dataPath + "train_images.txt")
  19. train_labels = np.loadtxt(dataPath + "train_labels.txt",'int8')
  20. print(train_images.shape)
  21. print(train_images.ndim)
  22. print(type(train_images[0,0]))
  23. print(train_images.size)
  24. print(train_images.nbytes)
  25.  
  26.  
  27. # In[3]:
  28.  
  29.  
  30. #plot the first 100 training images with their labels in a 10 x 10 subplot
  31. nbImages = 10
  32. plt.figure(figsize=(5,5))
  33. for i in range(nbImages**2):
  34. plt.subplot(nbImages,nbImages,i+1)
  35. plt.axis('off')
  36. plt.imshow(np.reshape(train_images[i,:],(28,28)),cmap = "gray")
  37. #plt.show()
  38. labels_nbImages = train_labels[:nbImages**2]
  39. #print(np.reshape(labels_nbImages,(nbImages,nbImages)))
  40.  
  41.  
  42. # In[4]:
  43.  
  44.  
  45. #load test images
  46. test_images = np.loadtxt(dataPath + "test_images.txt")
  47. test_labels = np.loadtxt(dataPath + "test_labels.txt",'int8')
  48. #plot the first 100 testing images with their labels in a 10 x 10 subplot
  49. nbImages = 10
  50. plt.figure(figsize=(5,5))
  51. for i in range(nbImages**2):
  52. plt.subplot(nbImages,nbImages,i+1)
  53. plt.axis('off')
  54. plt.imshow(np.reshape(test_images[i,:],(28,28)),cmap = "gray")
  55. #plt.show()
  56. labels_nbImages = test_labels[:nbImages**2]
  57. #print(np.reshape(labels_nbImages,(nbImages,nbImages)))
  58.  
  59.  
  60. # In[ ]:
  61.  
  62.  
  63. #do 1-NN, 3-NN, 5-NN, 7 -NN for the first test image
  64. #plot the neighbors
  65.  
  66. v=test_images[0,:]
  67. distanta=np.sqrt(np.sum((v-train_images)**2,axis=1))
  68. indici=distanta.argsort();
  69. print("Predictia mea este", train_labels[indici[0]])
  70.  
  71. #16:00
  72.  
  73. def clasifica( elem, train, labels, k=1, dist='l1'):
  74.  
  75. if dist == 'l2':
  76. distanta = np.sqrt(np.sum((elem-train)**2,axis=1))
  77. elif dist =='l1':
  78. distanta = np.sum(np.abs(elem-train),axis=1)
  79. else:
  80. print('eroare')
  81. #return -1
  82.  
  83. indici = distanta.argsort()
  84. vecini=labels[indici[0:k]]
  85. contor=np.bincount(vecini)
  86.  
  87. return contor.argmax()
  88.  
  89. def acuratete(p,y):
  90. p = np.array(p)
  91. y = np.array(y)
  92. return len(p[p==y])*100/len(y)
  93.  
  94. l,c = test_images.shape
  95.  
  96. for k in [1,3,5,7]:
  97. predictii=[]
  98. for i in range (0,l):
  99. elem = test_images[i,:]
  100. p = clasifica(elem,train_images,train_labels,k)
  101. predictii.append(p)
  102. print(acuratete(predictii,test_labels))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement