mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-22 04:55:10 +00:00
create traits for reduce complexity
This commit is contained in:
parent
6f5f190600
commit
a4ab370a48
@ -4,28 +4,25 @@ declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\Classifier;
|
||||
|
||||
use Phpml\Classifier\Traits\Predictable;
|
||||
use Phpml\Classifier\Traits\Trainable;
|
||||
use Phpml\Metric\Distance;
|
||||
use Phpml\Metric\Distance\Euclidean;
|
||||
|
||||
class KNearestNeighbors implements Classifier
|
||||
{
|
||||
use Trainable, Predictable;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $k;
|
||||
|
||||
/**
|
||||
* @var Distance
|
||||
*/
|
||||
private $distanceMetric;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $samples;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $labels;
|
||||
|
||||
/**
|
||||
* @param int $k
|
||||
* @param Distance|null $distanceMetric (if null then Euclidean distance as default)
|
||||
@ -42,35 +39,6 @@ class KNearestNeighbors implements Classifier
|
||||
$this->distanceMetric = $distanceMetric;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
* @param array $labels
|
||||
*/
|
||||
public function train(array $samples, array $labels)
|
||||
{
|
||||
$this->samples = $samples;
|
||||
$this->labels = $labels;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function predict(array $samples)
|
||||
{
|
||||
if (!is_array($samples[0])) {
|
||||
$predicted = $this->predictSample($samples);
|
||||
} else {
|
||||
$predicted = [];
|
||||
foreach ($samples as $index => $sample) {
|
||||
$predicted[$index] = $this->predictSample($sample);
|
||||
}
|
||||
}
|
||||
|
||||
return $predicted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $sample
|
||||
*
|
||||
|
@ -4,46 +4,12 @@ declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\Classifier;
|
||||
|
||||
use Phpml\Classifier\Traits\Predictable;
|
||||
use Phpml\Classifier\Traits\Trainable;
|
||||
|
||||
class NaiveBayes implements Classifier
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $samples;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $labels;
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
* @param array $labels
|
||||
*/
|
||||
public function train(array $samples, array $labels)
|
||||
{
|
||||
$this->samples = $samples;
|
||||
$this->labels = $labels;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function predict(array $samples)
|
||||
{
|
||||
if (!is_array($samples[0])) {
|
||||
$predicted = $this->predictSample($samples);
|
||||
} else {
|
||||
$predicted = [];
|
||||
foreach ($samples as $index => $sample) {
|
||||
$predicted[$index] = $this->predictSample($sample);
|
||||
}
|
||||
}
|
||||
|
||||
return $predicted;
|
||||
}
|
||||
use Trainable, Predictable;
|
||||
|
||||
/**
|
||||
* @param array $sample
|
||||
@ -67,4 +33,5 @@ class NaiveBayes implements Classifier
|
||||
|
||||
return key($predictions);
|
||||
}
|
||||
|
||||
}
|
||||
|
27
src/Phpml/Classifier/Traits/Predictable.php
Normal file
27
src/Phpml/Classifier/Traits/Predictable.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Phpml\Classifier\Traits;
|
||||
|
||||
trait Predictable
|
||||
{
|
||||
/**
|
||||
* @param array $samples
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function predict(array $samples)
|
||||
{
|
||||
if (!is_array($samples[0])) {
|
||||
$predicted = $this->predictSample($samples);
|
||||
} else {
|
||||
$predicted = [];
|
||||
foreach ($samples as $index => $sample) {
|
||||
$predicted[$index] = $this->predictSample($sample);
|
||||
}
|
||||
}
|
||||
|
||||
return $predicted;
|
||||
}
|
||||
|
||||
}
|
29
src/Phpml/Classifier/Traits/Trainable.php
Normal file
29
src/Phpml/Classifier/Traits/Trainable.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Phpml\Classifier\Traits;
|
||||
|
||||
trait Trainable
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $samples;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $labels;
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
* @param array $labels
|
||||
*/
|
||||
public function train(array $samples, array $labels)
|
||||
{
|
||||
$this->samples = $samples;
|
||||
$this->labels = $labels;
|
||||
}
|
||||
|
||||
}
|
@ -16,7 +16,7 @@ class CsvDatasetTest extends \PHPUnit_Framework_TestCase
|
||||
new CsvDataset('missingFile', 3);
|
||||
}
|
||||
|
||||
public function testSampleCsvDataset()
|
||||
public function testSampleCsvDatasetWithHeaderRow()
|
||||
{
|
||||
$filePath = dirname(__FILE__).'/Resources/dataset.csv';
|
||||
|
||||
@ -25,4 +25,14 @@ class CsvDatasetTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(10, count($dataset->getSamples()));
|
||||
$this->assertEquals(10, count($dataset->getLabels()));
|
||||
}
|
||||
|
||||
public function testSampleCsvDatasetWithoutHeaderRow()
|
||||
{
|
||||
$filePath = dirname(__FILE__).'/Resources/dataset.csv';
|
||||
|
||||
$dataset = new CsvDataset($filePath, 2, false);
|
||||
|
||||
$this->assertEquals(11, count($dataset->getSamples()));
|
||||
$this->assertEquals(11, count($dataset->getLabels()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user