惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
Docker
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
美团技术团队
爱范儿
爱范儿
V
V2EX
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
博客园 - 司徒正美
博客园 - 叶小钗
S
SegmentFault 最新的问题
量子位
S
Secure Thoughts
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
L
LINUX DO - 最新话题
罗磊的独立博客
SecWiki News
SecWiki News
雷峰网
雷峰网
Recent Announcements
Recent Announcements
V2EX - 技术
V2EX - 技术
T
Tailwind CSS Blog
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
云风的 BLOG
云风的 BLOG
Schneier on Security
Schneier on Security
T
The Blog of Author Tim Ferriss
IT之家
IT之家
博客园 - 聂微东
腾讯CDC
N
News | PayPal Newsroom
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
aimingoo的专栏
aimingoo的专栏
Webroot Blog
Webroot Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
53. Overfitting: When Your Model Is Too Good at Being Wrong
Akhilesh · 2026-05-05 · via DEV Community

Series: How Machines Learn: A Complete Guide from Zero to AI Engineer
Phase 6: Machine Learning (The Core)


You trained your model. It got 99% on training data. You tested it on new data. It got 61%.

That gap is the problem. And it has a name: overfitting.

This is one of the most important concepts in all of machine learning. If you don't understand it, you'll keep building models that look great in your notebook and fail in the real world.


What You'll Learn Here

  • What overfitting actually means in plain terms
  • What underfitting is and why both are bad
  • The bias-variance tradeoff (explained without scary math)
  • How to detect overfitting with a learning curve
  • Practical ways to fix it with real code

The Overfitting Story

Imagine you're studying for a history exam. Instead of understanding why events happened, you memorize every single detail from the textbook. Every date, every name, every quote.

On the practice test, you score 100%. It's the exact same questions from the book.

On the real exam, the teacher rephrases the questions slightly. You freeze. You memorized the exact words, not the concepts. You fail.

Your ML model does the same thing when it overfits. It memorizes the training data so perfectly that it picks up on noise, random quirks, and flukes in that specific dataset. When it sees new data, those memorized quirks don't exist, and the model is lost.


Overfitting vs Underfitting

There are two ways a model can go wrong.

Underfitting: The model didn't learn enough. It's too simple. It misses the real pattern even in the training data.

Overfitting: The model learned too much. It's too complex. It memorized the training data including all its noise.

Good fit: The model learned the actual pattern and can apply it to new data.

Think of it like this. You're trying to draw a line through data points on a graph.

  • Underfit: You draw a flat horizontal line. It's too simple. It misses the trend.
  • Overfit: You draw a line that zigzags through every single point exactly. It matches training data perfectly but has nothing to do with the real pattern.
  • Good fit: You draw a smooth curve that captures the actual trend without chasing every outlier.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline

# Create some data with a true pattern + noise
np.random.seed(42)
X = np.sort(np.random.rand(30, 1) * 10, axis=0)
y = np.sin(X).ravel() + np.random.normal(0, 0.3, X.shape[0])

# Three models: underfit, good fit, overfit
degrees = [1, 3, 15]
titles  = ['Underfit (degree=1)', 'Good Fit (degree=3)', 'Overfit (degree=15)']

fig, axes = plt.subplots(1, 3, figsize=(15, 4))
X_plot = np.linspace(0, 10, 300).reshape(-1, 1)

for ax, degree, title in zip(axes, degrees, titles):
    model = Pipeline([
        ('poly', PolynomialFeatures(degree=degree)),
        ('linear', LinearRegression())
    ])
    model.fit(X, y)
    y_plot = model.predict(X_plot)

    ax.scatter(X, y, color='gray', alpha=0.6, label='Data')
    ax.plot(X_plot, y_plot, color='blue', linewidth=2)
    ax.set_title(title)
    ax.set_ylim(-2, 2)

plt.tight_layout()
plt.savefig('overfit_comparison.png', dpi=100)
plt.show()

Enter fullscreen mode Exit fullscreen mode

Run this and look at all three plots. The degree=15 model passes through almost every point perfectly. It looks impressive. But ask it to predict on new data and it goes wild.


Detecting Overfitting: The Training vs Test Gap

