MNT use train_test_spkit in example
@@ -20,6 +20,7 @@ import matplotlib.pyplot as plt
# Import datasets, classifiers and performance metrics
from sklearn import datasets, svm, metrics
+from sklearn.model_selection import train_test_split
# The digits dataset
digits = datasets.load_digits()
@@ -45,16 +46,19 @@ data = digits.images.reshape((n_samples, -1))
# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)
+# Split data into train and test subsets
+X_train, X_test, y_train, y_test = train_test_split(
+ data, digits.target, test_size=0.5, shuffle=False)
+
# We learn the digits on the first half of the digits
-classifier.fit(data[:n_samples // 2], digits.target[:n_samples // 2])
+classifier.fit(X_train, y_train)
# Now predict the value of the digit on the second half:
-expected = digits.target[n_samples // 2:]
-predicted = classifier.predict(data[n_samples // 2:])
+predicted = classifier.predict(X_test)
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))
+ % (classifier, metrics.classification_report(y_test, predicted)))
+print("Confusion matrix:\n%s" % metrics.confusion_matrix(y_test, predicted))
images_and_predictions = list(zip(digits.images[n_samples // 2:], predicted))
for index, (image, prediction) in enumerate(images_and_predictions[:4]):
