Sitemap
Taha Soylu

Taha Soylu

I am a cs student.I try to improve my skills and gain experience.I aim to post something from what I learned.Feel free to follow me if you wonder my journey.

Classification

5 min readSep 24, 2026

--

Classification is a supervised machine learning technique used to predict labels or categories from input data. It assigns each data point to a predefined class based on learned patterns.

  • Predict Categories: Determines the class of new data points.
  • Uses Labeled Data (Supervised): Trained on datasets where the correct class is known.
  • Common Examples: Spam vs non spam emails, diseased vs healthy patients.

Basic Performance Metrics

Accuracy

The ratio of correct predictions among all predictions made by the model (both positive and negative).

Press enter or click to view image in full size
Accuracy Formula
  • When it is dangerous?: When classes are imbalanced. For example, if 95% of the data is negative, the model can achieve a 95% accuracy rate by directly labeling everyone as negative without learning anything. Therefore, it is not reliable on its own.

Precision

It represents how many of the predictions labeled as “Positive” by the model are “actually true”. We look at this metric when our goal is to minimize false alarms (FP).

Precision Formula

Recall / Sensitivity

It is what percentage of the actually positive cases are successfully caught by the model. We focus on this metric when our goal is to reduce missed cases (FN) to zero.

Recall Formula
  • Critical Scenario: Missing an actual patient while diagnosing cancer in the healthcare field, or missing a notorious criminal in criminal law (False Negative), can have fatal consequences. In these situations, giving false alarms is risked if necessary, and efforts are made to maximize the “recall” metric.

F1 Score

Precision and Recall metrics are like a seesaw; pulling one up usually brings the other down. The F1 score is the harmonic mean that shows the balance between these two metrics.

F1 Formula

ROC Curve

A ROC(Receiver Operating Characteristics) curve visualizes the proportion of true positives to true negatives. The chart plots the true positive rate against the true negative rate for each threshold used in model classification. The Area Under Curve (AUC) statistic arises from the ROC curve. AUC measures how likely a randomly selected positive has a higher confidence score than a random negative. AUC values range from 0 to 1. The former signifies that the model scores all negatives with higher probabilities than positive while 1 means that the model scores every positives with higher probability.

Get Taha Soylu’s stories in your inbox

Join Medium for free to get updates from this writer.

AUC measures a model’s degree of seperability. It tells how capable the model is of distinguishing between classes. Higher AUC means the model is better at ranking positive instances higher than negative instances.

AUC evaluates performance correctly regardless of class imbalance.

Calibration

Predicting probabilities are near the expected distribution then we called that as calibrate. The major problem lies when the algorithm does not calibrate so we need to adjust or calibrate the predicting probabilities such that it goes near to expected distribution.

from sklearn.calibration import CalibratedClassifierCV, CalibrationDisplay, FrozenEstimator
from sklearn.metrics import brier_score_loss

frozen_model = FrozenEstimator(lgb_model_shuffle) # Ensemble=False -> model zaten eğitilmiş, sen eğitim ile uğraşma

calibrated_lgb_model = CalibratedClassifierCV(
estimator=frozen_model,
method="isotonic",
)

print("Calibration started...")
calibrated_lgb_model.fit(X_val_calib, y_val_calib) # daha önce ayırmış olduğumuz set ile Calibration yap.
print("Calibration completed")

Importance of Calibration

  • Interpretability: Well calibrated probabilities can be directly interpreted as confidence levels.
  • Decision Making: Accurate probabilities are essential for decision making process that rely on risk assessment or cost sensitive decisions.
  • Comparison Across Models: Calibration helps in comparing the performance of different models more fairly.

Calibration of a classifier is a critical step in the field of machine learning that ensures the predicted probabilities of a model truly reflect the actual likelihood of an event. In simpler terms, it is about making sure that when your model says there is 70% chance of rain, it really does rain 70% of the time.

Calibration is particularly useful when dealing with imbalanced datasets , where some classes are much less frequent than others. Standart classifiers may be biased forwards the majority class, but calibration can help in providing more accurate probability estimates for the minority class.

Methods for Calibration

Temperature Scaling

It softens or sharpens the probabilities by dividing the raw mathematical scores produced by the model by a single “T” (Temperature) number. This method is the cure for massive deep learning and neural network projects built with Pytorch or TensorFlow. It is used to prevent artificial intelligences like ChatGPT from “hallucinating” or speaking too confidently. It is not suitable for the nature and mathematics of tree-based algorithms like XGBoost and LightGBM.

Platt Scaling (Calibration with Logistic Regression)

It takes the unbalanced scores produced by the model and confines them between 0 and 1 by fitting a soft curve resembling the letter “s” over them. Mostly used in vector models like SVM. This used if the dataset is small.

Isotonic Regression

It does not have a specific rule. It orders the probabilities from smallest to largest and corrects the data step by step (piecewise). It only considers a single condition: If the model’s raw score high, the calibrated actual risk (probability) must also be high or equal; it can never decrease. It is the best friend of trees. LightGBM, CatBoost and XGBoost inherently produce probabilities step by step, not like an “S” curve. Their working logic is the same as isotonic regression. It does not work on small data.

Some models are over confident

An overconfident model produces high confidence scores while not being right. Other models are under confident. They produce low confidence scores while being right. If model confidence scores are not calibrated, this can be misleading. Confidence scores are based on patterns learned from the training data. If data is imbalanced or biased , the confidence scores might not reflect the true probabilities in real world scenarious.

Brier Score

The Brier Score measures the accuracy of probabilistic predictions by computing the mean squared difference between predicted probabilities and actual outcomes.

Brier Score Formula

--

--

Taha Soylu
Taha Soylu