Skip to content

Instantly share code, notes, and snippets.

@64lines
Last active November 6, 2017 16:28
Show Gist options
  • Select an option

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

Select an option

Save 64lines/5600bf0a8e83c06aef0ba1161214439b to your computer and use it in GitHub Desktop.
[PYTHON][SKLEARN] KNeighbors Classifier Predictions
# 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