# Prediction of human fat percentage data with BP neural network in MATLAB

Time：2021-8-17

This example shows how a function fitting neural network can estimate the percentage of fat (BFP) based on the measurement results  。

### Question: estimated fat percentage

In this example, we try to build a neural network to estimate a person’s fat percentage, which is described by 13 physical attributes.

• Age
• weight
• height
• Neck circumference
• bust
• Abdominal circumference
• hipline
• Thigh circumference
• Knee circumference
• Ankle circumference
• Biceps brachii (extension) circumference
• Forearm circumference
• Wrist circumference

This is an example of a fitting problem, in which the input matches the relevant target output. We hope to create a neural network, which can not only estimate the known target and given the known input, but also generalize to accurately estimate the result of the unknown data.

### Why neural network?

Neural networks are excellent in function fitting. A neural network with enough elements (called neurons) can fit any data with arbitrary accuracy. They are especially suitable for solving nonlinear problems. In view of the nonlinear nature of the real world, such as the increase of body fat, neural network is a good method to solve this problem.

Thirteen physical attributes will be input to the neural network, and the percentage of body fat will be the target.

The network trains it to produce target values by building a model using human body data that already knows the percentage of body fat.

### Prepare data

The data of function fitting are two matrices, namely input matrix X and objective matrix t.
Each column of the input matrix will have 13 elements representing body data for a known percentage of fat.
Each corresponding column of the target matrix will have an element representing the fat percentage.

After loading the dataset, we can view the size of input X and target variable t.

Notice that both X and t have 252 columns. These represent 252 Constitutions (input variables) and associated body fat percentages (target variables).

The input matrix X has 13 rows representing 13 attributes. The target matrix T has only one row, because we have only one expected output for each example, that is, fat percentage.

``````size(X)
size(T)`````` ### Fitting function with neural network

The next step is to create a neural network that will learn to estimate the percentage of fat.

Since the neural network starts with random initial weights, the results of each run of this example will be slightly different. Random seeds are set to avoid this randomness.

``setdemorandstream(82)``

Two layers (i.e. one hidden layer) feedforward neural network can fit any input-output relationship as long as there are enough neurons in the hidden layer. Layers that do not belong to the output layer are called hidden layers.

In this example, we will try to use a single hidden layer composed of 15 neurons. In general, more difficult problems require more neurons and perhaps more layers. Simpler problems require fewer neurons.

The size of the input and output is 0 because the neural network has not been configured to match our input and target data. This will be done when the network is trained.

``````net = fitnet(15);
view(net)`````` Now, the network is ready to be trained. The samples are automatically divided into training set, verification set and test set. The training set is used to train the network. As long as the network continues to improve on the verification set, the training will continue. The test set provides a completely independent measure of network accuracy.
The neural network training tool displays the network being trained and the algorithm used for training. It also shows the training status during training, and the criteria for stopping training will be highlighted in green.

The buttons at the bottom open useful charts that can be opened during and after training. The link next to the algorithm name and the plot button opens documentation on these topics.

`` train(net,X,T);`` To see how the network performance is improved during training, you can click the “performance” button in the training tool.
Performance is measured by mean square error and displayed in logarithmic scale. With the training of the network, the error decreases rapidly. The performance of training set, verification set and test set are displayed respectively. The final network is the one that performs best in the verification set.

``plotperform(tr)`` ### Test neural network

The relationship between the mean square error of the trained neural network and the test sample can now be measured. We can understand how the network performs when applied to real data.

``mse(net,testT,testY)`` Another way to measure the fitting degree of neural network to data is regression diagram. The regression plot here is drawn in all samples.
The regression diagram shows the relationship between the actual network output and the target value. If the network has learned to fit the data well, the linear fitting of the output target relationship should closely intersect the lower left and upper right corners of the graph.

If not, train further, or train a network with more hidden neurons.

``plot(T,Y)`` Another way to measure the fitting degree of neural network to data is error histogram. This shows how the error size is distributed. Usually, most errors are close to zero, and few errors are far from zero.

``````e = T - Y;
hist(e)`````` This example shows how to design a neural network to estimate the percentage of fat from body characteristics. Most popular insights

## “Self test” stay up late to summarize 50 Vue knowledge points, all of which will make you God!!!

preface Hello everyone, I’m Lin Sanxin. A lot of things have happened these days (I won’t say what’s specific). These things have scared me to treasure my collection these yearsVue knowledge pointsI took out my notes and tried my best to recall them. Finally, I realized these 50Knowledge points(let’s not be too vulgar. It’s not […]