The easiest way to catch overfitting is to compare training accuracy and test accuracy.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

data = load_breast_cancer()
X, y = data.data, data.target

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

# Try different tree depths
print(f"{'Depth':<8} {'Train Acc':<12} {'Test Acc':<12} {'Gap':<8}")
print("-" * 42)

for depth in [1, 2, 3, 5, 10, 20, None]:
    model = DecisionTreeClassifier(max_depth=depth, random_state=42)
    model.fit(X_train, y_train)

    train_acc = accuracy_score(y_train, model.predict(X_train))
    test_acc  = accuracy_score(y_test,  model.predict(X_test))
    gap       = train_acc - test_acc

    depth_label = str(depth) if depth else 'None'
    print(f"{depth_label:<8} {train_acc:.3f}       {test_acc:.3f}       {gap:.3f}")

Enter fullscreen mode Exit fullscreen mode

Output:

Depth    Train Acc    Test Acc     Gap     
------------------------------------------
1        0.904        0.895        0.009
2        0.940        0.930        0.010
3        0.962        0.947        0.015
5        0.979        0.947        0.032
10       0.998        0.930        0.068
20       1.000        0.912        0.088
None     1.000        0.912        0.088

Enter fullscreen mode Exit fullscreen mode

Look at that pattern. As the tree gets deeper:

  • Training accuracy goes up (model memorizes more)
  • Test accuracy goes up then starts dropping
  • The gap keeps growing

When you see a big gap between training and test accuracy, that's overfitting.


The Bias-Variance Tradeoff

This is the theory behind all of it. It sounds complicated but it's actually a simple idea.

Bias is how wrong your model is on average. A high-bias model makes strong assumptions and misses the real pattern. It underfits.

Variance is how much your model changes when you train it on different data. A high-variance model is sensitive to every little detail in the training data. It overfits.

The tradeoff: reducing one usually increases the other.

  • Simple models: high bias, low variance (underfit)
  • Complex models: low bias, high variance (overfit)
  • The sweet spot: enough complexity to learn the pattern, not so much that you memorize noise
Bias-Variance Tradeoff:

Error
 |
 |  \          /
 |   \        /   <- Total Error
 |    \      /
 |     \    /
 |  Variance \  /
 |            \/
 |         Bias
 |________________________
        Model Complexity

Sweet spot is at the bottom of the total error curve.

Enter fullscreen mode Exit fullscreen mode

You can't eliminate both bias and variance at the same time. The goal is to find the right balance for your specific problem.


Learning Curves: Visualizing the Problem

A learning curve shows you how training and test performance change as you add more training data. It's one of the most useful diagnostic tools in ML.

from sklearn.model_selection import learning_curve
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_breast_cancer
import numpy as np
import matplotlib.pyplot as plt

data = load_breast_cancer()
X, y = data.data, data.target

def plot_learning_curve(model, title):
    train_sizes, train_scores, test_scores = learning_curve(
        model, X, y,
        train_sizes=np.linspace(0.1, 1.0, 10),
        cv=5,
        scoring='accuracy'
    )

    train_mean = train_scores.mean(axis=1)
    test_mean  = test_scores.mean(axis=1)

    plt.figure(figsize=(8, 5))
    plt.plot(train_sizes, train_mean, label='Training accuracy', color='blue')
    plt.plot(train_sizes, test_mean,  label='Test accuracy',     color='orange')
    plt.fill_between(train_sizes,
                     train_scores.mean(axis=1) - train_scores.std(axis=1),
                     train_scores.mean(axis=1) + train_scores.std(axis=1),
                     alpha=0.1, color='blue')
    plt.xlabel('Training set size')
    plt.ylabel('Accuracy')
    plt.title(title)
    plt.legend()
    plt.tight_layout()
    plt.savefig(f'learning_curve_{title[:5]}.png')
    plt.show()

# Overfit model (deep tree)
plot_learning_curve(
    DecisionTreeClassifier(max_depth=None, random_state=42),
    'Overfit Model - Deep Tree'
)

# Better model (shallow tree)
plot_learning_curve(
    DecisionTreeClassifier(max_depth=3, random_state=42),
    'Better Model - Shallow Tree'
)

