neighbors.KNeighborsClassifier
Usage
const knn = new KNeighborsClassifier();
const X = [[0, 0, 0], [0, 1, 1], [1, 1, 0], [2, 2, 2], [1, 2, 2], [2, 1, 2]];
const y = [0, 0, 0, 1, 1, 1];
knn.fit(X ,y);
console.log(knn.predict([1, 2])); // predicts 1
Constructors
Methods
Constructors
constructor
⊕ KNeighborsClassifier(__namedParameters: `object`)
Defined in neighbors/classification.ts:34
Parameters:
| Param | Type | Default | Description |
|---|---|---|---|
| options.distance | string | DIST_EUC | |
| options.k | number | 0 | |
| options.type | string | TYPE_KD |
Returns: KNeighborsClassifier
Methods
λ fit
Train the classifier with input and output data
Defined in neighbors/classification.ts:74
Parameters:
| Param | Type | Default | Description |
|---|---|---|---|
| X | unknown | Training data. | |
| y | unknown | Target data. |
Returns:
void
λ fromJSON
Restores the model from a JSON checkpoint
Defined in neighbors/classification.ts:141
Parameters:
| Param | Type | Default | Description |
|---|---|---|---|
| options.classes | any | null | |
| options.distance | any | null | |
| options.k | any | null | |
| options.tree | any | null | |
| options.type | any | null |
Returns:
void
λ predict
Predict single value from a list of data
Defined in neighbors/classification.ts:157
Parameters:
| Param | Type | Default | Description |
|---|---|---|---|
| X | T[] or T[][] | Prediction data. |
Returns:
any
λ toJSON
Return the model's state as a JSON object
Defined in neighbors/classification.ts:102
Returns:
| Param | Type | Description |
|---|---|---|
| classes | any[] | classes used for KNN tree |
| distance | any | Choice of distance function, should choose between euclidean |
| k | number | Number of neighbors to classify |
| tree | any | KNN tree |
| type | string | Type of algorithm to use, choose between kdtree(default) |