Practical Machine Learning

2025-07-126 min read

#Machine Learning#Data Science#Artificial Intelligence
Category:Machine Learning
Priyanshu Jha

Priyanshu Jha

Software Engineer

Cover

Key Points

  • Dataset
  • Code
AgeGenderHeightWeightDite_typeHealthy
25M5570protein1
26M6075fat_free1
27M4960protein0
28M5085balance0
30M5260fat_free1
32M5355protein0
35F5558fat_free0
36F5659fat_free1
37F6061protein0
38F6262protein1
39F6563protein0
41F6964fat_free1
43F5765fat_free0
42M5866fat_free1
40F5967protein0
25M5866balance0
31F5867balance1
60F5589balance0
45M5656protein0
import csv

heights = [] # Assume X
weights = [] # Assume Y

with open('dataset.csv', 'r') as file:
csv_data = csv.reader(file)
next(csv_data)
for data_row in csv_data:
    heights.append(float(data_row[2]))
    weights.append(float(data_row[3]))


for height in heights:
print(height)

for weight in weights:
print(weight)

sumOfX = sum(heights)
print("Sum of X:", sumOfX)
sumOfY = sum(weights)
print("Sum of Y:", sumOfY)



sumOfProdOfXY = 0
for i in range(len(heights)):
product = heights[i] * weights[i]
sumOfProdOfXY += product

print("Sum of product of X*Y:", sumOfProdOfXY)

sumOfSquareOfX = 0

for x in heights:
square = x * x
sumOfSquareOfX += square

print("Sum of squares of X:", sumOfSquareOfX)


n = len(heights)
print("Value of n: ", n)


# calculate slope
numerator = (n * sumOfProdOfXY) - (sumOfX * sumOfY)
denominator = (n * sumOfSquareOfX) - (sumOfX * sumOfX)

slope = numerator / denominator
print("slope:", slope)

b = (sumOfY - (slope * sumOfX)) / n
print("B value:", b)

predict = [66,65,50]
for X in predict:
predY = slope * X + b
print(f"Predicted Y for X = {X} is: {predY}")

Related Posts