0% found this document useful (0 votes)
7 views7 pages

Overfitting vs Underfitting in Python

The document discusses overfitting and underfitting in Python, detailing methods such as comparing training vs validation performance, learning curves, cross-validation, and bias-variance indicators. It includes code examples using libraries like sklearn to demonstrate model training, evaluation, and regularization checks. Key insights include identifying overfitting through accuracy discrepancies and using regularization to improve model performance.

Uploaded by

surendranfer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Overfitting vs Underfitting in Python

The document discusses overfitting and underfitting in Python, detailing methods such as comparing training vs validation performance, learning curves, cross-validation, and bias-variance indicators. It includes code examples using libraries like sklearn to demonstrate model training, evaluation, and regularization checks. Key insights include identifying overfitting through accuracy discrepancies and using regularization to improve model performance.

Uploaded by

surendranfer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Overfitting and Underfitting in Python

August 25, 2025

1 Overfiiting and Underfitting using Python


1. Compare Training vs Validation/Testing Performance
2. Learning Curves (Training vs Validation Error)
3. Cross Validation
4. Bias - Variance Indicators
5. Residual Analysis
6. Regularization Check

1.1 1. Compare Training vs Validation / Testing Performance


[6]: from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
from [Link] import RandomForestClassifier
from [Link] import load_digits

[8]: #Load Dataset


X, y = load_digits(return_X_y=True)

[10]: # Split Train / Val / Test


X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.3,␣
↪random_state=42)

X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.3,␣


↪random_state=42)

[12]: [Link]

[12]: (1797, 64)

[14]: [Link]

[14]: (1797,)

[16]: X_train.shape

[16]: (1257, 64)

[18]: X_temp.shape

1
[18]: (540, 64)

[20]: X_val.shape

[20]: (378, 64)

[22]: X_test.shape

[22]: (162, 64)

[24]: X

[24]: array([[ 0., 0., 5., …, 0., 0., 0.],


[ 0., 0., 0., …, 10., 0., 0.],
[ 0., 0., 0., …, 16., 9., 0.],
…,
[ 0., 0., 1., …, 6., 0., 0.],
[ 0., 0., 2., …, 12., 0., 0.],
[ 0., 0., 10., …, 12., 1., 0.]])

[26]: y

[26]: array([0, 1, 2, …, 8, 9, 8])

[28]: #Train Model


model = RandomForestClassifier()

[30]: [Link](X_train, y_train)

[30]: RandomForestClassifier()

[32]: y_pred_train = [Link](X_train)

[34]: y_pred_test = [Link](X_test)

[36]: y_pred_val = [Link](X_val)

[38]: train_accuracy = accuracy_score(y_train, y_pred_train)


val_accuracy = accuracy_score(y_val, y_pred_val)
test_accuracy = accuracy_score(y_test, y_pred_test)

[40]: print("Training Accuracy:", train_accuracy)

Training Accuracy: 1.0

[42]: print("Validation Accuracy:", val_accuracy)

Validation Accuracy: 0.9735449735449735

2
[46]: print("Test Accuracy:", test_accuracy)

Test Accuracy: 0.9691358024691358

[48]: # Compare
print("Training Accuracy:", train_accuracy)
print("Validation Accuracy:", val_accuracy)
print("Test Accuracy:", test_accuracy)

Training Accuracy: 1.0


Validation Accuracy: 0.9735449735449735
Test Accuracy: 0.9691358024691358

1.2 2. Learning Curves (Training vs Validation Error)


[52]: import numpy as np
import [Link] as plt
from sklearn.model_selection import learning_curve
from sklearn.linear_model import LogisticRegression

[54]: #Learning Curves


train_sizes, train_scores, val_scores = learning_curve(
LogisticRegression(max_iter=2000), X, y, cv=5, scoring = "accuracy",
train_sizes=[Link](0.1, 1.0, 10), n_jobs=-1
)

[56]: #Mean Errors


