Advertisement
Guest User

digits.py

a guest
Sep 27th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. # Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
  2. # License: BSD 3 clause
  3.  
  4. # Import datasets, classifiers and performance metrics
  5. from sklearn import datasets, svm, metrics
  6.  
  7. # The digits dataset
  8. digits = datasets.load_digits()
  9.  
  10. # To apply a classifier on this data, we need to flatten the image, to
  11. # turn the data in a (samples, feature) matrix:
  12. n_samples = len(digits.images)
  13. data = digits.images.reshape((n_samples, -1))
  14.  
  15. # Create a classifier: a support vector classifier
  16. classifier = svm.SVC(gamma=0.001)
  17.  
  18. # We learn the digits on the first half of the digits
  19. classifier.fit(data[:n_samples // 2], digits.target[:n_samples // 2])
  20.  
  21. # Now predict the value of the digit on the second half:
  22. expected = digits.target[n_samples // 2:]
  23. predicted = classifier.predict(data[n_samples // 2:])
  24.  
  25. print("Classification report for classifier %s:\n%s\n"
  26.       % (classifier, metrics.classification_report(expected, predicted)))
  27. print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement