Overfitting and underfitting explain why a model might not work well on new data. They help us understand if the model learned too much or too little from the training data.
Overfitting and underfitting in ML Python
No specific code syntax; these are concepts to check during model training and evaluation.Overfitting means the model memorizes training data details and noise.
Underfitting means the model is too simple to capture the data patterns.
# Overfitting example from sklearn.tree import DecisionTreeClassifier model = DecisionTreeClassifier(max_depth=None) # very deep tree model.fit(X_train, y_train) train_score = model.score(X_train, y_train) test_score = model.score(X_test, y_test)
# Underfitting example from sklearn.tree import DecisionTreeClassifier model = DecisionTreeClassifier(max_depth=1) # very shallow tree model.fit(X_train, y_train) train_score = model.score(X_train, y_train) test_score = model.score(X_test, y_test)
This code trains two decision tree models on the iris dataset. One is very deep (likely overfitting), the other is very shallow (likely underfitting). It prints their accuracy on training and test data to show the difference.
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier # Load data iris = load_iris() X, y = iris.data, iris.target # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Overfitting model: very deep tree model_overfit = DecisionTreeClassifier(max_depth=None, random_state=42) model_overfit.fit(X_train, y_train) train_score_overfit = model_overfit.score(X_train, y_train) test_score_overfit = model_overfit.score(X_test, y_test) # Underfitting model: very shallow tree model_underfit = DecisionTreeClassifier(max_depth=1, random_state=42) model_underfit.fit(X_train, y_train) train_score_underfit = model_underfit.score(X_train, y_train) test_score_underfit = model_underfit.score(X_test, y_test) print(f"Overfitting model - Train accuracy: {train_score_overfit:.2f}") print(f"Overfitting model - Test accuracy: {test_score_overfit:.2f}") print(f"Underfitting model - Train accuracy: {train_score_underfit:.2f}") print(f"Underfitting model - Test accuracy: {test_score_underfit:.2f}")
Overfitting usually shows very high training accuracy but lower test accuracy.
Underfitting shows low accuracy on both training and test data.
Use techniques like cross-validation, pruning, or simpler models to avoid overfitting.
Overfitting means the model learns too much noise and details from training data.
Underfitting means the model is too simple and misses important patterns.
Good models balance learning enough to predict well on new data without memorizing training data.
Practice
overfitting mean in machine learning?Solution
Step 1: Understand overfitting definition
Overfitting happens when a model learns not only the true patterns but also the noise and random details in training data.Step 2: Match definition to options
The model learns noise and details too well, failing on new data describes this exactly, while others describe underfitting or unrelated concepts.Final Answer:
The model learns noise and details too well, failing on new data -> Option BQuick Check:
Overfitting = learning noise too well [OK]
- Confusing overfitting with underfitting
- Thinking overfitting means perfect prediction on new data
- Believing overfitting ignores training data
underfitting in a model?Solution
Step 1: Understand underfitting error patterns
Underfitting means the model is too simple, so it performs poorly on both training and test data, causing high errors.Step 2: Match error patterns to options
High training error and high test error shows high errors on both training and test sets, which matches underfitting.Final Answer:
High training error and high test error -> Option DQuick Check:
Underfitting = high error everywhere [OK]
- Confusing underfitting with overfitting error patterns
- Assuming low training error means no underfitting
- Ignoring test error when diagnosing underfitting
from sklearn.tree import DecisionTreeClassifier 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, random_state=42) model = DecisionTreeClassifier(max_depth=1) model.fit(X_train, y_train) train_score = model.score(X_train, y_train) test_score = model.score(X_test, y_test) print(round(train_score, 2), round(test_score, 2))What is the likely output?
Solution
Step 1: Understand model complexity and max_depth=1
A decision tree with max_depth=1 is very simple, likely underfitting the iris data, so training accuracy will be low.Step 2: Predict scores based on underfitting
Both training and test scores will be low and close, around 0.6, matching 0.60 0.58.Final Answer:
0.60 0.58 -> Option AQuick Check:
Simple model = low train and test scores [OK]
- Assuming max_depth=1 gives perfect accuracy
- Confusing training and test scores
- Ignoring model simplicity effect
from sklearn.tree import DecisionTreeClassifier 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, random_state=42) model = DecisionTreeClassifier(max_depth=10) model.fit(X_train, y_train) print(model.score(X_train, y_train)) print(model.score(X_test, y_test)) # To reduce overfitting, we set max_depth=1 model.max_depth = 1 print(model.score(X_train, y_train)) print(model.score(X_test, y_test))What is the error in this approach?
Solution
Step 1: Understand how max_depth affects model
max_depth is a parameter set when creating the model. Changing it after fitting does not change the trained model.Step 2: Identify mistake in code
Setting model.max_depth=1 after fitting does not reduce overfitting. The model must be recreated with max_depth=1 before fitting.Final Answer:
Changing max_depth after fitting does not affect the model -> Option AQuick Check:
Parameters must be set before fit() [OK]
- Thinking changing max_depth after fit changes model
- Believing fit() can only be called once
- Confusing parameter setting with method calls
Solution
Step 1: Identify techniques to reduce overfitting
Overfitting can be reduced by regularization (penalties to simplify model) and by increasing training data to help generalize better.Step 2: Evaluate options
Use regularization and increase training data size correctly combines regularization and more data. Other options either increase complexity or remove validation, which worsen overfitting.Final Answer:
Use regularization and increase training data size -> Option CQuick Check:
Regularization + more data = less overfitting [OK]
- Increasing complexity to fix overfitting
- Ignoring validation data importance
- Increasing learning rate without reason
Ready to prove it?
Pass the test and this pattern moves unknown
