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" |]
module Map from Microsoft.FSharp.Collections
--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...
--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
Record fields
| Record Field | Description |
Full Usage:
labeledPoints
Field type: ('a * 'l) array
Modifiers: mutable |
|
Constructors
| Constructor | Description |
Full Usage:
Classifier(distance, k)
Parameters:
Distance<'a>
k : int
Returns: Classifier<'a, 'l>
|
|
Instance members
| 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
|
Example
val reds: float list array
val blues: float list array
val labeledPoints: Map<string,float list array>
Multiple items
module Map from Microsoft.FSharp.Collections -------------------- type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ... -------------------- new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> |
Full Usage:
this.fit (points, labels)
Parameters:
'a array
-
the array of points
labels : 'l array
-
the array of labels for the `points`
|
Fails if `points` and `labels` do not have the same length.
Example
val points: float list seq
Multiple items
val seq: sequence: 'T seq -> 'T seq -------------------- type 'T seq = System.Collections.Generic.IEnumerable<'T> val labels: string seq
|
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
|
Example
val points: float list seq
Multiple items
val seq: sequence: 'T seq -> 'T seq -------------------- type 'T seq = System.Collections.Generic.IEnumerable<'T> val labels: string seq
val labeledPoints: (float list * string) seq
module Seq
from Microsoft.FSharp.Collections
val zip: source1: 'T1 seq -> source2: 'T2 seq -> ('T1 * 'T2) seq
|
Full Usage:
this.predict (points, ?overwriteK)
Parameters:
'a array
-
the array of points to be classified.
?overwriteK : int
-
Returns: 'l option array
|
Example
|
Full Usage:
this.predict (x, ?overwriteK)
Parameters:
'a
-
the point to be classified.
?overwriteK : int
-
Returns: 'l option
|
FSharp.Stats