train_error = 1 - [Link](train_scores, axis=1)
val_error = 1 - [Link](val_scores, axis=1)

[58]: #Plot
[Link](train_sizes, train_error, 'o-', label="Training Error")
[Link](train_sizes, val_error, 'o-', label="Validation Error")
[Link]("Training Size")
[Link]("Error(1-Accuracy)")
[Link]("Learning Curves")
[Link]()
[Link](True)
[Link]()

3
1.3 3. Cross Validation
[61]: from sklearn.model_selection import cross_validate
cv_results = cross_validate(
LogisticRegression(max_iter=2000), X, y, cv=5,
return_train_score=True, scoring="accuracy"
)

[63]: print("Train Scores:", cv_results["train_score"])


print("Test Scores:", cv_results["test_score"])
print("Mean Train Accuracy:", cv_results["train_score"].mean())
print("Mean Val Accuracy:", cv_results["test_score"].mean())

Train Scores: [1. 1. 1. 1. 1.]


Test Scores: [0.92222222 0.87222222 0.94150418 0.94150418 0.89693593]
Mean Train Accuracy: 1.0
Mean Val Accuracy: 0.9148777468276075

4
1.4 4. Bias Variance Indicators
[66]: mean_train = cv_results['train_score'].mean()
mean_val = cv_results['test_score'].mean()

[68]: if mean_train < 0.7 and mean_val < 0.7:


print("High Bias - Underfitting")
elif mean_train > 0.9 and (mean_train - mean_val) > 0.1:
print("High Variance - Overfitting")
else:
print("Balanced - Good Fit")

Balanced - Good Fit

1.5 5. Residual Analysis (Regression Example)


[71]: import [Link] as plt
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error

[73]: #Train Test Split


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,␣
↪random_state=42)

[75]: #Fit Regression Model


reg = LinearRegression()

[77]: [Link](X_train, y_train)

[77]: LinearRegression()

[79]: #Predictions
y_pred_train = [Link](X_train)
y_pred_test = [Link](X_test)

[81]: #Residuals
residuals_train = y_train - y_pred_train
residuals_test = y_test - y_pred_test

[85]: # Plot Residuals


[Link](y_pred_train, residuals_train, label="Train", alpha=0.6)
[Link](y_pred_test, residuals_test, label="Test", alpha=0.6, color="red")
[Link](0, color="black", linestyle="--")
[Link]("Predicted Values")
[Link]("Residuals")
[Link]("Residual Analysis")
[Link]()
[Link](True)

5
[Link]()

Interpretation 1. Random Scattered Around 0 - Good Fit 2. Large Residuals in Test but Small in
Train - Overfitting 3. Systematic Pattern - Underfitting

1.6 6. Regularization Check


[89]: from sklearn.linear_model import LogisticRegression

[91]: # Without Regularization (C very large)


model_no_reg = LogisticRegression(C=1e6, max_iter=2000)
model_no_reg.fit(X_train, y_train)

[91]: LogisticRegression(C=1000000.0, max_iter=2000)

[93]: # With Stronger Regularization (C Small)


model_reg = LogisticRegression(C=0.01, max_iter=2000)
model_reg.fit(X_train, y_train)

[93]: LogisticRegression(C=0.01, max_iter=2000)

6
[97]: # Compare
print("No Reg - Train Acc:", accuracy_score(y_train, model_no_reg.
↪predict(X_train)))

print("No Reg - Val Acc:", accuracy_score(y_val, model_no_reg.predict(X_val)))

No Reg - Train Acc: 1.0


No Reg - Val Acc: 0.9682539682539683

[99]: print("With Reg - Train Acc:", accuracy_score(y_train, model_reg.


↪predict(X_train)))

print("No Reg - Val Acc:", accuracy_score(y_val, model_reg.predict(X_val)))

With Reg - Train Acc: 0.9920445505171042


No Reg - Val Acc: 0.9682539682539683
Interpretation: 1. If Validation Accuracy Improves when Adding Regularization - Model was
Overfitting 2. If Both Train and Val Accuracy Drops - Model May Be Underfitting Already

