fork download
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3.  
  4. # Dataseti aldığım adres:
  5. # https://a...content-available-to-author-only...i.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits
  6.  
  7.  
  8. def load(path_test, path_train):
  9. #Kullanacağımız verileri yüklüyoruz
  10. with open(path_test, 'r') as f: testing = pd.read_csv(f)
  11. with open(path_train, 'r') as f: training = pd.read_csv(f)
  12.  
  13.  
  14. n_features = testing.shape[1]
  15.  
  16. X_test = testing.ix[:,:n_features-1]
  17. X_train = training.ix[:,:n_features-1]
  18. y_test = testing.ix[:,n_features-1:].values.ravel()
  19. y_train = training.ix[:,n_features-1:].values.ravel()
  20.  
  21.  
  22. return X_train, X_test, y_train, y_test
  23.  
  24.  
  25. def peekData(X_train):
  26. # The 'targets' or labels are stored in y. The 'samples' or data is stored in X
  27. print ("Peeking your data...")
  28. fig = plt.figure()
  29.  
  30. cnt = 0
  31. for col in range(5):
  32. for row in range(10):
  33. plt.subplot(5, 10, cnt + 1)
  34. plt.imshow(X_train.ix[cnt,:].reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
  35. plt.axis('off')
  36. cnt += 1
  37. fig.set_tight_layout(True)
  38. plt.show()
  39.  
  40.  
  41. def drawPredictions(X_train, X_test, y_train, y_test):
  42. fig = plt.figure()
  43.  
  44. # Make some guesses#Bazı tahminlerin yapılması
  45. y_guess = model.predict(X_test)
  46.  
  47.  
  48. num_rows = 10 #Test için kullanılacak satır sayısı
  49. num_cols = 5 #Test için kullanılacak sütun sayısı
  50.  
  51. index = 0
  52. for col in range(num_cols):
  53. for row in range(num_rows):
  54. plt.subplot(num_cols, num_rows, index + 1)
  55.  
  56. # 8x8 is the size of the image, 64 pixels #8x8 resimlerin boyutu,64 piksel olacak şekilde
  57. plt.imshow(X_test.ix[index,:].reshape(8,8), cmap=plt.cm.gray_r, interpolation='nearest')
  58.  
  59. # Doğru tahminlerin etiketleri yeşil renkte
  60. # Yanlış tahminlerin etiketleri kırmızı renkte
  61. fontcolor = 'g' if y_test[index] == y_guess[index] else 'r'
  62. plt.title('Label: %i' % y_guess[index], fontsize=6, color=fontcolor)
  63. plt.axis('off')
  64. index += 1
  65. fig.set_tight_layout(True)
  66. plt.show()
  67.  
  68.  
  69.  
  70. #
  71. # TODO: Pass in the file paths to the .tes and the .tra files
  72. X_train, X_test, y_train, y_test = load('C:\\Users\\Fatih\\Desktop\\Bitirme\\Optical-Recognition-of-Handwritten-Digits\\optdigits.test', 'C:\\Users\\Fatih\\Desktop\\Bitirme\\Optical-Recognition-of-Handwritten-Digits\\optdigits.train')
  73.  
  74.  
  75. from sklearn import svm
  76.  
  77. #
  78. # Get to know your data. It seems its already well organized in
  79. # [n_samples, n_features] form. Our dataset looks like (4389, 784).
  80. # Also your labels are already shaped as [n_samples].
  81. peekData(X_train)
  82.  
  83.  
  84. #
  85. # TODO: Create an SVC classifier. Leave C=1, but set gamma to 0.001
  86. # and set the kernel to linear. Then train the model on the training
  87. # data / labels:
  88. print( "Training SVC Classifier...")
  89. #
  90. # .. your code here ..
  91. from sklearn.svm import SVC
  92. model=SVC(kernel='rbf',C=1,gamma=0.001)
  93. model.fit(X_train,y_train)
  94.  
  95.  
  96.  
  97. # TODO: Calculate the score of your SVC against the testing data
  98. print ("Scoring SVC Classifier...")
  99. #
  100. # .. your code here ..
  101. score=model.score(X_test,y_test)
  102.  
  103. print( "Score:\n", score)
  104.  
  105.  
  106.  
  107. # Visual Confirmation of accuracy
  108. drawPredictions(X_train, X_test, y_train, y_test)
  109.  
  110.  
  111. #
  112. # TODO: Print out the TRUE value of the 1000th digit in the test set
  113. # By TRUE value, we mean, the actual provided label for that sample
  114. #
  115. # .. your code here ..
  116. true_1000th_test_value=X_test.loc[1000]
  117. print( "1000th test label: ", true_1000th_test_value)
  118. print(true_1000th_test_value)
  119.  
  120. #
  121. # TODO: Predict the value of the 1000th digit in the test set.
  122. # Was your model's prediction correct?
  123. # INFO: If you get a warning on your predict line, look at the
  124. # notes from the previous module's labs.
  125. #
  126. # .. your code here ..
  127. guess_1000th_test_value=model.predict(true_1000th_test_value)
  128. print ("1000th test prediction: ", guess_1000th_test_value)
  129.  
  130.  
  131. #
  132. # TODO: Use IMSHOW to display the 1000th test image, so you can
  133. # visually check if it was a hard image, or an easy image
  134. #
  135. # .. your code here ..
  136. true_1000th_test_value=true_1000th_test_value.reshape(8,8)
  137. plt.imshow(true_1000th_test_value,cmap=plt.cm.gray_r,interpolation='nearest')
  138. plt.show()
  139.  
  140.  
  141.  
  142. # your code goes here
Runtime error #stdin #stdout #stderr 0.04s 9232KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ImportError: No module named 'pandas'