Python program to find out best fit line in Linear Regresion by Predicting Y

Making predictions on Y to draw line

Our prediction function outputs an estimate of sales based on advertising and our current values for Weight and Bias.
Weight: the coefficient for the Radio independent variable. In machine learning we call coefficients weights.
X: Advertisement here the independent variable. In machine learning we call these variables features.
Bias: the intercept where our line intercepts the y-axis. In machine learning we can call intercepts bias. Bias offsets all predictions that we make. Our algorithm will try to learn the correct values for Weight and Bias. By the end of our training, our equation will approximate the line of best fit.

Here sales are depend on advertisement.

Here X is advertisement and Y is Sales. Sample data in csv below.

Sales for linear regression

Python program to fit the the line by predicting sales
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

plt.rcParams['figure.figsize'] = (12.0, 9.0)

data = pd.read_csv('sales.csv')  # load data set

X = data['Advertising'].values.reshape(-1, 1)  # values converts it into a numpy array
Y = data['Sales'].values.reshape(-1, 1)  # -1 means that calculate the dimension of rows, but have 1 column
print (Y)

linear_regressor = LinearRegression()  # create object for the class, Y)  # perform linear regression

plt.scatter(X, Y)
plt.plot(X, Y, color='green')

Y_pred = linear_regressor.predict(X)  # make predictions
plt.plot(X, Y_pred, color='red')
Here is the output.
Predicted linear regression

Predicted values are here
Predicted value