## Original link:http://tecdat.cn/?p=22739

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

1.Analysis of fitting yield curve with Nelson Siegel model improved by neural network in R language

2.R language to realize fitting neural network prediction and result visualization

3.Python uses genetic algorithm neural network fuzzy logic control algorithm to control the lottoanalysis

4.Python for NLP: multi label text LSTM neural network classification using keras

5.An example of stock forecasting based on neural network using R language

6.Deep learning image classification of small data set based on keras in R language

7.An example of seq2seq model for NLP uses keras to realize neural machine translation

8.Analysis of deep learning model based on grid search algorithm optimization in Python