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]:
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]:
In [5]:
print(" Model gradient: ", model.coef_[0])
print("Model intercept:", model.intercept_)