CodeBlanks Logo Icon CodeBlanks

Prediction

Make predictions with the model.

Prediction and probability

The trained pipeline takes new examples directly with raw measurements. predict_proba shows how confident the decision is:

p = model.predict(X_test[:5])
print(p)
print(model.predict_proba(X_test[:2]).round(2))

In KNN the probabilities are the shares of neighbour votes; with 5 neighbours the values are multiples of 0.2.

Finding wrong predictions

With a boolean mask the indices of wrongly predicted examples are found; then by examining those examples' probabilities you learn where the model is undecided:

tahmin = model.predict(X_test)
yanlis = np.where(tahmin != y_test)[0]
print(yanlis)
print(model.predict_proba(X_test[yanlis]).round(2))

Most errors are usually between two neighbouring classes; the confusion matrix confirms this.

Practice Inspect the errors

Get the predictions for the test set. Print the number of wrong predictions, then the true classes of the wrongly predicted examples (y_test[yanlis]).

Starter code

model.py
import numpy as np
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5)).fit(X_train, y_train)

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
model = make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5)).fit(X_train, y_train)

tahmin = model.predict(X_test)
yanlis = np.where(tahmin != y_test)[0]
print(len(yanlis))
print(y_test[yanlis])

Expected output

3
[1 2 1]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 With a 5-neighbour KNN, which can't be a class's probability?

  • 0.5 Correct answer
  • 0.4
  • 0.6
  • 1.0

Explanation: The probability has the form k/5: 0, 0.2, 0.4, 0.6, 0.8, 1.0.

2 Get the indices of the wrong predictions.

Question
yanlis = np.where(tahmin ____ y_test)[0]
Answer
yanlis = np.where(tahmin != y_test)[0]

Explanation: != produces an element-wise inequality mask.

3 Match the expressions.

Term Meaning
model.predict Class decision
model.predict_proba Neighbour vote shares
tahmin != y_test Error mask
np.where(...)[0] Error indices

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation