Hello, and welcome to this tutorial. We are almost done with the first part, **Data Preprocessing**. Iâ€™m looking forward to being well prepared with our data so that we can start making our machine learning models in the second part. Just this one tutorial and weâ€™re ready. In this tutorial weâ€™re going to learn about **Feature Scaling in R, **and why it is very important in Data Science and Machine Learning.

#### Â What is Feature Scaling and why do we need to do it?

If you take a look at our dataset, youâ€™ll see that the Age and Salary variables are not on the same scale. Theyâ€™re not on the same scale because, the â€˜Ageâ€™ is going from 27 â€“ 50, while the â€˜Salaryâ€™ is going from 48,000 to 83,000. This would cause some issues in our Machine Learning Models. Whyâ€™s that? Itâ€™s because, a lot of machine learning models are based on what is called, the Euclidean Distance. If you can remember it from back in High School, the Euclidean Distance between two points is the square root of the sum of the square coordinates. Did you catch that? Maybe a diagram will help

Picture â€˜Ageâ€™ as the â€˜xâ€™ coordinate and the â€˜Salaryâ€™ as the â€˜yâ€™ coordinate. In some machine learning models, the Euclidean Distance between observation points, is computed using the two x and y coordinates. Since the salary column has a wide range of values, it would dominate the â€˜Ageâ€™ column. Letâ€™s use an example from our dataset so that we can understand this more.

Take Kisumu in the 2^{nd} row and Nairobi in the 8^{th} row. The Euclidean Distance between those two observation points would be.

Â The difference between the x – coordinates is; 48 – 27 = 21. The square of 21 is 441.

The difference between the y- coordinates is; 79000 â€“ 48000 = 31000. The square of 31000 is 961,000,000

You can see very clearly how the square difference of the â€˜Salaryâ€™ dominates the square difference of the â€™Ageâ€™. In the Machine learning equations, it will be like the â€˜Ageâ€™ doesnâ€™t exist, because it will be dominated by the â€˜Salaryâ€™.

This is why we need to put the variables on the same scale. Weâ€™re going to transform those variables and give them values in the same range. For example, they are going to have values from a negative one to plus one, to eliminate huge numbers that dominate small numbers.

**Feature Scaling in R for Data Science and Machine Learning.**

There are several ways of scaling your data. One of the most common ones is standardization. Standardization means that for each observation and each feature, you subtract the mean value of the feature and you divide it by the standard deviation.

The other way of scaling your data is Normalization. Normalization means that you subtract your observation feature â€˜Xâ€™ by the minimum value of all the feature values and you divide it by the difference between the maximum and the minimum of your feature values.

Donâ€™t worry. The theory and â€˜*mazematic*â€™ might be a little confusing but generally what we are doing is that weâ€™re putting our variables on the same scale so that no variable is dominated by the other.

Now that you understand why feature scaling is important, letâ€™s apply it to our training set and the test set. Unlike in **Python**, we just need two lines of code to feature scale in R. These are;

**training_set = scale(training_set)**

**test_set = scale(test_set)**

That is the feature scaling block of code that we will use in our template. However, if you run that code, you will get an error.

The County and Purchased columns are not numeric, they are factors (** See encoding Categorical Data**), and we cannot scale factors, we can only scale numeric values. A factor in R is not a numeric number. This means we have to specify which columns we want to scale. Indexes in R start at 1, so if we count from the County column, the â€˜Ageâ€™ and the â€˜Salaryâ€™ will be in the 2

^{nd}and 3

^{rd}indexes respectively.

To specify those two columns in the block of code;

**training_set[, 2:3] = scale(training_set[, 2:3])**

**test_set[, 2:3] = scale(test_set[, 2:3])**

The code should look like this in R Studio

If you press ‘Ctrl + Enter’ to run the code now.

Take a look at our training set and the test set now.

Perfect. Now you know how to do Feature Scaling in R and why it is important to Data Science and Machine Learning. Our data is ready to offer good precision and accuracy. Our machine learning models will converge rapidly and work faster.

###### Some Really Useful Data Science and Machine Learning Books

Congratulations, you now know how to Feature Scale in R. Even more congratulations on finishing all the required steps in Preprocessing our data. Next, Iâ€™m going to explain how we are going to use our Data Preprocessing template in our Machine Learning Models. Youâ€™ve passed the most boring part and now itâ€™s time to have fun. Itâ€™s time to start making the models. See you at the next one.

[…] the same folder. We are going to use the Data Preprocessing template we created in the first part (Data Preprocessing). Copy all the contents of the Data Preprocessing Template into the new […]