CodeBlanks Logo Icon CodeBlanks

Summary - Classification

In this group you learned classification, accuracy, and confusion matrix.

Summary: classification

In this group:

  • Classification predicts labels; logistic regression produces probabilities with sigmoid.
  • In a binary problem predict_proba[:, 1] is the positive probability; 0.5 is only the default threshold.
  • With many classes each probability row sums to 1; OvR extends binary models.
  • Accuracy misleads on imbalanced data; DummyClassifier gives a baseline.
  • The confusion matrix separates TP, TN, FP and FN errors.

Practice Classification report

Split Iris with test_size=0.3, random_state=0, stratify=y and train a LogisticRegression(max_iter=1000). Print the test accuracy with round(..., 3), then the confusion matrix.

Starter code

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix

X, y = load_iris(return_X_y=True)

# buraya yaz

Sample solution

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix

X, y = load_iris(return_X_y=True)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
m = LogisticRegression(max_iter=1000).fit(X_train, y_train)
p = m.predict(X_test)
print(round(accuracy_score(y_test, p), 3))
print(confusion_matrix(y_test, p))

Expected output

1.0
[[15  0  0]
 [ 0 15  0]
 [ 0  0 15]]

Questions and answers

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

1 What should the first comparison be to show that a model is worthwhile?

  • A baseline that predicts the most frequent class Correct answer
  • Training accuracy
  • Another dataset
  • No comparison is needed

Explanation: The model must clearly beat a baseline that learns nothing.

2 Compute the confusion matrix.

Question
cm = ____(y_test, y_pred)
Answer
cm = confusion_matrix(y_test, y_pred)

Explanation: confusion_matrix rows are true classes, columns predicted ones.

3 Match the tools.

Term Meaning
LogisticRegression Probabilistic classifier
DummyClassifier Baseline
accuracy_score Share correct
confusion_matrix Error breakdown

All lessons in Beginner

13 Project: Classification

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