liveBook · Manning

Linear regression

As explained in Starting Data Analytics with Generative AI and Python

Linear regression is a statistical modeling technique that estimates a linear relationship between a dependent variable and one or more independent variables.

Does a heavier or larger product cost more to ship? Linear regression helps turn that business question into an estimate of how freight value changes with product weight or volume. Start by deciding what each possible result would mean for the decision—not by hunting for an impressive number after fitting a model. section 3.4.2 section 3.2

Think of the fitted line as a compact description of a pattern in many orders. Freight value is the dependent variable; weight and volume are independent variables. Fit them separately to see each relationship, then together to see how much the two attributes explain in one model. section 3.4.2

A practical Python workflow is:

  • Merge order items with products on product ID, calculate volume from three dimensions, select the needed columns, and remove missing rows. section 3.4.2
  • Ask an AI assistant for code to fit the proposed comparisons, then inspect what it generated. The demonstrated models add an intercept and use statsmodels ordinary least squares. section 3.4.2
  • Plot the observations and fitted line with a 95% confidence band; compare the output with the underlying records and the question you planned to answer. section 3.4.2 section 3.2
Listing 3.5 Predicting product weight and volume influence on freight value
import pandas as pd
import numpy as np
import statsmodels.api as sm

# Merge the DataFrames
merged_df = df_order_items.merge(df_products, on="product_id", how="left")

# Calculate product volume
merged_df["product_volume"] = merged_df["product_length_cm"] * 
merged_df["product_height_cm"] * merged_df["product_width_cm"]

# Filter out the relevant columns
analysis_df = merged_df[
["freight_value", "product_weight_g", "product_volume"]]

# Remove rows with NaN values
analysis_df = analysis_df.dropna()

# Define the independent variables (weight and volume) 
and the dependent variable (freight value)
X = analysis_df[['product_weight_g', 'product_volume']]
y = analysis_df['freight_value']

# Add a constant for the intercept
X = sm.add_constant(X)

# Fit the model
model = sm.OLS(y, X).fit()

# Print the summary
print(model.summary())

Inspect the combined-model code: both product weight and calculated volume enter as independent variables, with freight value as the target. section 3.4.2

Which model explains more?

In the shipping example, the weight-only model reports R² = 0.37 and the volume-only model R² = 0.34; the combined model reports R² = 0.399. R² describes the share of variation in freight value explained by the tested variables. Combining attributes improves that figure, but leaves substantial variation unexplained. The separate models’ reported mean absolute errors are 6.89 and 7.41, respectively; check errors as well as R² rather than judging a model by one statistic. section 3.4.2 section 4.6

A result is not a business decision by itself. A high R² may be attractive, while a low value may still tell you something worth reporting. Revisit the records, data preparation, and intended decision before trusting a generated interpretation. The shown predictions are evaluated against the data used to fit the models; those figures alone do not establish performance on new orders. section 4.6 section 3.4.2 section 3.2

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through data preparation, OLS fitting, confidence-band plots, metrics, and interpretation without a mathematical derivation.
Examples
Compares weight-only, volume-only, and combined models of freight value using plots and reported error metrics.
What it assumes
Builds on a defined business question and cleaned product and order data; demonstrates the model-fitting steps.
What you can do after
Apply it via statsmodels to fit shipping-cost models and evaluate/understand their reported fit and errors.
How it connects
Connects ordinary least squares with confidence intervals, R-squared, prediction errors, and analysis design.

Also explained in

Compare how 22 books explain linear regression →

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.