K-Nearest Neighbors and Distance Metrics

Chapter 5, Distance-Based Learning, K-Nearest Neighbors

Learning objectives

  • Call a sample by the vote of its k nearest neighbours, and explain why the logs are standardized before a distance is taken
  • Read Ogbon-1's neutron-density crossplot as kNN reads it: limestone dense and low in neutron porosity, shale high in neutron, sandstone between
  • Explain why k = 1 is always right on its own samples, and why a sample is left out of its own vote
  • Compare Euclidean and Manhattan distance, and say what k changes in the boundary between the rocks

The Nearest Samples Vote

k nearest neighbours (kNN) calls a sample by the vote of the kk training samples nearest it: no weights are fitted, the training samples are the model. On Ogbon-1's neutron-density crossplot, the petrophysicist's own plot, it finds the well's three rocks: with k = 15 it calls 0.994 of the well's samples right when each sample is left out of its own vote. Voting for itself, with k = 1, every sample is called right, 1.000, a score that says nothing: the nearest training sample to a training sample is that sample. Left out, k = 1 calls 0.993 right.

Knn CrossplotInteractive figure, enable JavaScript to interact.

The Data

The figure reads Ogbon-1, the Petrophysics book's well, as the book's own learning set holds it: 1201 samples from 5800 ft to 6400 ft, every 0.5 ft, its neutron porosity (NPHI, v/v) and bulk density (RHOB, g/cc) as the book's own tool model records them, which averages a few feet of rock and adds a stated noise. The set is synthetic, and that is why it can be checked: the lithology of every sample is the generator's bed table, 214.5 ft of shale, 308.0 ft of sandstone and 78.0 ft of limestone.

A Distance Needs Standardized Logs

A distance adds the differences between two samples log by log, and a difference in v/v cannot be added to one in g/cc. Each log is therefore standardized first, z=(xβˆ’xΛ‰)/sz = (x - \bar x)/s, over the well's samples: neutron porosity has a standard deviation of 0.078 v/v here and density 0.105 g/cc, and after the change each is a number of standard deviations, with no unit. The Euclidean distance is the straight line between two samples on the standardized logs, dE=βˆ‘j(zijβˆ’zlj)2d_E = \sqrt{\textstyle\sum_j (z_{ij} - z_{lj})^2}; the Manhattan distance adds the differences, dM=βˆ‘j∣zijβˆ’zlj∣d_M = \textstyle\sum_j |z_{ij} - z_{lj}|. The samples at one Euclidean distance from a probe lie on a circle on the standardized logs, at one Manhattan distance on a diamond; on the figure's axes, in v/v and g/cc, both are stretched.

The Crossplot kNN Reads

The three rocks sit apart on the crossplot, where the Petrophysics book puts them. The limestone is the densest rock in the well and the lowest in neutron porosity, 2.56 g/cc and 0.11 v/v on average; the sandstone is the lightest, 2.27 g/cc and 0.26 v/v; the shale reads 2.31 g/cc and the highest neutron, 0.35 v/v, because the water bound in its clay counts as porosity to a neutron tool. Shale and sandstone differ by only 0.04 g/cc in density, so density alone would confuse them; the neutron log separates them. kNN learns none of this as a rule: it only finds, for each sample, which rock its nearest samples are.

One Neighbour, and Leaving a Sample Out

With k = 1 and every training sample allowed to vote, each sample is its own nearest neighbour, at a distance of zero, and is called by its own label: 1.000 right, whatever the labels are. That is why a sample is asked about with itself left out of its own vote, as a new sample would be. Left out, one neighbour calls 0.993 of Ogbon-1's samples right and misses 4.5 ft. The ground of the figure's crossplot, the call at every point of a grid, follows single samples at k = 1, ragged where the rocks meet.

What k and the Metric Change

A larger kk is a wider vote, and the boundaries between the rocks grow smooth. On Ogbon-1 the rocks are so far apart that every kk scores near 1: 0.994 left out at k = 15 and 0.994 at k = 51, 3.5 ft missed at each. The metric changes the shape of the neighbourhood and, here, almost nothing else: at k = 15 Manhattan distance changes 0 of the 1201 calls. Five of the seven misses at k = 15 lie within 1.25 ft of a bed edge, inside half the neutron tool's 2.5 ft response, where the tool averages two rocks; the other two, in the main sand at 6248.0 ft and in the shale at 6317.5 ft, are samples the tool's noise has carried across.

What This Score Is

Every score in this section is on Ogbon-1's own samples. Leaving one sample out is fairer than letting it vote for itself, but it still flatters: every other sample of its own bed, read by the same tool in the same well, stays among the candidates, and for 0.727 of the samples the nearest other sample is in the same bed. The model is never asked about a well it has not seen. Section 5.2 asks kNN about wells it has not seen, one well held out at a time.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.