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.
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()
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()
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
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.
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