Introduction to Linear Modeling in Python
Jason Vestuto
Data Scientist
from sklearn.linear_model import LinearRegression
# Initialize a general model
model = LinearRegression(fit_intercept=True)
# Load and shape the data
x_raw, y_raw = load_data()
x_data = x_raw.reshape(len(y_raw),1)
y_data = y_raw.reshape(len(y_raw),1)
# Fit the model to the data
model_fit = model.fit(x_data, y_data)
# Extract the linear model parameters
intercept = model.intercept_[0]
slope = model.coef_[0,0]
# Use the model to make predictions
future_x = 2100
future_y = model.predict(future_x)
x, y = load_data()
df = pd.DataFrame(dict(times=x_data, distances=y_data))
fig = df.plot('times', 'distances')
model_fit = ols(formula="distances ~ times", data=df).fit()
a0 = model_fit.params['Intercept']
a1 = model_fit.params['times']
e0 = model_fit.bse['Intercept']
e1 = model_fit.bse['times']
intercept = a0
slope = a1
uncertainty_in_intercept = e0
uncertainty_in_slope = e1
Introduction to Linear Modeling in Python