- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
The statsmodels library in Python provides a powerful way to fit statistical models and make predictions. The .predict() method is used to generate predictions based on a fitted model.
Example: Predicting with OLS Regression
import statsmodels.api as smimport pandas as pd# Create a datasetdata = pd.DataFrame({'hours': [1, 2, 2, 4, 2, 1, 5, 4, 2, 4],'exams': [1, 3, 3, 5, 2, 2, 1, 1, 0, 3],'score': [76, 78, 85, 88, 72, 69, 94, 94, 88, 92]})# Define predictors and response variableX = data[['hours', 'exams']]X = sm.add_constant(X) # Add constant for intercepty = data['score']# Fit the modelmodel = sm.OLS(y, X).fit()# Create new data for predictionnew_data = pd.DataFrame({'hours': [3, 5], 'exams': [2, 4]})new_data = sm.add_constant(new_data)# Make predictionspredictions = model.predict(new_data)print(predictions)Copied!✕CopyOutput:
0 82.1234561 90.987654dtype: float64Copied!✕CopyKey Points:
Prediction (out of sample) - statsmodels 0.15.0 (+853)
Using formulas can make both estimation and prediction a lot easier. We use the I to indicate use of the Identity transform. Ie., we do not want any expansion magic from using **2. Now we only …
See results only from statsmodels.orgInstalling Statsmodels
Installing statsmodels The easiest way to …
Getting Started
Getting started This very simple case …
How to Make Predictions Using Regression Model in Statsmodels
Aug 30, 2022 · This tutorial explains how to use a regression model fit using statsmodels to make predictions on new observations, including an example.
Python Statsmodels predict () Explained - PyTutorial
Jan 23, 2025 · In this example, we first fit a linear regression model using sm.OLS (). Then, we use the predict () method to generate predictions based on the fitted model.
Linear Regression in Python using Statsmodels
Jul 23, 2025 · In this article, we will discuss how to use statsmodels using Linear Regression in Python. Linear regression analysis is a statistical technique for predicting the value of one variable (dependent variable) …
Statsmodels Linear Regression: A Guide to Statistical Modeling
Nov 29, 2025 · Unlike scikit-learn, which optimizes for prediction, statsmodels gives you the statistical framework to understand relationships in your data. Let’s work through linear …
Searches you might like
Introduction to Regression with statsmodels in …
You’ll then learn how to fit simple linear regression models with numeric and categorical explanatory variables, and how to describe the relationship between the response and explanatory variables using model coefficients. …
How to run R-style linear regressions in Python the …
Nov 1, 2025 · If you have experience with R or want a quick way to generate a regression with statsmodels using a pandas DataFrame, you can use R-style formulas. First, you need to import statsmodels and its formula API: …
statmodels_regression_in_python.ipynb - Colab
statmodels is a Python module that provides classes and functions for the estimation of many different statistical models, as well as for conducting statistical tests, and statistical data...
Linear Regression in Python: A Guide to Predictive …
Mar 12, 2025 · Linear regression can be implemented in Python using different approaches. I'll walk you through three common methods: manual calculations with NumPy, detailed statistical modeling with statsmodels, …
A Quick Guide to Statistical Modeling in Python …
Apr 30, 2025 · It complements libraries like NumPy, SciPy, and pandas by providing tools for estimation, inference, and prediction in statistics and econometrics. This guide walks you through the most...