Last active
November 6, 2017 16:28
-
-
Save 64lines/5600bf0a8e83c06aef0ba1161214439b to your computer and use it in GitHub Desktop.
[PYTHON][SKLEARN] 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 KNeighborsClassifier from sklearn.neighbors | |
| from sklearn.neighbors import KNeighborsClassifier | |
| # Create arrays for the features and the response variable | |
| y = df['party'].values | |
| X = df.drop('party', axis=1).values | |
| # Create a k-NN classifier with 6 neighbors: knn | |
| knn = KNeighborsClassifier(n_neighbors=6) | |
| # Fit the classifier to the data | |
| knn.fit(X, y) | |
| # Predict the labels for the training data X | |
| y_pred = knn.predict(X) | |
| # Predict and print the label for the new data point X_new | |
| new_prediction = knn.predict(X_new) | |
| print("Prediction: {}".format(new_prediction)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment