0
0
ML Pythonprogramming~5 mins

Linear regression concept in ML Python

Choose your learning style9 modes available
Introduction
Linear regression helps us find a straight line that best fits data points, so we can predict one value from another easily.
Predicting house prices based on size.
Estimating sales based on advertising budget.
Forecasting temperature changes over time.
Understanding how study hours affect exam scores.
Predicting weight from height.
Syntax
ML Python
y = b0 + b1 * x
y is the value we want to predict.
b0 is the intercept (where the line crosses the y-axis).
b1 is the slope (how much y changes when x changes).
Examples
Here, the intercept is 2 and the slope is 3. For each increase of 1 in x, y increases by 3.
ML Python
y = 2 + 3 * x
The intercept is 5 and the slope is -0.5, so y decreases by 0.5 for each increase of 1 in x.
ML Python
y = 5 - 0.5 * x
Sample Program
This code trains a linear regression model to predict exam scores based on hours studied. It prints the slope and intercept, then predicts the score for 6 hours.
ML Python
from sklearn.linear_model import LinearRegression
import numpy as np

# Sample data: hours studied vs exam score
X = np.array([[1], [2], [3], [4], [5]])  # hours
y = np.array([2, 4, 5, 4, 5])           # scores

# Create and train the model
model = LinearRegression()
model.fit(X, y)

# Print the slope and intercept
print(f"Slope (b1): {model.coef_[0]:.2f}")
print(f"Intercept (b0): {model.intercept_:.2f}")

# Predict score for 6 hours studied
predicted_score = model.predict([[6]])[0]
print(f"Predicted score for 6 hours: {predicted_score:.2f}")
OutputSuccess
Important Notes
Linear regression assumes a straight-line relationship between input and output.
It works best when data points roughly form a line.
Outliers can affect the line, so check your data carefully.
Summary
Linear regression finds a straight line to predict values.
It uses slope and intercept to describe the line.
Good for simple predictions with one input variable.