# This example use the digits dataset to illustrate how to use the
# KNeighborsClassifier and spliting the data to training set and testing set
# Outcome: the predicted digits using the testing data set

# Import necessary modules
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split

# Load the digits dataset
digits = datasets.load_digits()

# Create feature and target arrays
X = digits.data
y = digits.target

# Split into training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                    test_size=0.2,
                                                    random_state=42,
                                                    stratify=y)

# Create a k-NN classifier with 7 neighbors: knn
knn = KNeighborsClassifier(n_neighbors=7)

# Fit the classifier to the training data
knn.fit(X_train, y_train)

# Print the accuracy
print('score: {}'.format(knn.score(X_test, y_test)))

# predict the result of testing data
print(knn.predict(X_test))
