DDC-48: Least squares in statsmodels
A data challenge a day helps you master machine learning
About these daily data challenges
Each post is an exercise that helps you learn about data in Python.
Try to solve the exercise before checking my solution at the bottom of the post 🤓
You can share your solution or visualization in the comments!
Today’s challenge
This challenge follows from the previous challenge. Create a variable x as whole numbers from zero to nine, and variable y as those numbers squared. Then use the OLS function from statsmodels.api to fit a model that predicts y from x. Use sm.add_constant to include an intercept term to variable x.
Fit the model and print out the summary as below.
.
.
.
.
Scroll down for the solution…
.
.
.
.
.
.
.
.
keep scrolling!
.
.
.
.
import numpy as np
import statsmodels.api as sm
N = 10
X = sm.add_constant(np.arange(N))
y = np.arange(N)**2
print(sm.OLS(y,X).fit().summary())



Cool!