The [k-nearest neighbors algorithm](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm) to classify new data points into their target classes, depending on the features of their neighboring data points.
Convencience methods for using `KNN.Array.predict`, similiar to `KNeighborsClassifier` in `sklearn.neighbors`.
let reds = [| [ 2.0; 4.0 ]; [ 1.0; 3.0 ]; [ 2.0; 4.0 ]; [ 3.0; 2.0 ]; [ 2.0; 1.0 ] |]
let blues = [| [ 5.0; 6.0 ]; [ 4.0; 5.0 ]; [ 4.0; 6.0 ]; [ 6.0; 6.0 ]; [ 5.0; 4.0 ] |]
let knnClassifier = KNN.Classifier(FSharp.Stats.DistanceMetrics.euclidean, 3)
// fit the classifier and predict new points
// version 1.
let labeledPoints = Map [ "blue", blues; "red", reds ]
knnClassifier.fit(labeledPoints)
let color = knnClassifier.predict [3.0; 3.0] // should be: Some "red"
let colors = knnClassifier.predict [| [3.0; 3.0]; [6.0; 6.0] |] // should be: [| Some "red"; Some "blue" |]
// version 2.
let points = Array.append reds blues
let labels = [| "red"; "red"; "red"; "red"; "red"; "blue"; "blue"; "blue"; "blue"; "blue" |]
knnClassifier.fit(points, labels)
let color = knnClassifier.predict [3.0; 3.0] // should be: Some "red"
let colors = knnClassifier.predict [| [3.0; 3.0]; [6.0; 6.0] |] // should be: [| Some "red"; Some "blue" |]
Record Field | Description |
Full Usage:
labeledPoints
Field type: ('a * 'l) array
Modifiers: mutable |
|
Constructor | Description |
Full Usage:
Classifier(distance, k)
Parameters:
Distance<'a>
k : int
Returns: Classifier<'a, 'l>
|
|
Instance member | Description |
Full Usage:
this.K
|
|
Full Usage:
this.OverwriteK
Parameters:
int
-
The _positive_ number of nearest neighbors from x to look for.
Returns: int option
|
|
Full Usage:
this.fit labeledPoints
Parameters:
Map<'l, 'a array>
-
the array of classified (or labeled) points [in the format "Map
|
Fit the constructed `KNN.Classifier`, i.e. provide the points with their labels, used for the prediction.
Example
|
Full Usage:
this.fit (points, labels)
Parameters:
'a array
-
the array of points
labels : 'l array
-
the array of labels for the `points`
|
Fit the constructed `KNN.Classifier`, i.e. provide the points with their labels, used for the prediction. Fails if `points` and `labels` do not have the same length.
Example
|
Full Usage:
this.fit labeledPoints
Parameters:
('a * 'l) array
-
the array of classified (or labeled) points [in the format (point, label)],
used for the classification
|
Fit the constructed `KNN.Classifier`, i.e. provide the points with their labels, used for the prediction.
Example
|
Full Usage:
this.predict (points, ?overwriteK)
Parameters:
'a array
-
the array of points to be classified.
?overwriteK : int
Returns: 'l option[]
|
Example
|
Full Usage:
this.predict (x, ?overwriteK)
Parameters:
'a
-
the point to be classified.
?overwriteK : int
Returns: 'l option
|
Example
|