Skip to content

Instantly share code, notes, and snippets.

@64lines
Created November 6, 2017 17:06
Show Gist options
  • Select an option

  • Save 64lines/ca47a00663f844a9aeb05c86cc1613a9 to your computer and use it in GitHub Desktop.

Select an option

Save 64lines/ca47a00663f844a9aeb05c86cc1613a9 to your computer and use it in GitHub Desktop.
[PYTHON][SKLEARN] Measuring Accuracy KNeighbors Classifier Predictions
# Import necessary modules
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
# 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(knn.score(X_test, y_test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment