0Pricing
Machine Learning Academy · Ders

Temel Modeller: DummyClassifier'ı Her Zaman Geçin

DummyClassifier'ı asgari temel model olarak oluşturun ve yararlı kabul edilmeden önce her gerçek modelin onu geçmesi gerektiğini doğrulayın.

Temel Modeller: DummyClassifier'ı Her Zaman Geçin, CoddyKit'te ücretsiz bir Machine Learning Academy dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Machine Learning Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Machine Learning Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Why You Need a Baseline

When you report that your model achieves 92% accuracy, that number is meaningless without context. Is 92% impressive or disappointing? It depends entirely on what the simplest possible strategy would achieve on the same problem.

A baseline model is the floor that every real model must beat to be considered useful. Without a baseline, you might celebrate 92% accuracy on a dataset where a model that always predicts 'not fraud' would achieve 95% accuracy — which means your fancy ML model is actually worse than doing nothing.

The DummyClassifier: scikit-learn's Baseline Tool

Scikit-learn provides DummyClassifier as a minimal classifier that makes predictions using simple rules completely ignorant of the input features. It is the formal tool for establishing a baseline before any real modelling begins.

DummyClassifier is not a joke or placeholder — it is a rigorous sanity check. If your real model cannot beat the DummyClassifier, something is fundamentally wrong: your features may not be predictive, your pipeline has a bug, or the problem is harder than expected.

from sklearn.dummy import DummyClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Most frequent baseline: always predicts the majority class
dummy = DummyClassifier(strategy='most_frequent')
dummy.fit(X_train, y_train)
baseline_acc = dummy.score(X_test, y_test)
print(f'Baseline accuracy (always predict majority class): {baseline_acc:.3f}')

DummyClassifier Strategies

DummyClassifier supports several baseline strategies:

  • most_frequent: always predicts the class that appears most often in training. Best for imbalanced datasets.
  • stratified: randomly predicts each class with the probability equal to its training frequency. Maintains class distribution.
  • uniform: randomly predicts each class with equal probability. Useful when all classes are equally represented.
  • constant: always predicts a specified constant class. Use to test what happens if you always predict the positive class.
from sklearn.dummy import DummyClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
import numpy as np

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

for strategy in ['most_frequent', 'stratified', 'uniform', 'prior']:
    dummy = DummyClassifier(strategy=strategy, random_state=42)
    dummy.fit(X_train, y_train)
    acc = dummy.score(X_test, y_test)
    print(f'strategy={strategy}: accuracy={acc:.3f}')

The Accuracy Paradox in Action

The accuracy paradox is most dramatic on imbalanced datasets. Consider credit card fraud where 99% of transactions are legitimate. The most_frequent DummyClassifier predicts 'not fraud' for every transaction and achieves 99% accuracy. A real ML model scoring 97% accuracy would look worse than this baseline by accuracy — even if it correctly identifies most of the actual fraud cases.

This is why the DummyClassifier baseline must be evaluated with the same metric you will use to evaluate your real model. For imbalanced problems, use F1, precision, or recall — not accuracy.

from sklearn.dummy import DummyClassifier
from sklearn.metrics import f1_score, accuracy_score
import numpy as np

# Simulate 99% negative class (not fraud)
np.random.seed(42)
n = 10000
y_true = np.array([0]*9900 + [1]*100)
X_fake = np.random.randn(n, 5)  # random features (not predictive)

# Baseline always predicts not-fraud
dummy = DummyClassifier(strategy='most_frequent')
dummy.fit(X_fake, y_true)
y_pred_dummy = dummy.predict(X_fake)

print(f'Dummy Accuracy: {accuracy_score(y_true, y_pred_dummy):.3f}')  # 0.990
print(f'Dummy F1-score: {f1_score(y_true, y_pred_dummy):.3f}')         # 0.000
print('Dummy catches 0 fraud cases despite 99% accuracy!')

DummyRegressor: Baseline for Regression

For regression problems, scikit-learn provides DummyRegressor with strategies:

  • mean: always predicts the training set mean. This is the standard baseline — R² measures how much better your model is than simply predicting the mean.
  • median: always predicts the training set median. More robust to outliers.
  • quantile: always predicts a specific quantile of the training target.
  • constant: always predicts a specified constant.

A regression model with R² below 0 is actually worse than always predicting the mean — a clear sign something went wrong in training.

from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split

X, y = fetch_california_housing(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

dummy_reg = DummyRegressor(strategy='mean')
dummy_reg.fit(X_train, y_train)
y_pred_d = dummy_reg.predict(X_test)

print(f'DummyRegressor MAE: {mean_absolute_error(y_test, y_pred_d):.3f}')
print(f'DummyRegressor R2:  {r2_score(y_test, y_pred_d):.3f}')  # exactly 0.0

Your Model vs the Baseline

Now compare your real model against the baseline using the same metric. The improvement over the baseline tells you how much value your ML model adds. This comparison should be the first result you report in any ML project.

If the improvement is small (e.g., real model achieves F1=0.65 vs baseline F1=0.60), ask whether the complexity and cost of the ML model is worth a 5% improvement. Sometimes a simple rule-based system is cheaper and sufficiently accurate.

from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.metrics import f1_score
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Baseline
dummy = DummyClassifier(strategy='most_frequent')
dummy.fit(X_train, y_train)
baseline_f1 = f1_score(y_test, dummy.predict(X_test))

# Real model
pipeline = Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression(max_iter=1000))])
pipeline.fit(X_train, y_train)
real_f1 = f1_score(y_test, pipeline.predict(X_test))

