Let us put together the code we had used in the class for image classification using digits.
import matplotlib.pyplot as plt
from sklearn import datasets, svm, metrics
from sklearn.linear_model import LogisticRegression
digits = datasets.load_digits()
n_samples = len(digits.images)
data = digits.images.reshape((n_samples, -1))
# Create a classifier
classifier = LogisticRegression()
# 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("Accuracy: %s\n" % (metrics.accuracy_score(expected, predicted)))
The accuracy using Logistic Regression is 91.7%.
Write a function called solution()
which
- accepts the parameters
X_train_digits
andy_train_digits
datasets (available in your environment) for training, and - Use SVC algorithm
- returns the trained model as output
- We'll create
X_train_digits
,y_train_digits
,X_test
andy_test
X_train_digits
andy_train_digits
will be available in student's environmentX_test
andy_test
will be available to us to evaluate the output from student's function