Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Regression Model in Weight Prediction
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 ↓