2016-04-16 19:41:37 +00:00
|
|
|
# NaiveBayes Classifier
|
|
|
|
|
|
|
|
Classifier based on applying Bayes' theorem with strong (naive) independence assumptions between the features.
|
|
|
|
|
|
|
|
### Train
|
|
|
|
|
2019-11-02 10:41:34 +00:00
|
|
|
To train a classifier, simply provide train samples and labels (as `array`). Example:
|
2016-04-16 19:41:37 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
$samples = [[5, 1, 1], [1, 5, 1], [1, 1, 5]];
|
|
|
|
$labels = ['a', 'b', 'c'];
|
|
|
|
|
|
|
|
$classifier = new NaiveBayes();
|
|
|
|
$classifier->train($samples, $labels);
|
|
|
|
```
|
|
|
|
|
2017-02-01 18:06:38 +00:00
|
|
|
You can train the classifier using multiple data sets, predictions will be based on all the training data.
|
|
|
|
|
2016-04-16 19:41:37 +00:00
|
|
|
### Predict
|
|
|
|
|
2019-11-02 10:41:34 +00:00
|
|
|
To predict sample label use the `predict` method. You can provide one sample or array of samples:
|
2016-04-16 19:41:37 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
$classifier->predict([3, 1, 1]);
|
|
|
|
// return 'a'
|
|
|
|
|
2019-01-23 08:41:44 +00:00
|
|
|
$classifier->predict([[3, 1, 1], [1, 4, 1]]);
|
2016-04-16 19:41:37 +00:00
|
|
|
// return ['a', 'b']
|
|
|
|
```
|