diff --git a/docs/machine-learning/classification/knearestneighbors.md b/docs/machine-learning/classification/knearestneighbors.md index 569c48b..7d16828 100644 --- a/docs/machine-learning/classification/knearestneighbors.md +++ b/docs/machine-learning/classification/knearestneighbors.md @@ -5,9 +5,11 @@ Classifier implementing the k-nearest neighbors algorithm. ### Constructor Parameters * $k - number of nearest neighbors to scan (default: 3) +* $distanceMetric - Distance class, default Euclidean (see Distance Metric documentation) ``` $classifier = new KNearestNeighbors($k=4); +$classifier = new KNearestNeighbors($k=3, new Minkowski($lambda=4)); ``` ### Train diff --git a/docs/machine-learning/classification/naivebayes.md b/docs/machine-learning/classification/naivebayes.md new file mode 100644 index 0000000..c700106 --- /dev/null +++ b/docs/machine-learning/classification/naivebayes.md @@ -0,0 +1,27 @@ +# NaiveBayes Classifier + +Classifier based on applying Bayes' theorem with strong (naive) independence assumptions between the features. + +### Train + +To train a classifier simply provide train samples and labels (as `array`): + +``` +$samples = [[5, 1, 1], [1, 5, 1], [1, 1, 5]]; +$labels = ['a', 'b', 'c']; + +$classifier = new NaiveBayes(); +$classifier->train($samples, $labels); +``` + +### Predict + +To predict sample class use `predict` method. You can provide one sample or array of samples: + +``` +$classifier->predict([3, 1, 1]); +// return 'a' + +$classifier->predict([[3, 1, 1], [1, 4, 1]); +// return ['a', 'b'] +``` diff --git a/mkdocs.yml b/mkdocs.yml index 92240e7..55d1914 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -4,6 +4,7 @@ pages: - Machine Learning: - Classification: - KNearestNeighbors: machine-learning/classification/knearestneighbors.md + - NaiveBayes: machine-learning/classification/naivebayes.md - Cross Validation: - RandomSplit: machine-learning/cross-validation/randomsplit.md - Datasets: diff --git a/src/Phpml/Classifier/NaiveBayes.php b/src/Phpml/Classifier/NaiveBayes.php index ba3881b..4eeb865 100644 --- a/src/Phpml/Classifier/NaiveBayes.php +++ b/src/Phpml/Classifier/NaiveBayes.php @@ -33,5 +33,4 @@ class NaiveBayes implements Classifier return key($predictions); } - } diff --git a/src/Phpml/Classifier/Traits/Predictable.php b/src/Phpml/Classifier/Traits/Predictable.php index 7c22585..8090f8c 100644 --- a/src/Phpml/Classifier/Traits/Predictable.php +++ b/src/Phpml/Classifier/Traits/Predictable.php @@ -1,5 +1,6 @@ samples = $samples; $this->labels = $labels; } - } diff --git a/src/Phpml/Dataset/CsvDataset.php b/src/Phpml/Dataset/CsvDataset.php index f2c8ca7..7d1f91e 100644 --- a/src/Phpml/Dataset/CsvDataset.php +++ b/src/Phpml/Dataset/CsvDataset.php @@ -26,7 +26,7 @@ class CsvDataset extends ArrayDataset throw DatasetException::missingFile(basename($filepath)); } - if(false === $handle = fopen($filepath, 'r')) { + if (false === $handle = fopen($filepath, 'r')) { throw DatasetException::cantOpenFile(basename($filepath)); }