Classifying Purchase Intention of Online Shoppers with Python

This tutorial predicts whether an online-shopping session ends in revenue. The UCI dataset contains 12,330 complete sessions, with a purchase rate of 15.5%. The updated workflow uses a fixed stratified split and fits scaling, one-hot encoding, and logistic regression entirely on training rows.

Train-only preprocessing
Numeric fields are standardized and categorical fields are one-hot encoded inside a ColumnTransformer. handle_unknown='ignore' makes unseen holdout categories safe. Balanced class weights make minority purchases more influential during fitting.
model = Pipeline([
('preprocessor', preprocessor),
('classifier', LogisticRegression(
class_weight='balanced', max_iter=2000, random_state=42
)),
])
Verified holdout results
The prevalence-only baseline scored ROC AUC 0.500. Logistic regression reached ROC AUC 0.898, average precision 0.630, and F1 0.609 at the default 0.5 threshold.

Permutation importance
Holdout permutation importance uses average precision, which is informative for this imbalanced target. The highest-ranked raw feature was PageValues. The ranking describes this model’s predictive dependence and is not causal.

Limitations
Rows represent sessions rather than verified unique people. A random holdout does not test seasonality or repeated visitors. Balanced weights and the 0.5 threshold create a particular intervention trade-off. A store should choose its threshold from contact costs, expected margin, and capacity, then validate on a later time period.



