Machine Learning Project 9 — Predict weight based on height and gender | by Omair Aasim | Towards Data Science

Today I am going to be working on this Kaggle dataset. This dataset has 10,000 rows and provides the weight, height and gender of the person. We have to build and train a model on this dataset so it can predict the weight of a person given their gender and height. This is the first project that we are working on that has a large dataset. So this is going to be interesting.
#100DaysOfMLCode #100ProjectsInML
Let’s get started.
First we will load the dataset. Below is the sample dataset — The full data file can be found here.
The dataset contains 3 columns namely “Gender”, “Height” and “Weight”.
Height is given in inches and weight in pounds.
Let’s analyze the data.
dataset.info():
This method returns the output below. It basically shows the number of entries, the column names and the column types. Below is the output of the command.
<class ‘pandas.core.frame.DataFrame’>RangeIndex: 10000 entries, 0 to 9999Data columns (total 3 columns):Gender 10000 non-null objectHeight 10000 non-null float64Weight 10000 non-null float64dtypes: float64(2), object(1)memory usage: 234.5+ KB
dataset.describe():
The describe method displays basic statistical information on the numerical values of the dataset like mean, standard deviation, percentile, min and max.
For example: for height, we can see that the average is 66.36 inches and the min height is 54.26 inches and max height is 78.99 inches.
Similarly for weight, the average is 161.44 lbs, the min weight is 64.7 lbs and the max weight is 269.98 lbs.
Below is the output of the command.
               Height       Weightcount    10000.000000 10000.000000mean        66.367560   161.440357std          3.847528    32.108439min         54.263133    64.70012725%         63.505620   135.81805150%         66.318070   161.21292875%         69.174262   187.169525max         78.998742   269.989699
dataset.isnull().sum()
This method is basically used to check if there are any null values in the dataset. This will list number of null values in each column
Below is the output of the command. We can see that there are no null values in any of the 3 columns.
Gender 0Height 0Weight 0dtype: int64
Before we run any machine learning models, we have to convert all categorical values (text values) to numerical values. In our dataset, we can see that we have one field — “Gender” which is categorical. So we have to convert this field into numerical.
The typical way of converting categorical values to numerical is by using the LabelEncoder class to convert Text to Numbers and then using OneHotEncoder to add dummy variables. We did this in our Project 2.
But this is useful only if the categorical field contains more than 2 values — for instance, let’s say we have a field called “City” and it contains more than 2 values like “Houston”, “New York” and “San Francisco”.
In this case, we will first use LabelEncoder to convert the cities to numbers like: “Houston” => 0 and “New York” => 1 and “San Francisco” => 2.
Then we will use OneHotEncoder to add 3 dummy variables one for each city. Finally, we will drop one dummy variable to avoid the dummy variable trap.
The rule of thumb is — if a categorical field has k values then you will need k-1 dummy variables.
Now in our case, our “Gender” field contains only 2 values — “Male” and “Female”. When we use LabelEncoder, it will convert say “Male” =>0 and “Female” => 1. Since it contains only 2 values, there is no need for us to use OneHotEncoder and add dummy variables. If we use OneHotEncoder, it will end up adding 2 dummy variables but we will end up dropping 1 to avoid the dummy trap. So it makes no sense to do it.
So if a categorical field has 2 values — there are 2 ways for us to do this.
Use LabelEncoder
Replace directly in DataFrame
Rather than using a LabelEncoder, we can directly replace Male with 0 and Female with 1 in the DataFrame as shown below:
In both cases, the Gender will be converted to 0 for Male and 1 for Female as shown below in screenshot of X.
We will use 80% of the data for training and 20% of the data for testing.
We will use the LinearRegression to train our model.
Let’s compare the predicted values (lin_pred) with the actual values (y_test). We can see that our model did a fairly good job.
There are a couple of metrics we can check to see how well our model performed. One of them is the R squared.
We can see that our R squared is showing almost 90% accuracy which is good. The Mean Squared Error and Mean Absolute Error we will explore in future projects.
R square = 0.9052889824534557Mean squared Error = 97.87152220196164Mean absolute Error = 7.955916525326746R square = 0.9052889824534557Mean squared Error = 97.87152220196164Mean absolute Error = 7.955916525326746
Let’s see how well the model does in predicting my weight. I’m 6ft 2 — that’s 74 inches.
My predicted weight = [197.2481918]
All I can say is that it’s not accurate :) — Maybe I am just an outlier — well outliers is another topic we will leave for another day.
I had fun building this project. Looking for the next challenging project that I will be solving next.
The source code can be found here.