liveBook · Manning

Overfitting

As explained in Starting Data Analytics with Generative AI and Python

Overfitting is a modeling failure in which a model fits noise or sample-specific variation rather than generalizable underlying patterns.

Overfitting occurs when a model fits noise or sample-specific variation instead of underlying patterns that generalize. In regression, too many parameters for the number of observations can create this failure, so adding variables does not automatically improve reliability. section 3.4.3

A review-score model

A model predicting customer review scores from delivery, product, and payment attributes should be treated cautiously when many predictors are included. A train/test workflow produced 0.43 accuracy and a confusion matrix with substantial misclassification, reinforcing the need to inspect results rather than assume the model is reliable. section 3.4.3

import pandas as pd
import mord
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, accuracy_score

# Prepare the data (assuming you have already merged necessary 
DataFrames into analysis_df)

# Convert categorical variables to numerical format if required
# (e.g., using pd.get_dummies() for product categories)

# Split the data into training and testing sets
X = analysis_df.drop("review_score", axis=1)
y = analysis_df["review_score"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, 
random_state=42)

# Fit the ordinal logistic regression model
model = mord.LogisticAT(alpha=0)
model.fit(X_train, y_train)

# Predict the review scores on the test data
y_pred = model.predict(X_test)

# Calculate the accuracy of the predictions
print("Accuracy:", accuracy_score(y_test, y_pred))

# Display the confusion matrix
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))

# Analyze the model's coefficients
print("Coefficients:\n", model.coef_)

Use the train/test prediction workflow to inspect accuracy and the confusion matrix before trusting a review-score model. section 3.4.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines the failure and its parameter-to-observation cause, but stops short of detailed mitigation procedures.
Examples
Uses a customer review-score model involving delivery, product, price, and payment attributes.
What it assumes
Assumes familiarity with regression, predictors, observations, and dependent variables.
What you can do after
Evaluate model reliability by limiting unnecessary predictors and checking held-out predictions, accuracy, and confusion matrices.
How it connects
Connects overfitting with regression, independent variables, observations, model parameters, and multicollinearity.

Also explained in

Compare how 24 books explain overfitting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.