CodeBlanks Logo Icon CodeBlanks

What is classification?

Classification is the task of assigning data to one of predefined classes.

What is classification?

Classification predicts which of a set of predefined classes an example belongs to: is the e-mail spam, is the tumour benign, which digit is in the photo. The output is not a number but a label.

from sklearn.linear_model import LogisticRegression
X = [[1], [2], [3], [7], [8], [9]]
y = ["düşük", "düşük", "düşük", "yüksek", "yüksek", "yüksek"]
m = LogisticRegression().fit(X, y)
print(m.predict([[2.5], [7.5]]))   # ['düşük' 'yüksek']

Labels can be text; scikit-learn keeps the classes sorted in the classes_ attribute.

Logistic regression

Despite its name, logistic regression is a classifier. It squeezes a linear score between 0 and 1 with the sigmoid function and interprets the result as a probability:

def sigmoid(z):
    return 1 / (1 + np.exp(-z))
print(sigmoid(0))    # 0.5   decision boundary
print(sigmoid(4))    # 0.98
print(sigmoid(-4))   # 0.018

If the probability is above 0.5 the positive class is chosen. It is a fast, interpretable and strong baseline model.

Practice Sigmoid

Write the sigmoid(z) function with np.exp. Print the results for np.array([-2, 0, 2]) with np.round(..., 3).

Starter code

model.py
import numpy as np

# buraya yaz

Sample solution

model.py
import numpy as np

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

print(np.round(sigmoid(np.array([-2, 0, 2])), 3))

Expected output

[0.119 0.5   0.881]

Questions and answers

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

1 Which is a classification output?

  • Sales count 340
  • "approved" / "rejected" Correct answer
  • Tomorrow's temperature 21.3
  • House price 1.2 million

Explanation: It is a choice from a limited label set; the others are continuous numbers.

2 Create the logistic regression classifier.

Question
m = ____()
Answer
m = LogisticRegression()

Explanation: LogisticRegression lives in sklearn.linear_model and does classification.

3 Match the concepts.

Term Meaning
label Class name
sigmoid Squeezes into 0-1
0.5 Default decision threshold
classes_ Sorted classes

All lessons in Beginner

13 Project: Classification

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