Body Fat Prediction — Random Forest Regressor

Using model Random Forest Regressor we would be making a Body Fat Prediction model which will predict the fat in a human body on the basis of few measurements

Aviral Bhardwaj
3 min readApr 29, 2023

As in one of my previous article, I have given you an introduction to Random Forest. Now, in this article, I’ll demonstrate how to build a RFR model using a few lines of code.

So let’s start

The first step is we need to download the dataset and then apply the dataset to the model. you can download or copy data from the URL —

https://raw.githubusercontent.com/aviralb13/git-codes/main/datas/bodyfat.csv

Importing the libraries

Now we will import pandas and NumPy as shown below. If your system does not have these libraries installed, you may get them using the pip command.

Data preparation

Now we’ll read the data using Pandas and save it in a variable called data so we don’t have to call it again and again. Using the head command, we can view the first 5 components of the data; if you want to see more, enter the number inside the bracket.

Defining X and Y

And now I have created a list in which I think it will be the deciding factor for a Body fat i.e. Age, Weight, Height and etc. we will assign it to variable features now I will pass features in the dataset and store it as xand the price of the car as y.

I believe that these x parameters are more appropriate, and if you want to modify the parameters because you believe they are relevant, you can do so.

Making the model

Splitting the Dataset

We must first import the test train split from sklearn model selection before splitting our model dataset into a train and test dataset.

Model

Here, we would be making a random forest regressor because we have to classify our values into an integer. To do this, we would import the random forest regressor from sklearn.ensemble and then fit our training data into the model to train the model.

And finally, our model is ready now we are all set to predict from our model.

Predicting

nowhere I have made a variable named prediction which will predict the values of our test data.

Accuracy

Now we’ll look at our model’s accuracy our model’s Mean Absolute Error, Mean Squared Error, Root Mean Squared Error, and R2 score.

source code

you can go check the link for full code

Conclusion

If you found this article useful please appreciate it by giving claps and follow me for more interesting articles. Well, I have good news for you I would be bringing more articles to explain machine learning concepts and models with codes so leave a comment and tell me how excited are you about this.

--

--

Aviral Bhardwaj

One of the youngest writer and mentor on AI-ML & Technology.