Most introductions to machine learning stop at "the model achieved 0.87 AUC" and never show what happens next. But the gap between a trained model in a notebook and a model serving real predictions is where most beginners get stuck. This guide walks the whole path - data to deployed API - using a realistic problem (predicting whether a customer will make a purchase) and the tools that actually run production ML: pandas, scikit-learn, XGBoost, and FastAPI. None of it requires deep learning. Most useful predictive work doesn't.
The problem
The task: given a customer's history and behavior, predict the probability they'll purchase. It's a binary classification problem with a clear business use - prioritize outreach, personalize offers, forecast demand. Framing matters: you're predicting a probability, not a yes/no, because the threshold that turns a probability into an action is a business decision, not a modeling one.
Step 1: prepare the data
Real data is messy. Before any modeling, handle missing values deliberately (drop, fill, or flag - each says something different), fix types, and split into train and test sets before you do anything else, so the test set stays a true holdout. The most common beginner mistake is leaking information from the future or the test set into training; guarding against it starts here.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42)
Step 2: engineer features
This is where most of the real gains live - not in the algorithm. Turn raw behavior into signals the model can use: recency (days since last purchase), frequency (orders in the last 90 days), monetary value, and ratios that capture trend. A well-engineered feature often beats a fancier model on the same data. Fit any transformations on the training set only, then apply them to the test set, to avoid leakage.
Step 3: train a model
Start simple to get a baseline - logistic regression is fast and interpretable - then reach for gradient boosting, which wins on most tabular problems:
from xgboost import XGBClassifier
model = XGBClassifier(n_estimators=300, max_depth=5,
learning_rate=0.05, subsample=0.8)
model.fit(X_train, y_train)
If a simple model already does the job, you may be done - complexity is a cost, not a goal.
Step 4: tune, carefully
Hyperparameter tuning helps, but use cross-validation so you're not tuning to a single lucky split, and keep the test set untouched until the very end. Modest, validated improvements beat chasing decimal points that won't survive contact with new data.
Step 5: evaluate beyond AUC
A single accuracy or AUC number hides what matters. Look at precision and recall at the threshold you'll actually use, the confusion matrix (which errors are you making, and which are costly?), and whether the predicted probabilities are calibrated - when the model says 70%, does it happen about 70% of the time? For an imbalanced problem like purchase prediction, accuracy is especially misleading; a model that predicts "no purchase" for everyone can look accurate and be useless.
Step 6: save the model
Persist the trained model and the feature-engineering steps together, so serving applies exactly the same transformations as training. A model saved without its preprocessing is a bug waiting to happen.
import joblib
joblib.dump({"model": model, "features": feature_list}, "model.joblib")
Step 7: serve predictions over an API
FastAPI turns the saved model into a service other systems can call:
from fastapi import FastAPI
import joblib
app = FastAPI()
bundle = joblib.load("model.joblib")
@app.post("/predict")
def predict(features: dict):
proba = bundle["model"].predict_proba([build_row(features)])[0][1]
return {"purchase_probability": float(proba)}
This is the step that turns a notebook artifact into something a product can use - and it's the step most tutorials skip.
The takeaway
Building a useful predictive model is mostly unglamorous: understand the data, engineer features that capture real behavior, pick the simplest model that works, evaluate it honestly against the decision it informs, and serve it with its preprocessing intact. The algorithms are the easy part. The discipline around them - no leakage, the right metric, reproducible serving - is what makes a model something other people can depend on. Start with this loop on a real problem, and the fundamentals will carry you far further than any single fancy technique.




Leave a Comment