Search code examples
pythonmachine-learningsvmmnist

Is SVM with the mnist dataset 100% accurate?


So I wrote a program using sklearn's svm.SVC module to learn the mnist dataset, for some reason whenever i calculate the accuracy its 100%. This seems too good to be true, is this expected?

from sklearn import datasets
from sklearn import svm

digits = datasets.load_digits()

clf = svm.SVC(gamma=0.001, C=100)
print(len(digits.data))
train_with_first = 50

x, y = digits.data[:-train_with_first], digits.target[:-train_with_first]
clf.fit(x,y)

print(digits.data[4])

num_corr = 0
num_total = 0

for pred in range(train_with_first-1):    
    prediction = clf.predict(digits.data[pred].reshape(1,-1))
    target = digits.target[pred]
    print(int(target))
    print(int(prediction))

    if int(target) == int(prediction):
        num_corr += 1 
    num_total += 1

print("Accuracy was: ", (float(num_corr)/num_total)*100)

I expect the accuracy to be less than 100%


Solution

  • It might be over-fitting because the prediction data you use is all in your training data, try to use train_test_split in sklearn to split training and testing set.

    Or you can change line 18 from

    for pred in range(train_with_first-1):

    to

    for pred in range(train_with_first,len(digits.data)):

    but use train_test_split is recommended.