Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
- # License: BSD 3 clause
- # Import datasets, classifiers and performance metrics
- from sklearn import datasets, svm, metrics
- # The digits dataset
- digits = datasets.load_digits()
- # To apply a classifier on this data, we need to flatten the image, to
- # turn the data in a (samples, feature) matrix:
- n_samples = len(digits.images)
- data = digits.images.reshape((n_samples, -1))
- # Create a classifier: a support vector classifier
- classifier = svm.SVC(gamma=0.001)
- # We learn the digits on the first half of the digits
- classifier.fit(data[:n_samples // 2], digits.target[:n_samples // 2])
- # Now predict the value of the digit on the second half:
- expected = digits.target[n_samples // 2:]
- predicted = classifier.predict(data[n_samples // 2:])
- print("Classification report for classifier %s:\n%s\n"
- % (classifier, metrics.classification_report(expected, predicted)))
- print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement