ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation. |
This module contains procedures and generic interfaces for computing the nearest neighbor statistics of random samples.
More...
Data Types | |
interface | setKnnSorted |
Return the input distance matrix whose columns are sorted in ascending order on output, optionally only up to the k th row of each column, such that the k th row in the i th column is the k th nearest neighbor to the \(i^{th}\) reference point.More... | |
Variables | |
character(*, SK), parameter | MODULE_NAME = "@pm_knn" |
This module contains procedures and generic interfaces for computing the nearest neighbor statistics of random samples.
The k-nearest neighbors algorithm (k-NN) is a non-parametric supervised learning method first developed by Evelyn Fix and Joseph Hodges in 1951 and later expanded by Thomas Cover.
It is used for classification and regression and in both cases, the input consists of the k
closest training examples in a data set.
The output depends on whether k-NN is used for classification or regression:
k
nearest neighbors (k is a positive integer, typically small).k = 1
, then the object is simply assigned to the class of that single nearest neighbor.k
nearest neighbors.k = 1
, then the output is simply assigned to the value of that single nearest neighbor.k-NN is a type of classification where the function is only approximated locally and all computation is deferred until function evaluation.
Since this algorithm relies on distance for classification, if the features represent different physical units or come in vastly different scales then normalizing the training data can improve its accuracy dramatically.
The training examples are vectors in a multidimensional feature space, each with a class label.
The training phase of the algorithm consists only of storing the feature vectors and class labels of the training samples.
In the classification phase, k
is a user-defined constant, and an unlabeled vector (a query or test point) is classified by assigning the label which is most frequent among the k training samples nearest to that query point.
A commonly used distance metric for continuous variables is Euclidean distance.
For discrete variables, such as for text classification, another metric can be used, such as the overlap metric (or Hamming distance).
In the context of gene expression microarray data, for example, k-NN has been employed with correlation coefficients, such as Pearson and Spearman, as a metric.
Often, the classification accuracy of k-NN can be improved significantly if the distance metric is learned with specialized algorithms such as Large Margin Nearest Neighbor or Neighborhood components analysis.
A major drawback of the basic majority voting classification occurs when the class distribution is skewed.
That is, examples of a more frequent class tend to dominate the prediction of the new example, because they tend to be common among the k
nearest neighbors due to their large number.
One way to overcome this problem is to weight the classification, taking into account the distance from the test point to each of its k nearest neighbors.
The class (or value, in regression problems) of each of the k
nearest points is multiplied by a weight proportional to the inverse of the distance from that point to the test point.
Another way to overcome skew is by abstraction in data representation.
For example, in a self-organizing map (SOM), each node is a representative (a center) of a cluster of similar points, regardless of their density in the original training data. K-NN can then be applied to the SOM.
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
character(*, SK), parameter pm_knn::MODULE_NAME = "@pm_knn" |
Definition at line 89 of file pm_knn.F90.