Our terms of service are changing. Learn more.

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 ↓