Friday, December 27, 2024
Google search engine
HomeLanguagesCalories Burnt Prediction using Machine Learning

Calories Burnt Prediction using Machine Learning

In this article, we will learn how to develop a machine learning model using Python which can predict the number of calories a person has burnt during a workout based on some biological measures.

Importing Libraries and Dataset

Python libraries make it easy for us to handle the data and perform typical and complex tasks with a single line of code.

  • Pandas – This library helps to load the data frame in a 2D array format and has multiple functions to perform analysis tasks in one go.
  • Numpy – Numpy arrays are very fast and can perform large computations in a very short time.
  • Matplotlib/Seaborn – This library is used to draw visualizations.
  • Sklearn – This module contains multiple libraries are having pre-implemented functions to perform tasks from data preprocessing to model development and evaluation.
  • XGBoost – This contains the eXtreme Gradient Boosting machine learning algorithm which is one of the algorithms which helps us to achieve high accuracy on predictions.

Python3




import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn import metrics
from sklearn.svm import SVC
from xgboost import XGBRegressor
from sklearn.linear_model import LinearRegression, Lasso, Ridge
from sklearn.ensemble import RandomForestRegressor
  
import warnings
warnings.filterwarnings('ignore')


Now let’s load the dataset into the panda’s data frame and print its first five rows.

Python3




df = pd.read_csv('calories.csv')
df.head()


Output:

First five rows of the dataset

First five rows of the dataset

Now let’s check the size of the dataset.

Python3




df.shape


Output:

(15000, 9)

Let’s check which column of the dataset contains which type of data.

Python3




df.info()


Output:

Information of the columns data type

Information of the column’s data type

Now we will check the descriptive statistical measures of the data.

Python3




df.describe()


Output:

Descriptive statistical measures of the dataset

Descriptive statistical measures of the dataset

Exploratory Data Analysis

EDA is an approach to analyzing the data using visual techniques. It is used to discover trends, and patterns, or to check assumptions with the help of statistical summaries and graphical representations. 

Python3




sb.scatterplot(df['Height'], df['Weight'])
plt.show()


Output:

Scatterplot of height v/s weight

Scatterplot of height v/s weight

So, we have a kind of linear relationship between these two features which is quite obvious.

Python3




features = ['Age', 'Height', 'Weight', 'Duration']
  
plt.subplots(figsize=(15, 10))
for i, col in enumerate(features):
    plt.subplot(2, 2, i + 1)
    x = df.sample(1000)
    sb.scatterplot(x[col], x['Calories'])
plt.tight_layout()
plt.show()


Output:

Scatter plot for features and target column

Scatter plot for features and target column

As expected higher is the duration of the workout higher will be the calories burnt. But except for that, we cannot observe any such relation between calories burnt and height or weight features.

Here we can observe some real-life observations:

  • The average height of the boys is higher than girls.
  • Also, the weight of the girls is lower than that of the boys.
  • For the same average duration of workout calories burnt by men is higher than that of women.

Python3




features = df.select_dtypes(include='float').columns
  
plt.subplots(figsize=(15, 10))
for i, col in enumerate(features):
    plt.subplot(2, 3, i + 1)
    sb.distplot(df[col])
plt.tight_layout()
plt.show()


Output:

Distribution plot for continuous features

Distribution plot for continuous features

The distribution of the continuous features follows close to normal distribution except for some features like Body_Temp and Calories.

Python3




df.replace({'male': 0, 'female': 1},
           inplace=True)
df.head()


Output:

First five rows of the dataset

First five rows of the dataset

Python3




plt.figure(figsize=(8, 8))
sb.heatmap(df.corr() > 0.9,
           annot=True,
           cbar=False)
plt.show()


Output:

Heatmap to detect highly correlated features

Heatmap to detect highly correlated features

Here we have a serious problem of data leakage as there is a feature that is highly correlated with the target column which is calories.

Python3




to_remove = ['Weight', 'Duration']
df.drop(to_remove, axis=1, inplace=True)


Model Training

Now we will separate the features and target variables and split them into training and testing data by using which we will select the model which is performing best on the validation data.

Python3




features = df.drop(['User_ID', 'Calories'], axis=1)
target = df['Calories'].values
  
X_train, X_val,\
    Y_train, Y_val = train_test_split(features, target,
                                      test_size=0.1,
                                      random_state=22)
X_train.shape, X_val.shape


Output:

((13500, 5), (1500, 5))

Now, let’s normalize the data to obtain stable and fast training.

Python3




# Normalizing the features for stable and fast training.
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_val = scaler.transform(X_val)


Now let’s train some state-of-the-art machine learning models and compare them which fit better with our data.

Python3




from sklearn.metrics import mean_absolute_error as mae
models = [LinearRegression(), XGBRegressor(),
          Lasso(), RandomForestRegressor(), Ridge()]
  
for i in range(5):
    models[i].fit(X_train, Y_train)
  
    print(f'{models[i]} : ')
  
    train_preds = models[i].predict(X_train)
    print('Training Error : ', mae(Y_train, train_preds))
  
    val_preds = models[i].predict(X_val)
    print('Validation Error : ', mae(Y_val, val_preds))
    print()


Output:

LinearRegression() : 
Training Error :  17.893463692619434
Validation Error :  18.007896272831253

XGBRegressor() : 
Training Error :  10.110870876925963
Validation Error :  10.16210130894184

Lasso() : 
Training Error :  17.915089584958036
Validation Error :  17.995033362288662

RandomForestRegressor() : 
Training Error :  3.982735208112875
Validation Error :  10.472395222222223

Ridge() : 
Training Error :  17.893530494767777
Validation Error :  18.00781790803129

Out of all the above models, we have trained RandomForestRegressor and the XGB model’s performance is the same as their MAE for the validation data is same.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments