Applied Data Analysis in Python

In [1]:
import pandas as pd

data = pd.read_csv("https://milliams.com/courses/applied_data_analysis/linear.csv")
In [2]:
from sklearn.linear_model import LinearRegression

model = LinearRegression(fit_intercept=False)
X = data[["x"]]
y = data["y"]
model.fit(X, y)
Out[2]:
LinearRegression(fit_intercept=False)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
In [3]:
pred = pd.DataFrame({"x": [0, 10]})
pred["y"] = model.predict(pred)
In [4]:
import seaborn as sns

sns.relplot(data=data, x="x", y="y")
sns.lineplot(data=pred, x="x", y="y", c="red", linestyle=":")
Out[4]:
<Axes: xlabel='x', ylabel='y'>
No description has been provided for this image
In [5]:
print(" Model gradient: ", model.coef_[0])
print("Model intercept:", model.intercept_)
 Model gradient:  1.1985226874421444
Model intercept: 0.0