KNN.Classifier<'a, 'l> Type

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`.

Example


 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 fields

Record Field Description

labeledPoints

Full Usage: labeledPoints

Field type: ('a * 'l) array
Modifiers: mutable
Field type: ('a * 'l) array

Constructors

Constructor Description

Classifier(distance, k)

Full Usage: Classifier(distance, k)

Parameters:
Returns: Classifier<'a, 'l>
distance : Distance<'a>
k : int
Returns: Classifier<'a, 'l>

Instance members

Instance member Description

this.K

Full Usage: this.K

this.OverwriteK

Full Usage: this.OverwriteK

Parameters:
    k : int - The _positive_ number of nearest neighbors from x to look for.

Returns: int option

Overwrite the parameter `k` from the constructor.

k : int

The _positive_ number of nearest neighbors from x to look for.

Returns: int option

this.fit labeledPoints

Full Usage: this.fit labeledPoints

Parameters:
    labeledPoints : Map<'l, 'a array> - the array of classified (or labeled) points [in the format "Map"], used for the classification

Fit the constructed `KNN.Classifier`, i.e. provide the points with their labels, used for the prediction.

labeledPoints : Map<'l, 'a array>

the array of classified (or labeled) points [in the format "Map"], used for the classification

Example


 // .. construct the knnClassifier before ..
 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 labeledPoints = Map [ "blue", blues; "red", reds ]
 knnClassifier.fit(labeledPoints)

this.fit (points, labels)

Full Usage: this.fit (points, labels)

Parameters:
    points : '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.

points : 'a array

the array of points

labels : 'l array

the array of labels for the `points`

Example


 // .. construct the knnClassifier before ..
 let points = seq { [ 2.0; 4.0 ]; [ 1.0; 3.0 ]; [ 2.0; 4.0 ]; [ 3.0; 2.0 ]; [ 2.0; 1.0 ]; [ 5.0; 6.0 ]; [ 4.0; 5.0 ]; [ 4.0; 6.0 ]; [ 6.0; 6.0 ]; [ 5.0; 4.0 ] }
 let labels = seq { "red"; "red"; "red"; "red"; "red"; "blue"; "blue"; "blue"; "blue"; "blue" }
 knnClassifier.fit(points, labels)

this.fit labeledPoints

Full Usage: this.fit labeledPoints

Parameters:
    labeledPoints : ('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.

labeledPoints : ('a * 'l) array

the array of classified (or labeled) points [in the format (point, label)], used for the classification

Example


 // .. construct the knnClassifier before ..
 let points = seq { [ 2.0; 4.0 ]; [ 1.0; 3.0 ]; [ 2.0; 4.0 ]; [ 3.0; 2.0 ]; [ 2.0; 1.0 ]; [ 5.0; 6.0 ]; [ 4.0; 5.0 ]; [ 4.0; 6.0 ]; [ 6.0; 6.0 ]; [ 5.0; 4.0 ] }
 let labels = seq { "red"; "red"; "red"; "red"; "red"; "blue"; "blue"; "blue"; "blue"; "blue" }
 let labeledPoints = Seq.zip points labels
 knnClassifier.fit(labeledPoints)

this.predict (points, ?overwriteK)

Full Usage: this.predict (points, ?overwriteK)

Parameters:
    points : 'a array - the array of points to be classified.
    ?overwriteK : int

Returns: 'l option[]

Predict (or classify) the given collection of points.

points : 'a array

the array of points to be classified.

?overwriteK : int
Returns: 'l option[]
Example


 // .. construct and fit the knnClassifier before ..
 knnClassifier.predict [| [3.0; 3.0]; [6.0; 6.0] |] // should be: [| Some "red"; Some "blue" |]

this.predict (x, ?overwriteK)

Full Usage: this.predict (x, ?overwriteK)

Parameters:
    x : 'a - the point to be classified.
    ?overwriteK : int

Returns: 'l option

Predict (or classify) the given point.

x : 'a

the point to be classified.

?overwriteK : int
Returns: 'l option
Example


 // .. construct and fit the knnClassifier before ..
 knnClassifier.predict [3.0; 3.0] // should be: Some "red"