print(f'Baseline F1: {baseline_f1:.3f}')
print(f'Real model F1: {real_f1:.3f}')
print(f'Improvement over baseline: {real_f1 - baseline_f1:.3f}')

Human-Level Performance as a Second Baseline

For many real-world problems, a human-level performance benchmark is more relevant than a dummy baseline. For example, a radiologist's error rate on detecting tumours, a human spam screener's accuracy, or an expert appraiser's house price estimation error.

Human performance provides a ceiling: if your model matches or exceeds human performance, you have solved the problem. If your model is far below human performance, there is room to grow. If your model is very close to human performance, improving it further may require extraordinary effort for diminishing returns.

Previous System as a Baseline

In industry, the most relevant baseline is usually the existing system your model is replacing. If the current production system uses hand-crafted rules, it is the target to beat. If a previous ML model has been deployed, its production metrics are your baseline.

When reporting results to stakeholders, always compare to the current system, not just a DummyClassifier. Business value comes from improvement over the status quo. A 2% improvement over the current system may be worth millions of dollars in reduced fraud losses, even if it seems small in absolute terms.

Baseline for Multi-Class Classification

For multi-class problems, the DummyClassifier baseline depends on class balance. With K balanced classes, the most_frequent strategy achieves 1/K accuracy. With imbalanced classes, it achieves the proportion of the most frequent class.

For metrics like macro-averaged F1 (which weights all classes equally), a random classifier will score much lower on rare classes. Always compare per-class metrics between your real model and the baseline to understand where the improvement comes from.

from sklearn.dummy import DummyClassifier
from sklearn.metrics import classification_report
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

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=42)

dummy = DummyClassifier(strategy='most_frequent')
dummy.fit(X_train, y_train)
y_pred = dummy.predict(X_test)

print('DummyClassifier (most_frequent) on Iris:')
print(classification_report(y_test, y_pred,
      target_names=['setosa', 'versicolor', 'virginica']))
print('Notice: only the majority class has non-zero precision/recall')

Zero-Rule Classifier: The Simplest Baseline

Even simpler than the DummyClassifier is the Zero-Rule (ZeroR) classifier — a common baseline in data science competitions. For classification, ZeroR always predicts the majority class. For regression, it always predicts the mean. ZeroR represents the absolute minimum that any model must beat to be useful.

If you cannot beat ZeroR, your features contain no information about the target variable. This diagnostic is fast and free to compute and should always be the first step in any new ML project before you spend time on complex models.

import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# ZeroR: manually compute
majority_class = np.bincount(y_train).argmax()
y_pred_zeror = np.full(len(y_test), majority_class)

print(f'Majority class in training: {majority_class}')
print(f'ZeroR Accuracy: {accuracy_score(y_test, y_pred_zeror):.3f}')
print(f'ZeroR F1 (pos): {f1_score(y_test, y_pred_zeror):.3f}')

Bagging Baselines in Your ML Projects

A complete baseline evaluation workflow for any ML project:

  1. Compute the DummyClassifier (most_frequent) or DummyRegressor (mean) baseline.
  2. If available, compute the current system baseline using its predictions on your test set.
  3. Research human-level performance from benchmarks or literature.
  4. Train your first simple ML model (logistic regression or a shallow decision tree) and compare all three.
  5. Only proceed to complex models if the simple model beats the baseline by a meaningful margin — complexity should be motivated by measurable improvement.
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_breast_cancer
import numpy as np

X, y = load_breast_cancer(return_X_y=True)

models = {
    'DummyClassifier': DummyClassifier(strategy='most_frequent'),
    'LogisticRegression': Pipeline([('sc', StandardScaler()), ('clf', LogisticRegression(max_iter=1000))]),
    'DecisionTree(d=5)': DecisionTreeClassifier(max_depth=5),
    'RandomForest': RandomForestClassifier(random_state=42),
}

for name, model in models.items():
    score = cross_val_score(model, X, y, cv=5, scoring='f1').mean()
    print(f'{name}: F1={score:.3f}')

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

In this lesson you learned: DummyClassifier establishes the floor of performance that every real model must exceed to provide value, accuracy baselines are misleading for imbalanced datasets — always use the same metric for baseline and real model comparison, and a complete baseline includes the trivial classifier, the current production system, and human-level performance when available. Next up we begin the Data Preprocessing Pipeline course, starting with systematic strategies for handling missing values using imputation techniques.

Sıkça Sorulan Sorular

“Temel Modeller: DummyClassifier'ı Her Zaman Geçin” dersi ücretsiz mi?

Evet — “Temel Modeller: DummyClassifier'ı Her Zaman Geçin” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Machine Learning Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Machine Learning Academy kursu toplamda 4 dersten oluşur.

“Temel Modeller: DummyClassifier'ı Her Zaman Geçin” dersinde ne öğreneceğim?

DummyClassifier'ı asgari temel model olarak oluşturun ve yararlı kabul edilmeden önce her gerçek modelin onu geçmesi gerektiğini doğrulayın. Machine Learning Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Machine Learning Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Machine Learning Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Temel Modeller: DummyClassifier'ı Her Zaman Geçin” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Machine Learning Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Machine Learning Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Eğitim Verileriyle Değerlendirme Yapamamanızın Nedeni
  2. train_test_split: Oranlar, Tohumlar ve Tabakalı Bölme
  3. Yanlılık-Varyans Ödünleşimi: Yetersiz ve Aşırı Uydurma
  4. Temel Modeller: DummyClassifier'ı Her Zaman Geçin
← Machine Learning Academy Sayfasına Dön