Join thousands of AI enthusiasts and experts at the Learn AI Community.

Publication

Latest

Regression Model in Weight Prediction

Last Updated on April 29, 2022 by Editorial Team

Author(s): Gencay I.

Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.

Photo by Piret Ilver onย Unsplash

Introduction

Data scientists have known too many industrial applications of Machine Learning. But what about real-life problems, like losing weight, or being fit? From time to time, many people have struggled with losing weight or being fit. There are endless diets, supplements, and meal replacement plans, that ensure weight loss. Some of these strategies backed by science do have an impact on weight management.

But how can we ensure reaching ourย goals?

Well for me, like many other problems, It is a numbersย game.

You canโ€™t improve what you donโ€™tย measure.

Thatโ€™s why, I have been keeping recording my measurements such as the waistline, and myย weight.

By the way, you can jump into wherever youย like;

ยท Introduction
โˆ˜ Simple Linear Regression to Predict your Weight
โˆ˜ Removing Outliers
โˆ˜ Applying Linear Model
โˆ˜ Evaluation
ยท bodycal
โˆ˜ Installation
โˆ˜ BMIโ€Šโ€”โ€ŠBody Mass
โˆ˜ BMRโ€Šโ€”โ€ŠBasal Metabolic Rate
โˆ˜ BodyFat Percentage-
ยท Conclusion

One day, after measuring my waistline, I wonder, if I can predict my weight according toย that.

Sure as a Data Scientist and Machine Learning Engineer, It is obvious that this is a Simple Linear Regression Problem.

So I try to code myย problem.

(Here is my full codeย )

First I upload the necessary libraries, which I will explain to you step byย step.

Simple Linear Regression to Predict yourย Weight

import pandas as pd
import numpy as np
from sklearn import linear_model
import bodycal
import matplotlib.pyplot as plt

It is simple like that, after that I wrote my previous measurements to feed myย model.

df1 = pd.DataFrame({'Weight': [90.9, 91.6 , 91.6, 92.4, 92.6, 91.9, 92.5, 92.7],
'WaistLine': [100, 99.5, 101, 103, 103.5, 103,104, 103.5]},
index=[0, 1, 2, 3, 4 , 5, 6,7 ])

After creating my Dataframe, it is vital to see if there could be an outlier, which could outperform my model's efficiency.

One way to doย that

plt.scatter(df1['Weight'],df1['WaistLine'] ,  color='blue')
plt.title("Weight Prediction Model")
plt.xlabel('Weight')
plt.ylabel('WaistLine')
plt.show()
Source: Image by theย author.

Removing Outliers

If you want to remove that outlier, one fancy way to doย that:

df2 = df1[df1['Weight'] > 91.00]
plt.scatter(df2['Weight'],df2['WaistLine'] , color='blue')
plt.title("Weight Prediction Model")
plt.xlabel('Weight')
plt.ylabel('WaistLine')
plt.show()
Source: Image by theย author.

Applying Linearย Model

Now it is time to apply our linearย model.

regr = linear_model.LinearRegression()
y = np.asanyarray(df1['Weight'])
x = np.asanyarray(df1['WaistLine'])
regr.fit(x.reshape(-1, 1), y)

We did reshape our x value because that is a simple linear regression model.

If you will add additional measurements, like the measure of your Hip, to make your model sharper, apply the Multiple Linear regression model.

Which looks likeย that;

regr = linear_model.LinearRegression()
y = np.asanyarray(df1['Weight'])
x = np.asanyarray(df1['WaistLine'],['Hip'])
regr.fit(x.reshape(-1, 1), y)

Evaluation

Now, let's make our modelย fancier.

To do that, first, we will make our prediction about our weight by entering our waistline measurement.

It will be good to type it float because your waistline measurement could have aย decimal.

print("Please enter you waistline measurement")
d = float(input())

Now it is time to make a prediction with that and to see the output is good, it will be a good idea to round that with 2 decimal, and print it out accordingly.

b  = regr.predict([[d]])[0]
b = round(b,2)
print("Your predicted weight according to your waistline: {}".format(b))

Now it is time to evaluate our model inย reality.

print("Please enter you weight in scale.")
c = float(input())
a = (1- abs((b - c) / c)) * 100
a = round(a,2)
print("Your models accuracy is % {}".format(a))

Now you understand the code. So you could run that in oneย piece;

print("Please enter you waistline measurement")
d = float(input())
b = regr.predict([[d]])[0]
b = round(b,2)
print("Your predicted weight according to your waistline: {}".format(b))
print("Please enter you weight in scale.")
c = float(input())
a = (1- abs((b - c) / c)) * 100
a = round(a,2)
print("Your models accuracy is % {}".format(a))

After running your script your output should be something likeย that;

bodycal

Source: Image by theย author.

After digging into that, I did some research about body measurements.

After that my research becomes my passion and I try to write a library in Python, which includes mainly 3 functions(BMI, BMR,Fat Percentage) and differs into it according to your metric system andย gender.

bodycal

Installation

pip install bodycal

BMIโ€Šโ€”โ€ŠBodyย Mass

While calculating BMI, it is important to see the range that you must in to beย healthy.

If you are not in the range, then it is vital for you to know, how much weight you have toย lose?

Also, the bmi_kg function took two arguments, which are the measurements ofย your;

  • Weight
  • Height
bodycal.bmi_kg(100,185)

Also, you could find the explanation of thatย function

And that returns to your BMI category, ideal weight range the amount kg of you have toย lose

bodycal.bmi_kg_exp(100,185)

And that returns to your BMI category, ideal weight range the amount kg of you have toย lose.

BMRโ€Šโ€”โ€ŠBasal Metabolic Rate

That measures the amount of energyโ€Šโ€”โ€Šโ€Œin caloriesโ€Šโ€”โ€Šthat your body needs to stay alive and function properly.

This function took 3 arguments whichย are:

  • Weight
  • Height
  • Age
bodycal.bmr_male_cm_exp(85,185,32)

BodyFat Percentage-

This body fat percentage calculation formula using by Americanย Navy.

The calculation differs between men andย women.

This function takes three arguments forย men:

The measurement of;

  • abdomen
  • neck
  • height

Let me give an example forย you

bodycal.fat_perc_male_cm(100,38,93)

But if you want to dig deeper and get an explanation aboutย that:

bodycal.fat_perc_female_cm_exp(70,100,38,185)

And the fat percentage equation took 4 measurements in women whichย are

  • abdomen
  • hip
  • neck
  • height

Conclusion

So as I said before, I believe in life we can achieve most of our goals by measuring and making plans according to that, and keep measuring afterย that.

If you want to look up more about bodycal, here is theย link

Thanks!


Regression Model in Weight Prediction was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Join thousands of data leaders on the AI newsletter. Itโ€™s free, we donโ€™t spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aย sponsor.

Published via Towards AI

Feedback โ†“