Supervised Learning with scikit-learn
George Boorman
Core Curriculum Manager, DataCamp
Predict the label of a data point by
Looking at the k
closest labeled data points
Taking a majority vote
from sklearn.neighbors import KNeighborsClassifier
X = churn_df[["total_day_charge", "total_eve_charge"]].values y = churn_df["churn"].values
print(X.shape, y.shape)
(3333, 2), (3333,)
knn = KNeighborsClassifier(n_neighbors=15)
knn.fit(X, y)
X_new = np.array([[56.8, 17.5], [24.4, 24.1], [50.1, 10.9]])
print(X_new.shape)
(3, 2)
predictions = knn.predict(X_new)
print('Predictions: {}'.format(predictions))
Predictions: [1 0 0]
Supervised Learning with scikit-learn