mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-22 13:05:10 +00:00
update classifier docs
This commit is contained in:
parent
5170c10773
commit
d2e0ce446c
@ -5,9 +5,11 @@ Classifier implementing the k-nearest neighbors algorithm.
|
|||||||
### Constructor Parameters
|
### Constructor Parameters
|
||||||
|
|
||||||
* $k - number of nearest neighbors to scan (default: 3)
|
* $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=4);
|
||||||
|
$classifier = new KNearestNeighbors($k=3, new Minkowski($lambda=4));
|
||||||
```
|
```
|
||||||
|
|
||||||
### Train
|
### Train
|
||||||
|
27
docs/machine-learning/classification/naivebayes.md
Normal file
27
docs/machine-learning/classification/naivebayes.md
Normal file
@ -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']
|
||||||
|
```
|
@ -4,6 +4,7 @@ pages:
|
|||||||
- Machine Learning:
|
- Machine Learning:
|
||||||
- Classification:
|
- Classification:
|
||||||
- KNearestNeighbors: machine-learning/classification/knearestneighbors.md
|
- KNearestNeighbors: machine-learning/classification/knearestneighbors.md
|
||||||
|
- NaiveBayes: machine-learning/classification/naivebayes.md
|
||||||
- Cross Validation:
|
- Cross Validation:
|
||||||
- RandomSplit: machine-learning/cross-validation/randomsplit.md
|
- RandomSplit: machine-learning/cross-validation/randomsplit.md
|
||||||
- Datasets:
|
- Datasets:
|
||||||
|
@ -33,5 +33,4 @@ class NaiveBayes implements Classifier
|
|||||||
|
|
||||||
return key($predictions);
|
return key($predictions);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types = 1);
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace Phpml\Classifier\Traits;
|
namespace Phpml\Classifier\Traits;
|
||||||
|
|
||||||
@ -23,5 +24,4 @@ trait Predictable
|
|||||||
|
|
||||||
return $predicted;
|
return $predicted;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types = 1);
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace Phpml\Classifier\Traits;
|
namespace Phpml\Classifier\Traits;
|
||||||
|
|
||||||
trait Trainable
|
trait Trainable
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
@ -25,5 +25,4 @@ trait Trainable
|
|||||||
$this->samples = $samples;
|
$this->samples = $samples;
|
||||||
$this->labels = $labels;
|
$this->labels = $labels;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ class CsvDataset extends ArrayDataset
|
|||||||
throw DatasetException::missingFile(basename($filepath));
|
throw DatasetException::missingFile(basename($filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(false === $handle = fopen($filepath, 'r')) {
|
if (false === $handle = fopen($filepath, 'r')) {
|
||||||
throw DatasetException::cantOpenFile(basename($filepath));
|
throw DatasetException::cantOpenFile(basename($filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user