The classification challenge

Supervised Learning with scikit-learn

George Boorman

Core Curriculum Manager, DataCamp

Classifying labels of unseen data

  1. Build a model
  2. Model learns from the labeled data we pass to it
  3. Pass unlabeled data to the model as input
  4. Model predicts the labels of the unseen data

 

  • Labeled data = training data
Supervised Learning with scikit-learn

k-Nearest Neighbors

  • Predict the label of a data point by

    • Looking at the k closest labeled data points

    • Taking a majority vote

Supervised Learning with scikit-learn

k-Nearest Neighbors

scatter plot with observations in blue and red, and a new observation in black

Supervised Learning with scikit-learn

k-Nearest Neighbors

radius around the three observations nearest to the black dot

Supervised Learning with scikit-learn

k-Nearest Neighbors

radius around the five observations nearest to the black dot

Supervised Learning with scikit-learn

KNN Intuition

scatterplot of total evening charge versus total day charge, where observations are colored in blue if they have churned and red if they have not churned

Supervised Learning with scikit-learn

KNN Intuition

churn scatterplot with a decision boundary splitting observations by whether KNN predicts they will churn or not

Supervised Learning with scikit-learn

Using scikit-learn to fit a classifier

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)
Supervised Learning with scikit-learn

Predicting on unlabeled data

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

Let's practice!

Supervised Learning with scikit-learn

Preparing Video For Download...