<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Digit Classification using K-Neighbours and Logistic Regression
http
Step1:  What does our data look like?
This is how we represent a handwritten '0' character - values with a 0 are dark, and comparatively higher values are much lighter.
We have a . suffix, to indicate this is a floating point number, for more accuracy in computations.
Step2:  Extract our input data (X digits), our target output data (Y digits) and the number of samples we will process.
Step3:  Extract 90% of our available data as training data for the models.
Step4:  Extract 10% of our available data as test data, to check the level of accuracy for the models.
Step5:  Use the K-Neighbours algorithm to create a Classifier implementing the k-nearest neighbors vote.
Step6:  Use the Logistic Regression algorithm
Step7:  We can see from the outcomes KNN was better at predicting the target result, with ~96% accuracy.
<ASSISTANT_TASK:>
Python Code:
from sklearn import datasets, neighbors, linear_model
digits = datasets.load_digits() # Retrieves digits dataset from scikit-learn
print(digits['DESCR'])
Explanation: Digit Classification using K-Neighbours and Logistic Regression
http://scikit-learn.org/stable/auto_examples/exercises/plot_digits_classification_exercise.html#sphx-glr-auto-examples-exercises-plot-digits-classification-exercise-py
Scikit-Learn includes a number of datasets to practice with, and many machine learning algorithms.
End of explanation
digits['images'][0]
import matplotlib.pyplot as plt
plt.gray() 
plt.matshow(digits.images[0])
plt.matshow(digits.images[10])
plt.show() 
for i in range(0,10):
    plt.matshow(digits.images[i]) 
    
plt.show()
Explanation: What does our data look like?
This is how we represent a handwritten '0' character - values with a 0 are dark, and comparatively higher values are much lighter.
We have a . suffix, to indicate this is a floating point number, for more accuracy in computations.
End of explanation
X_digits = digits.data
X_digits
y_digits = digits.target
y_digits
n_samples = len(X_digits)
n_samples
Explanation: Extract our input data (X digits), our target output data (Y digits) and the number of samples we will process.
End of explanation
X_train = X_digits[:int(.9 * n_samples)]
X_train
y_train = y_digits[:int(.9 * n_samples)]
y_train
Explanation: Extract 90% of our available data as training data for the models.
End of explanation
X_test = X_digits[int(.9 * n_samples):]
y_test = y_digits[int(.9 * n_samples):]
X_test
Explanation: Extract 10% of our available data as test data, to check the level of accuracy for the models.
End of explanation
knn = neighbors.KNeighborsClassifier() # Retrieve the default K-Neighbours Classification algorithm
knn
fitting = knn.fit(X_train, y_train) # Train the algorithm on 90% of the samples
fitting
knn_score = fitting.score(X_test, y_test) # Score the algorithm on how well it fits the 10% of the data that was left out
print('KNN score: %f' % knn_score)
Explanation: Use the K-Neighbours algorithm to create a Classifier implementing the k-nearest neighbors vote.
End of explanation
logistic = linear_model.LogisticRegression()
logistic
log_regression_fitting = logistic.fit(X_train, y_train)
log_regression_fitting
log_regression_score = log_regression_fitting.score(X_test, y_test)
print('LogisticRegression score: %f' % log_regression_score)
Explanation: Use the Logistic Regression algorithm
End of explanation
print('KNN score: %f' % knn_score)
print('LGR score: %f' % log_regression_score)
Explanation: We can see from the outcomes KNN was better at predicting the target result, with ~96% accuracy.
End of explanation
<END_TASK>