Created
November 6, 2017 17:06
-
-
Save 64lines/ca47a00663f844a9aeb05c86cc1613a9 to your computer and use it in GitHub Desktop.
[PYTHON][SKLEARN] Measuring Accuracy KNeighbors Classifier Predictions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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