[ ]:

Common questions

Powered by AI

Residual analysis involves examining the differences between the observed and predicted values. In a good fit, residuals are randomly distributed around zero, while systematic patterns or very high residuals suggest problems. Overfitting is indicated when residuals are small on the training set but large on the test set, suggesting the model captured noise during training. Underfitting is suggested by a systematic pattern, possibly indicating that the model is too simple to capture underlying trends. The provided residual plots would help identify such patterns .

Regularization helps prevent overfitting by penalizing complex models, thus promoting simpler models with fewer parameters. The strength of regularization can be adjusted to balance bias and variance. Its impact is evaluated by comparing model performance with and without regularization. In the example provided, adding regularization slightly decreased training accuracy but stabilized validation accuracy, indicating it successfully mitigated overfitting without causing significant underfitting .

A systematic pattern in residual plots, such as a clear curve or non-random trend, suggests model underfitting. This happens when the model is too simple to capture the underlying data structure, leading to consistent prediction errors across similar input values. The model fails to generalize the complexities present in the data, as indicated by non-random residual distribution around predicted values. Such patterns require model complexity enhancement to achieve better fit .

Learning curves plot the training and validation errors as functions of the training set size, which helps diagnose model performance issues. A large gap between the training and validation errors indicates overfitting, as the model performs well on the training data but poorly on unseen data. If both errors are high, the model may be underfitting, suggesting a need for a more complex model or more features. In the example given, the mean errors are plotted to help visualize these potential issues and adjust the model accordingly .

A learning curve showing a small gap between training and validation errors but high error values suggests underfitting. Both training and validation sets are performing poorly, indicating that the model lacks the capacity to generalize well. This usually points to the need for a more complex model or additional features to adequately capture the data's underlying patterns. This scenario requires adjustments in model architecture or features to improve accuracy .

Comparing training vs validation/testing performance is critical for understanding overfitting and underfitting in machine learning models. Overfitting occurs when the model performs very well on the training set but poorly on unseen data, indicated by a high training accuracy and significantly lower validation/testing accuracy. Conversely, underfitting is when the model performs poorly on both training and validation/testing sets, usually due to model simplicity. In the provided example, training accuracy is 1.0 while validation and test accuracies are slightly lower, suggesting a potential overfitting .

Evaluating both mean train and validation accuracy in cross-validation helps assess the model's ability to generalize. High train accuracy with lower validation accuracy suggests overfitting, while very close values suggest a balanced model with good generalization. An overall low accuracy reveals underfitting, necessitating model complexity adjustments. In this scenario, both accuracies are high and close, indicating a balanced model with minimal bias/variance issues .

Altering logistic regression's regularization parameter, C, affects the model's complexity. A large C value reduces regularization, allowing the model to fit the training data better, potentially leading to overfitting, as seen with a train accuracy of 1.0. Conversely, a smaller C increases regularization strength, which reduces complexity, as shown by a train accuracy of 0.992. While test accuracy remains similar, adjusting C balances bias and variance, reducing overfitting without significantly increasing bias .

Cross-validation plays a vital role in understanding model bias and variance by dividing the data into multiple subsets, training the model on some subsets while testing on others. This process provides a more comprehensive insight into the model's performance by estimating its generalization ability. A high variance model will show significant performance variation across folds, while a high bias model will perform poorly on both training and validation data consistently. In the presented data, the model exhibits balanced performance with a mean train accuracy of 1.0 and a mean validation accuracy of 0.914, indicating a good fit without severe bias or variance issues .

A significant drop in validation accuracy when adding regularization is expected if the model is already underfitting. Regularization reduces model complexity, which can be detrimental if the model is too simple and needs more complexity to capture the underlying data pattern. Hence, adding regularization would exacerbate underfitting by further limiting model capacity, as potentially indicated when both train and validation accuracies drop .

You might also like