php-ml/docs/machine-learning/classification/k-nearest-neighbors.md
David Monllaó c1b1a5d6ac Support for multiple training datasets (#38)
* Multiple training data sets allowed

* Tests with multiple training data sets

* Updating docs according to #38

Documenting all models which predictions will be based on all
training data provided.

Some models already supported multiple training data sets.
2017-02-01 19:06:38 +01:00

1023 B

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)
$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 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']