Assuming 'models[1]' is your trained classifier
cm = confusion_matrix(ytest, models[1].predict(xtest))
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=models[1].classes_) # Assuming your model has a 'classes_' attribute
disp.plot()
plt.show()
Let’s also print the classification report for the best performing model.
print(metrics.classification_report(ytest,
models[1].predict(xtest)))
Output
6.5 Calories Burnt Prediction
Importing Libraries and Dataset
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.
df = pd.read_csv('calories.csv')
df.head()
Output
Now let’s check the size of the dataset.
df.shape
Output
(15000, 9)
Let’s check which column of the dataset contains which type of data.
df.info()
Output
Now we will check the descriptive statistical measures of the data.
df.describe()
Output
Exploratory Data Analysis
sb.scatterplot(x='Height', y='Weight', data=df)
plt.show()
Output
So, we have a kind of linear relationship between these two features which is quite obvious.
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, y='Calories', data=x)
plt.tight_layout()
plt.show()
Output
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 v The average height of the boys is higher than girls. v Also, the weight of the girls is lower than that of the boys. v For the same average duration of workout calories burnt by men is higher than that of women.
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
The distribution of the continuous features follows close to normal distribution except for some features like Body_Temp and Calories.
df.replace({'male': 0, 'female': 1},
inplace=True)
df.head()
Output
plt.figure(figsize=(8, 8))
sb.heatmap(df.corr() > 0.9,
annot=True,
cbar=False)
plt.show()
Output
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.
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.
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.