Neural networks for regression - a comprehensive overview - Part 7
In this final post, we will briefly focus on how neural networks can be used in a simple real-world scenario: predicting the price of a house in the real estate market.
When we refer to a real-world example, we don't mean that we will be working with a real dataset, but rather that we will demonstrate how to systematically approach a real problem. In fact, our current dataset will consist of only 20 records, making it quite small.
We will consider a scenario where we want to predict the price of a house based on three features: its surface area, the number of rooms, and the number of bathrooms.
surface area (m²) | number of rooms | number of bathrooms | price (€) |
---|---|---|---|
120 | 4 | 2 | 687 |
98 | 4 | 1 | 610 |
76 | 3 | 1 | 367 |
132 | 6 | 3 | 850 |
100 | 5 | 1 | 560 |
110 | 5 | 2 | 710 |
98 | 3 | 1 | 520 |
100 | 4 | 2 | 640 |
105 | 4 | 2 | 650 |
86 | 3 | 1 | 510 |
88 | 3 | 1 | 580 |
168 | 8 | 3 | 1000 |
148 | 6 | 2 | 920 |
94 | 4 | 2 | 600 |
134 | 6 | 3 | 910 |
152 | 7 | 2 | 820 |
122 | 6 | 2 | 740 |
82 | 3 | 1 | 500 |
112 | 6 | 2 | 690 |
144 | 6 | 3 | 870 |
Normalizing data
In our earlier examples, the feature values were all within the $[-1,1]$ interval. However, in our current case, the values can vary significantly, such as the surface area ranging from 50 to 200. To address this, it's good practice to normalize the data so that all features lie within the same range, improving the performance and stability of the neural network.
The following code will normalize the input data, ensuring all values are scaled to the same range before feeding them into the neural network for training or prediction.
1 public void Normalize()
2 {
3 foreach (var feature in Features)
4 {
5 var inputs = Records.Select(t => t.Data[feature]);
6 var min = inputs.Min(); var max = inputs.Max();
7 var diff = max - min; var sum = max + min;
8
9 for (int i = 0; i < inputs.Count(); i++)
10 {
11 Records[i].Data[feature] = (2 / diff) * inputs.ElementAt(i) - sum / diff; // Normalize to range (-1, 1)
12 }
13 }
14 }
Making predictions
Our objective is to predict a house's price based on its surface area, the number of rooms, and the number of bathrooms. To achieve this, we must first train the neural network.
Afterward, we can test the model with any values we choose. An example is provided below.
The results obtained are quite consistent with the initial dataset.
It is important to note that our dataset is too small to be truly representative, and it's entirely possible that some predictions may be inaccurate.
Final thoughts
In this series, we explored how neural networks can be effectively applied to regression problems, enabling accurate predictions of output values. Neural networks are particularly powerful in capturing non-linear relationships between data, making them a popular choice for solving complex scenarios.
If you wish to delve deeper into this topic, acquire the following books, which encompass all the concepts emphasized in this series and delve into more advanced ones.
Deep Learning (Goodfellow, Bengio, Courville
Deep Learning: Foundations and Concepts (Bishop, Bishop)
Machine Learning: An Algorithmic Perspective (Marsland)
Do not hesitate to contact me should you require any further information.