mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-26 06:46:45 +00:00
7d5c6b15a4
* Fix typo in Features list * Update distance.md documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation * Fix grammatical mistakes in documentation
40 lines
1.0 KiB
Markdown
40 lines
1.0 KiB
Markdown
# KNearestNeighbors Classifier
|
|
|
|
Classifier implementing the k-nearest neighbors algorithm.
|
|
|
|
## Constructor Parameters
|
|
|
|
* $k - number of nearest neighbors to scan (default: 3)
|
|
* $distanceMetric - Distance object, default Euclidean (see [distance documentation](../../math/distance.md))
|
|
|
|
```
|
|
$classifier = new KNearestNeighbors($k=4);
|
|
$classifier = new KNearestNeighbors($k=3, new Minkowski($lambda=4));
|
|
```
|
|
|
|
## Train
|
|
|
|
To train a classifier, simply provide train samples and labels (as `array`). Example:
|
|
|
|
```
|
|
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
|
|
$labels = ['a', 'a', 'a', 'b', 'b', 'b'];
|
|
|
|
$classifier = new KNearestNeighbors();
|
|
$classifier->train($samples, $labels);
|
|
```
|
|
|
|
You can train the classifier using multiple data sets, predictions will be based on all the training data.
|
|
|
|
## Predict
|
|
|
|
To predict sample label use the `predict` method. You can provide one sample or array of samples:
|
|
|
|
```
|
|
$classifier->predict([3, 2]);
|
|
// return 'b'
|
|
|
|
$classifier->predict([[3, 2], [1, 5]]);
|
|
// return ['b', 'a']
|
|
```
|