K-Nearest Neighbors algorithm
OperationsCode example:
matrix := [
[51.0, 167.0]
[58.0, 169.0]
[62.0, 182.0]
[69.0, 176.0]
[64.0, 173.0]
[65.0, 172.0]
[56.0, 174.0]
[57.0, 173.0]
[55.0, 170.0]];
labels := [
"underweight",
"normal",
"normal",
"normal",
"normal",
"underweight",
"normal",
"normal",
"normal"];
knn := KNearestNeighbors->New(matrix, labels);
nearest := knn->Query(3, [57.0, 170.0]);
each(neighbor := nearest) {
neighbor->ToString()->PrintLine();
};
Constructor.
New(matrix:Float[,], labels:String[])
Name | Type | Description |
---|---|---|
matrix | Float[,] | input matrix |
labels | String[] | categories labels |
Query the matrix for classification
method : public : Query(k:Int, query:Float[]) ~ KNeighbor[]
Name | Type | Description |
---|---|---|
k | Int | number of nearest neighbors |
query | Float[] | input query |
Type | Description |
---|---|
KNeighbor[] | k nearest neighbors |