R Tutorial: The importance of scale

preview_player
Показать описание

---

When calculating the distance between two players on a soccer field, you used two features, x and y.

Both of these features are the coordinates of the players and both are measured in the same manner.

Because of this, they are comparable to one another and can be used together to calculate the euclidean distance between the players.

But, what happens when the features aren't measured in the same manner or to put it another way, when the values of these features aren't comparable to one another?

To answer this question let's walk through an example.

Imagine you are provided with a dataset that contains the heights and weights for a large number of men in the United States. The height feature is measured in feet and the weight feature in pounds.

You are interested in calculating the distance between these individuals.

Let us start by comparing observations one and two.

Both men are the same height, six feet. But they differ slightly in weight. In this case the difference is two pounds.

If we calculated the euclidean distance between them we would get a value of two.

Now let's look at observations one and three.

In this comparison, the weights are the same, but the height is different by two feet.

If we calculate the distance once more...

...you guessed it. It's also two.

The distances between both pairs are identical.

If we saw these three men standing side by side, would you really believe that observation one is just as similar to three as it is to two?

Of course not.

Then why are their distances the same?

This happens because these features are on different scales. Meaning they have different averages and different expected variability.

While in these comparisons these features only vary by a magnitude of two, we intuitively know that a change in two pounds is very different than a change of two feet.

So how can we adjust these features to calculate a distance that better aligns with our expectations?

To do this we need to convert our features to be on a similar scale with one another.

There are various methods for doing this, but for this course, we will use the method called standardization.

This entails updating each measurement for a feature by subtracting the average value of that feature and then dividing by its standard deviation.

Doing this across our features places them on a similar scale where each feature has a mean of zero and a standard deviation of one.

Going back to the previous scenario, we can use the mean and standard deviation of the height and weight features to standardize the values for our three observations.

Now, if we calculate the euclidean distances between them...

Voila, the values make sense! They agree with our intuition.

One and three are much less similar to one another than one and two.

In R we can use the scale function to standardize height and weight to the same scale.

If height_weight is our matrix of observations, similar to what we've just seen. Using the scale function with the default parameters will normalize each feature column to a mean of 0 and a variance of 1.

In the next exercise, you will have a chance to further explore how scales can affect your ability to interpret the distance value.
Рекомендации по теме
Комментарии
Автор

what's the difference between scale() function and z-score and which one is best to use?

alial-sarraji
Автор

Harmonic scale...






🤘La Raza

lapro
Автор

what is the equivalent of this in Python?

twarrag