Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 28 ยท Pitfalls and Production

Predict, Save and Deploy

Move a Statsmodels model into production: predict with intervals, shrink and pickle results, reload them, validate inputs, test on a holdout and wrap everything in a function.

A model that lives only in a notebook is a draft. Putting it to work means four steps: validate it on data it has not seen, save it, load it somewhere else and wrap it in a clean function with input checks. This lesson shows each step with Statsmodels.

Validate before you trust

Fit on one part of the data, score on another. The in-sample R-squared is optimistic; the out-of-sample error tells you what to expect from new cases. Report RMSE or MAE in the units of the outcome.

Python Holdout evaluation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The test RMSE should be close to the noise level (20 here) and far below the baseline. For time series, split by time instead of at random (Time Series Basics). For small data, use k-fold cross-validation with a loop like the one in Regularized Regression.

Prediction intervals in production

A single number hides risk. Return an interval too. get_prediction(new).summary_frame(alpha=0.1) gives a 90% interval for the mean (mean_ci_*) and for a new observation (obs_ci_*).

Python Point forecast plus 90% interval

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Save and load

Fitted results can be pickled. The results object keeps a copy of all training data, which can be large and may contain sensitive rows. res.remove_data() deletes the data arrays while keeping what is needed for predict. Statsmodels also provides res.save(path, remove_data=True) and sm.load(path). Both use pickle, so only load files you trust: loading a pickle can run arbitrary code.

Python Shrink, pickle and reload

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

After remove_data() you can still call predict with new data, params and summary(), but not things that need the original rows (residual plots, influence measures). If you need those, compute them before slimming.

Pickles depend on library versions. Record statsmodels, pandas and numpy versions with the file, and reload with the same versions. For long-term storage also keep the coefficients as plain JSON or CSV, which any language can read.

Export the model as plain numbers

A linear model is just coefficients. Saving res.params and the formula lets you score in any system (SQL, JavaScript, a spreadsheet) with no Python at all.

Python Score with coefficients only

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A production wrapper

Wrap the model in a function that validates input, applies business rules and returns a structured answer. Check types, ranges (do not extrapolate!), and missing values before calling predict.

Python A safe prediction function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Serving the function from a web framework such as Flask or Django is then a thin layer on top. Also plan to monitor the model: compare predictions with actual outcomes over time, and refit when the error drifts upward because the world changed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why evaluate on a holdout set?

    A model always fits its own training data better than future data.
  2. 2. What does res.remove_data() do?

    It trims the results before pickling; some diagnostics that need the original data are no longer available.
  3. 3. What is the main security risk of loading a pickled model?

    Pickle can run code during loading; never load files from untrusted sources.
  4. 4. Why validate inputs against the training range in a prediction function?

    The model has seen no evidence about behavior outside the observed range.

Summary

  • Validate on held-out data (by time for time series) and report out-of-sample error.
  • Return prediction intervals, not just point values.
  • remove_data() plus pickle (or plain JSON coefficients) stores a model; only load trusted pickles and track library versions.
  • Wrap prediction in a function with input checks and monitor performance after launch.

Next: Project: House Price Model