Linear regression learns a linear relationship between input features and a numerical target. It is supervised learning because training examples include their target values.
The equation of the line is modelled as:
Loss Function:
To improve this fit, minimise the squared difference between prediction and target .
Cost Function:
Gradient Descent

- we apply gradient descent to minimize cost function
- we initialize weights and iteratively adjust them in the direction of steepest descent
At portion of the curve where (gradient is positive), when we increase , increases.
At portion of the curve where (gradient is negative), when we increase , decreases.
To decrease the cost function, we can apply:
where is the learning rate.
Linear regression predicts a continuous value, and the rest of the models used for labels are listed in Text Classification.
Code
Fit a numerical relationship
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([[1.0], [2.0], [3.0], [4.0]])
y = np.array([3.0, 5.0, 7.0, 9.0])
model = LinearRegression()
model.fit(X, y)
predictions = model.predict([[5.0], [6.0]])
print(model.coef_.round(2)) # [2.]
print(round(model.intercept_, 2)) # 1.0
print(predictions.round(2)) # [11. 13.]Read the data and results:
Xhas shape(4, 1): four samples with one feature eachyhas shape(4,): one numerical target per samplefitlearns a coefficient near2and an intercept near1predictaccepts two new rows and returns two numerical values- The learned rule is
prediction = 2 * input + 1
LinearRegression fits a least-squares solution. Its fit method performs that calculation internally; this class does not require a manual gradient-descent loop.