Introduction to Data Science
Md Abdullah-Al-Kafi,
Lecturer,
Daffodil International University
Spring 2024
1 Regression Numericals
Given Dataset:
Hours Exercised (x) : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Change in Weight (y) : [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
Step 1: Calculate the Mean of x and y We calculate the mean of x (hours exercised) and y (change
in weight):
10 10
1 X 1 X
x̄ = xi ȳ = yi
10 i=1 10 i=1
For x:
1 1
x̄ = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) = × 55 = 5.5
10 10
For y:
1 1
ȳ = (0.5 + 1.0 + 1.5 + 2.0 + 2.5 + 3.0 + 3.5 + 4.0 + 4.5 + 5.0) = × 25 = 2.5
10 10
So, x̄ = 5.5 and ȳ = 2.5.
Step 2: Calculate the Slope (m) and Y-Intercept (b) The slope (m) and y-intercept (b) of the
regression line can be calculated using the formulas:
P10
i=1 (xi − x̄)(yi − ȳ)
m= P10 2
b = ȳ − m · x̄
i=1 (xi − x̄)
First, let’s calculate m:
1
P10
i=1 (xi − 5.5)(yi − 2.5) (1 − 5.5)(0.5 − 2.5) + (2 − 5.5)(1.0 − 2.5) + . . . + (10 − 5.5)(5.0 − 2.5) (−
m= P10 = 2 2 2
=
i=1 (xi − 5.5)
2 (1 − 5.5) + (2 − 5.5) + . . . + (10 − 5.5)
Now, let’s calculate b:
b = 2.5 − 0.8636 × 5.5 ≈ 2.5 − 4.7499 ≈ −2.2499
So, m ≈ 0.8636 and b ≈ −2.2499.
Step 4: Make Predictions Once we have m and b, we can make predictions for x = 9.5 using the
equation of a straight line:
ŷ = mx + b
Let’s calculate ŷ:
ŷ = 0.8636 × 9.5 − 2.2499 ≈ 8.2034 − 2.2499 ≈ 5.9535
So, the predicted change in weight for x = 9.5 hours exercised is approximately 5.9535 kilograms.
]
Given Dataset:
Hours Exercised (x) : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Change in Weight (y) : [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
Step 1: Calculate the Mean of x and y We calculate the mean of x (hours exercised) and y (change
in weight):
10 10
1 X 1 X
x̄ = xi ȳ = yi
10 i=1 10 i=1
For x:
1 1
x̄ = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) = × 55 = 5.5
10 10
For y:
1 1
ȳ = (0.5 + 1.0 + 1.5 + 2.0 + 2.5 + 3.0 + 3.5 + 4.0 + 4.5 + 5.0) = × 25 = 2.5
10 10
So, x̄ = 5.5 and ȳ = 2.5.
Step 2: Calculate the Slope (m) and Y-Intercept (b) The slope (m) and y-intercept (b) of the
regression line can be calculated using the formulas:
P10
i=1 (xi − x̄)(yi − ȳ)
m= P10 2
b = ȳ − m · x̄
i=1 (xi − x̄)
2
First, let’s calculate m:
P10
i=1 (xi − 5.5)(yi − 2.5) (1 − 5.5)(0.5 − 2.5) + (2 − 5.5)(1.0 − 2.5) + . . . + (10 − 5.5)(5.0 − 2.5) (−
m= P10 = 2 2 2
=
i=1 (xi − 5.5)
2 (1 − 5.5) + (2 − 5.5) + . . . + (10 − 5.5)
Now, let’s calculate b:
b = 2.5 − 0.8636 × 5.5 ≈ 2.5 − 4.7499 ≈ −2.2499
So, m ≈ 0.8636 and b ≈ −2.2499.
Step 4: Make Predictions Once we have m and b, we can make predictions for x = 9.5 using the
equation of a straight line:
ŷ = mx + b
Let’s calculate ŷ:
ŷ = 0.8636 × 9.5 − 2.2499 ≈ 8.2034 − 2.2499 ≈ 5.9535
So, the predicted change in weight for x = 9.5 hours exercised is approximately 5.9535 kilograms.