Getting ready

It is important to extend the nearest-neighbor algorithm, to take into account variables that are scaled differently. In this example, we will illustrate how to scale the distance function for different variables. Specifically, we will scale the distance function as a function of the feature variance.

The key to weighting the distance function is to use a weight matrix. The distance function, written with matrix operations, becomes the following formula:

Here, A is a diagonal weight matrix that we will use to scale the distance metric for each feature.

In this recipe, we will try to improve our MSE on the Boston housing value ...

Get TensorFlow Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.