liveBook · Manning

Product attribute

As explained in Starting Data Analytics with Generative AI and Python

A product attribute is a measurable or descriptive characteristic of a product, such as its weight, dimensions, or volume.

A product attribute is a recorded or calculated characteristic of a product. Weight and dimensions become useful when the business question is concrete: do heavier or larger products tend to cost more to ship? In this analysis, freight value is the outcome, while product weight and volume are attributes tested as possible explanations. section 3.4.2

To prepare attributes for that question:

  • Join order items to products using product_id, so each freight value can be examined alongside the product’s measurements. section 3.4.2
  • Calculate volume by multiplying product length, height, and width. Keep freight value, weight, and volume, then remove rows missing those values. section 3.4.2
  • Fit separate linear regressions for weight and volume, then fit a model using both attributes. Plot the relationships before interpreting the model results. section 3.4.2
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import summary_table

# 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()

The preparation code shows where measured dimensions become a calculated attribute and how product records are matched to shipping records. section 3.4.2

Weight, volume, and freight value

In the reported results, the weight-only model has R² = 0.37 and mean absolute error = 6.89; the volume-only model has R² = 0.34 and mean absolute error = 7.41. A model using both attributes has R² = 0.399. Weight performs somewhat better than volume alone, and combining them explains more variation than either single-attribute model. That still leaves substantial variation unexplained; the results do not establish that changing a product’s weight or dimensions would, by itself, cause a particular freight charge. section 3.4.2 section 4.6

Treat an attribute as a useful analytical input, not an automatic answer. Check that measurements and joins are sensible, inspect the scatterplots, and compare errors as well as R². A result becomes valuable when its limits are clear enough to inform a shipping-cost question without overstating what the attributes explain. section 3.4.2 section 4.6

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Works through attribute preparation, separate and combined regressions, plots, and metrics; stops short of establishing a causal effect.
Examples
Compares weight-only, volume-only, and combined models against recorded freight values.
What it assumes
Assumes order-item and product records with a shared product_id; demonstrates how to derive volume from dimensions.
What you can do after
Apply it via pandas and statsmodels to prepare product measurements and evaluate their association with freight value.
How it connects
Connects product dimensions and volume to data joins, missing values, linear regression, confidence intervals, and R².

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.