CalcWorldHub
Free online calculator · no signup

Two dimensional k nearest neighbors calculator

Classify a two-feature query by unweighted nearest-neighbor voting with deterministic ties.

4,000 tools
Two dimensional k nearest neighbors calculator — Training x coordinates, Training y coordinates, Integer class labels and calculation method
Two dimensional k nearest neighbors calculator: inputs and calculation method.

How to use this calculator

Classify a two-feature query by unweighted nearest-neighbor voting with deterministic ties.

  1. Enter Training x coordinates, Training y coordinates, Integer class labels, Query x, then complete the remaining labeled fields. Match the units shown beside each field and review the result together with the assumptions below.
  2. Read the worked example below to check the expected input format and result.
  3. Compare another scenario by changing one input at a time. Use Reset to return to the starting values.
Calculation guide

Two dimensional k nearest neighbors explained

For a query point, this calculator ranks the entered two-dimensional training points by Euclidean distance and takes a majority vote among the nearest k labels. It is an unweighted classification example; the winning vote share is a vote fraction, not a calibrated class probability.

Inputs, symbols, and units

x — Training x coordinates
Training-point x coordinates in original row order; one coordinate per class label.
y — Training y coordinates
Matching y coordinates in the same row order and a scale appropriate for Euclidean distance.
l — Integer class labels
Integer class identifiers aligned with the training rows; their numeric magnitudes do not represent distances.
a — Query x
Query point x coordinate, using the same coordinate scale as the training x values.
b — Query y
Query point y coordinate, using the same coordinate scale as the training y values.
k — Neighbors k
Number of training rows included in the vote; a whole number from 1 through the number of rows.

Formula and method

dᵢ = √[(xᵢ−xq)²+(yᵢ−yq)²]; take the majority label among the k smallest distances.

No model training or feature scaling; distance ties use original row order and vote ties use the smallest numeric label..

A worked scenario

Training x coordinates = 0, 1, 5; Training y coordinates = 0, 0, 0; Integer class labels = 1, 1, 2; Query x = 2; Query y = 0; Neighbors k = 3.

  • Predicted class: 1
  • Winning vote share: 0.666667
  • Neighbor row numbers: 2, 1, 3

Distances from (2,0) to rows 1, 2 and 3 are 2, 1 and 3. All three rows participate at k=3, so class 1 wins two votes out of three and neighbor order is 2, 1, 3.

Assumptions and edge cases

No model training or feature scaling; distance ties use original row order and vote ties use the smallest numeric label.

Frequently asked questions

Are labels numeric measurements?

No, integer values identify classes.

How are vote ties resolved?

The smallest numeric class wins.

Can features have different units?

Scale them appropriately before entering.

How do I use the Two dimensional k nearest neighbors calculator?

Enter Training x coordinates, Training y coordinates, Integer class labels, Query x, then complete the remaining labeled fields. Match the units shown beside each field and review the result together with the assumptions below.

What is a worked example of Two dimensional k nearest neighbors?

Training x coordinates = 0, 1, 5; Training y coordinates = 0, 0, 0; Integer class labels = 1, 1, 2; Query x = 2; Query y = 0; Neighbors k = 3. Predicted class: 1 Winning vote share: 0.666667 Neighbor row numbers: 2, 1, 3 Distances from (2,0) to rows 1, 2 and 3 are 2, 1 and 3. All three rows participate at k=3, so class 1 wins two votes out of three and neighbor order is 2, 1, 3.