What is Regression in Machine Learning?
Lab-2
Introduction
• Regression is a supervised learning technique used
to predict a continuous numerical value.
• The value of the dependent variable are real.
Terminology
• Training set: the data used to train the model
• x: input variable (feature) Training set
• y : the target variable or the output variable
• m : the number of training example
Learning Algorithm
f
The line of best fit
• Let’s apply a Linear Regression model step by step to your Income →
Rent dataset.
Income rent
23000 9500
14000 5000
24000 10000
52500 18000
43750 16000
18000 6000
15000 5000
16000 6000
41500 18000
45000 17500
Graph income versus Data
The line of best fit
• Y=mx+c, where
Linear Regression
• Predict Rent (y) based on Income (X) using Linear Regression.
• Step 1: Import Required Libraries
import numpy as np
from sklearn.linear_model import LinearRegression
Step-2: Load the data
# Data
x = [Link]([23000,14000,24000,52500,43750,
18000,15000,16000,41500,45000]).reshape(-1,1)
y = [Link]([9500,5000,10000,18000,16000,
6000,5000,6000,18000,17500])
# Model
model = LinearRegression()
[Link](x, y)
Training dataset result
print("Slope (m):", model.coef_[0])
print("Intercept (b):", model.intercept_)
print("R² Score:", [Link](X, y))
Learned Linear Regression Equation
• From the trained model:
• Slope (m) ≈ 0.378
• Intercept (b) ≈ 47.16
Final model:
Predict rent for income = 30,000
predicted_rent = [Link]([[30000]])
print(predicted_rent)
import numpy as np
from sklearn.linear_model import LinearRegression
# Data
x = [Link]([5, 15, 25, 35, 45, 55]).reshape(-1, 1)
y = [Link]([5, 20, 14, 32, 22, 38])
# Create model
model = LinearRegression()
# Train model
[Link](x, y)
# Output results
print("Slope (m):", model.coef_[0])
print("Intercept (c):", model.intercept_)
print("Score (R²):", [Link](x, y))