Intercept and Starting Points
In this exercise, you will see the intercept and slope parameters in the context of modeling measurements taken of the volume of a solution contained in a large glass jug. The solution is composed of water, grains, sugars, and yeast. The total mass of both the solution and the glass container was also recorded, but the empty container mass was not noted.
Your job is to use the preloaded pandas DataFrame df
, with data columns volumes
and masses
, to build a linear model that relates the masses
(y-data) to the volumes
(x-data). The slope will be an estimate of the density (change in mass / change in volume) of the solution, and the intercept will be an estimate of the empty container weight (mass when volume=0).
Diese Übung ist Teil des Kurses
Introduction to Linear Modeling in Python
Anleitung zur Übung
- Import
ols()
fromstatsmodels
and use it to build a model fit to thedata=df
withformula = "masses ~ volumes"
. - Extract the intercept
a0
and the slopea1
with.params['Intercept']
and.params['volumes']
, respectively. - Print
a0
anda1
with physically meaningful names. - Print
model_fit()
and look for values matching the ones found above; look for row labelsIntercept
,volumes
, and a column labelcoef
.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Import ols from statsmodels, and fit a model to the data
from statsmodels.formula.api import ols
model_fit = ols(formula="____ ~ ____", data=____)
model_fit = model_fit.fit()
# Extract the model parameter values, and assign them to a0, a1
a0 = model_fit.params['____']
a1 = model_fit.params['____']
# Print model parameter values with meaningful names, and compare to summary()
print( "container_mass = {:0.4f}".format(____) )
print( "solution_density = {:0.4f}".format(____) )
print( model_fit.summary() )