{
  "id": "k-nearest-neighbors-algorithm",
  "title": "K-nearest neighbors algorithm",
  "lang": "en",
  "source_lang": "en",
  "summary": "A nonparametric learning algorithm that predicts a sample’s class or numerical value from its nearest labeled examples.",
  "category": "computer-science",
  "subcategory": "artificial-intelligence",
  "type": "concept",
  "aliases": [
    "k-NN",
    "KNN",
    "k-nearest neighbour algorithm",
    "nearest-neighbor classifier"
  ],
  "generated_at": "2026-10-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/k-nearest-neighbors-algorithm/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/k-nearest-neighbors-algorithm/",
      "json": "https://aiwiki.page/zh/k-nearest-neighbors-algorithm.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "The K-nearest neighbors algorithm (k-NN) is a supervised learning method used in machine learning for classification and regression. It predicts an unknown outcome by finding the \\(k\\) most similar examples in training data and combining their outcomes. Classification typically uses a vote, while regression uses an average. The method is nonparametric: it does not impose a fixed functional form on the relationship between inputs and outputs. (scikit-learn.org)"
    },
    {
      "heading": "Prediction rule",
      "text": "Suppose the training set contains \\(n\\) pairs \\((x_i,y_i)\\), where \\(x_i\\) is a feature vector and \\(y_i\\) is a class label or numerical target. For a query \\(x\\), a distance function identifies a neighborhood \\(N_k(x)\\) containing the indices of its \\(k\\) closest training examples. The positive integer \\(k\\), usually no greater than \\(n\\), is a hyperparameter. (arxiv.org)\n\nFor classification, the predicted class is\n\n\\[\\hat y(x)=\\arg\\max_c\\sum_{i\\in N_k(x)}\\mathbf 1(y_i=c),\\]\n\nwhere the indicator equals one when example \\(i\\) belongs to class \\(c\\). Thus, the class receiving the most votes wins; it need not receive an absolute majority. For regression,\n\n\\[\\hat y(x)=\\frac{1}{k}\\sum_{i\\in N_k(x)}y_i.\\]\n\nWeighted variants replace equal contributions with weights, commonly giving closer examples greater influence through inverse-distance weighting. (scikit-learn.org)\n\nWith \\(k=1\\), the prediction copies the nearest example’s outcome. Equal class votes and equal distances at the neighborhood boundary require tie-handling conventions; some implementations can produce different results when tied training examples are reordered. (scikit-learn.org)"
    },
    {
      "heading": "Distance and feature representation",
      "text": "“Nearest” depends on how observations are represented and compared. For numerical vectors, Euclidean distance is a common choice:\n\n\\[d(x,z)=\\sqrt{\\sum_{j=1}^{p}(x_j-z_j)^2}.\\]\n\nAlternatives include Manhattan distance, Minkowski distances, and task-specific dissimilarities. Different measures can select different neighbors from the same dataset, so the distance function is part of the predictive specification rather than merely an implementation detail. (arxiv.org)\n\nFeature scaling strongly affects distance-based predictions. A variable measured over a large numerical range can dominate another measured over a small range, regardless of predictive relevance. Standardization rescales features to zero mean and unit standard deviation; it can substantially change both the neighbor structure and classification boundary. It does not establish that every feature is equally informative. (scikit-learn.org)\n\nCategorical variables require suitable representations or comparison rules. One-hot encoding represents unordered categories without assigning them artificial numerical ranks, although it increases the number of coordinates. These choices belong to feature engineering and determine which similarities the algorithm can recognize. (sklearn.org)"
    },
    {
      "heading": "Choosing k and evaluating performance",
      "text": "The neighborhood size controls a form of smoothing. Small neighborhoods respond closely to individual observations and noise, increasing susceptibility to overfitting. Larger neighborhoods usually produce smoother predictions but can obscure local distinctions and cause underfitting. This illustrates the bias–variance tradeoff: increasing \\(k\\) generally reduces variability while increasing approximation bias. No single value is optimal for every dataset. (arxiv.org)\n\nThe choice of \\(k\\), distance measure, and weighting rule can be evaluated through cross-validation or a separate validation set. A held-out test set provides a final assessment of generalization after model selection. The evaluation criterion depends on the task: classification and regression require different measures, with mean squared error being one common regression criterion. (scikit-learn.org)\n\nPreprocessing must also respect evaluation boundaries. Scaling, learned transformations, and feature selection are fitted on the training portion of each split, then applied unchanged to the held-out portion. Fitting these operations on the entire dataset can introduce data leakage and optimistic performance estimates. Pipelines provide a mechanism for keeping preprocessing inside each cross-validation split. (scikit-learn.org)"
    },
    {
      "heading": "Computation and dimensionality",
      "text": "K-NN is often described as lazy or instance-based learning because much of its work occurs at prediction time rather than during fitting. Stored examples remain central to the predictor, although fitting may construct a search index. A direct search computes distances to all \\(n\\) examples; for \\(p\\)-dimensional dense vectors, this requires approximately \\(O(np)\\) distance-computation work per query. (arxiv.org)\n\nKD-trees and ball trees can accelerate exact neighbor searches for suitable data. Their efficiency depends on dimensionality and data structure; tree-based methods may lose their advantage in high-dimensional spaces. This is one computational manifestation of the curse of dimensionality, which also makes local neighborhoods harder to populate densely. (scikit-learn.org)\n\nDimensionality reduction can reduce search costs and alter neighborhood quality. For example, principal component analysis projects observations into fewer coordinates, but retained variation need not coincide with information relevant to prediction. Scaling can affect this projection as well as the subsequent neighbor search. (scikit-learn.org)"
    },
    {
      "heading": "Statistical foundations",
      "text": "Thomas Cover and Peter Hart’s 1967 paper, Nearest Neighbor Pattern Classification, established a foundational large-sample result. Under the paper’s distributional conditions, the limiting error of the one-nearest-neighbor classifier is no greater than twice the Bayes error—the minimum achievable classification error given the underlying probability distribution. This is an asymptotic bound, not a guarantee for every finite dataset, and it does not imply that fixed \\(k=1\\) achieves the Bayes error. The analysis also explains why using more neighbors must be balanced against keeping the neighborhood local relative to sample size. (isl.stanford.edu)"
    }
  ],
  "links": [
    "supervised-learning",
    "machine-learning",
    "training-data",
    "hyperparameter",
    "euclidean-distance",
    "feature-scaling",
    "one-hot-encoding",
    "feature-engineering",
    "overfitting",
    "underfitting",
    "bias-variance-tradeoff",
    "cross-validation",
    "validation-set",
    "test-set",
    "generalization",
    "mean-squared-error",
    "feature-selection",
    "data-leakage",
    "kd-tree",
    "curse-of-dimensionality",
    "dimensionality-reduction",
    "principal-component-analysis"
  ],
  "sources": [
    {
      "title": "scikit-learn.org/stable/modules/neighbors.html",
      "url": "https://scikit-learn.org/stable/modules/neighbors.html"
    },
    {
      "title": "arxiv.org/abs/2004.04523",
      "url": "https://arxiv.org/abs/2004.04523"
    },
    {
      "title": "scikit-learn.org/stable/auto_examples/preprocessing/plot_scaling_importance.html",
      "url": "https://scikit-learn.org/stable/auto_examples/preprocessing/plot_scaling_importance.html"
    },
    {
      "title": "sklearn.org/stable/modules/preprocessing.html",
      "url": "https://sklearn.org/stable/modules/preprocessing.html"
    },
    {
      "title": "scikit-learn.org/stable/modules/cross_validation.html",
      "url": "https://scikit-learn.org/stable/modules/cross_validation.html?highlight=cross_validate"
    },
    {
      "title": "scikit-learn.org/stable/common_pitfalls.html",
      "url": "https://scikit-learn.org/stable/common_pitfalls.html?highlight=linearregression"
    },
    {
      "title": "isl.stanford.edu/~cover/papers/transIT/0021cove.pdf",
      "url": "https://isl.stanford.edu/~cover/papers/transIT/0021cove.pdf"
    }
  ],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}