Skip to content Skip to sidebar Skip to footer

R Abline() Equivalent In Python

I am trying to plot a Linear Regression onto a scatterplot in Python. In R I would simply do the following: Run OLS Linear Regresion fit_1 <- lm(medv ~ lstat) plot(medv ~ lstat)

Solution 1:

It can be done quite simply. In the below code, I use sklearn to fit the model and predict the values.

import pandas as pd
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt

model = LinearRegression()
model.fit(X,y)
predictions = model.predict(X)

plt.plot(X,y,'o')
# change here
plt.plot(X, predictions, '-')
plt.show()

enter image description here

Solution 2:

Try this:

plt.plot(Boston.lstat, fit_1.fittedvalues, 'r')

Post a Comment for "R Abline() Equivalent In Python"