Learn practical skills, build real-world projects, and advance your career

Linear Regression using sklearn

from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
n = np.random.randint(-100,4,100).reshape(-1,1) # some random numeber to add as noise
X = np.arange(100) # for X axis points
X = X.reshape(-1,1) # reshape as row,1 col
Y = 5 + 3 * X + n
Y = Y.reshape(-1,1) # reshape as row,1 col

Scatter plot of all points

plt.figure(figsize=(10,5))
plt.scatter(X,Y,color="k",label="Original data")
plt.legend()
plt.title("Linear Regression Example..")
plt.xlabel("X-Independent Variable")
plt.ylabel("Y-Dependent Variable")
plt.show()
Notebook Image