Enter fullscreen mode Exit fullscreen mode

Reading the learning curve:

  • Overfit: training accuracy is high, test accuracy is much lower. Big gap between the two lines.
  • Underfit: both lines are low and close together. Adding more data doesn't help much.
  • Good fit: the two lines are close and both are high.

How to Fix Overfitting

Once you detect it, here's how to fix it.

Fix 1: Get more training data

The most effective fix when you can get it. More data makes it harder for the model to memorize noise because there's just too much to memorize.

Fix 2: Simplify the model

Use fewer features, reduce tree depth, reduce polynomial degree. A simpler model can't memorize as much.

# Instead of this (overfitting)
model = DecisionTreeClassifier(max_depth=None)

# Try this
model = DecisionTreeClassifier(max_depth=3)

Enter fullscreen mode Exit fullscreen mode

Fix 3: Regularization

Regularization adds a penalty for complexity directly into the model's training. The model learns to prefer simpler solutions.

from sklearn.linear_model import Ridge, Lasso, LogisticRegression

# Ridge regression: penalizes large coefficients (L2 regularization)
ridge = Ridge(alpha=1.0)  # higher alpha = more regularization

# Lasso regression: can shrink some coefficients to zero (L1)
lasso = Lasso(alpha=0.1)

# Logistic regression with regularization (C is inverse of alpha)
lr = LogisticRegression(C=0.1)  # lower C = more regularization

Enter fullscreen mode Exit fullscreen mode

Fix 4: Cross-validation

Instead of relying on one train/test split, use cross-validation to get a more reliable estimate and catch overfitting early.

Fix 5: Pruning (for trees)

Decision trees can be pruned after training to remove branches that don't add much value.

# Cost complexity pruning
model = DecisionTreeClassifier(ccp_alpha=0.01, random_state=42)
model.fit(X_train, y_train)

Enter fullscreen mode Exit fullscreen mode


A Side-by-Side Comparison

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.tree import DecisionTreeClassifier

data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

models = {
    'Overfit  (no limit)': DecisionTreeClassifier(max_depth=None, random_state=42),
    'Better   (depth=5)':  DecisionTreeClassifier(max_depth=5,    random_state=42),
    'Underfit (depth=1)':  DecisionTreeClassifier(max_depth=1,    random_state=42),
}

print(f"{'Model':<25} {'Train':<8} {'Test':<8} {'CV Mean':<10}")
print("-" * 55)

for name, model in models.items():
    model.fit(X_train, y_train)
    train_acc = model.score(X_train, y_train)
    test_acc  = model.score(X_test,  y_test)
    cv_acc    = cross_val_score(model, X_train, y_train, cv=5).mean()
    print(f"{name:<25} {train_acc:.3f}    {test_acc:.3f}    {cv_acc:.3f}")

Enter fullscreen mode Exit fullscreen mode

Output:

Model                     Train    Test     CV Mean   
-------------------------------------------------------
Overfit  (no limit)       1.000    0.912    0.924     
Better   (depth=5)        0.984    0.956    0.953     
Underfit (depth=1)        0.904    0.895    0.901     

Enter fullscreen mode Exit fullscreen mode

The middle model has the best test and CV scores. That's the one you'd pick.


Quick Cheat Sheet

Sign What it means Fix
Train high, test low Overfitting Simplify model, regularize, more data
Train low, test low Underfitting More complexity, better features
Train high, test high Good fit Ship it
Big gap train vs test Overfitting Cross-validate, regularize
Both curves low on learning curve Underfitting More features or complex model

Practice Challenges

Level 1:
Run the depth comparison table on load_wine(). Find the depth where test accuracy peaks.

Level 2:
Plot learning curves for both an overfit and underfit version of a decision tree on the same dataset. See the difference visually.

Level 3:
Use Ridge regression on a regression dataset (load_diabetes()). Try alpha values of 0.01, 0.1, 1, 10, 100. Plot how train and test error change. Find the sweet spot.


References


Next up, Post 54: Linear Regression: Predicting Numbers From Patterns. We build the most fundamental ML model from scratch, understand the math behind it, and use scikit-learn to make real predictions.