diff --git a/src/Phpml/Dataset/CsvDataset.php b/src/Phpml/Dataset/CsvDataset.php new file mode 100644 index 0000000..6fa6b42 --- /dev/null +++ b/src/Phpml/Dataset/CsvDataset.php @@ -0,0 +1,65 @@ +filepath; + + if (!file_exists($filepath)) { + throw DatasetException::missingFile(basename($filepath)); + } + + $row = 0; + if (($handle = fopen($filepath, 'r')) !== false) { + while (($data = fgetcsv($handle, 1000, ',')) !== false) { + ++$row; + if ($row == 1) { + continue; + } + $this->samples[] = array_slice($data, 0, 4); + $this->lables[] = $data[4]; + } + fclose($handle); + } else { + throw DatasetException::cantOpenFile(basename($filepath)); + } + } + + /** + * @return array + */ + public function getSamples(): array + { + return $this->samples; + } + + /** + * @return array + */ + public function getLabels(): array + { + return $this->lables; + } +} diff --git a/src/Phpml/Dataset/Dataset.php b/src/Phpml/Dataset/Dataset.php index cf1574e..4e04931 100644 --- a/src/Phpml/Dataset/Dataset.php +++ b/src/Phpml/Dataset/Dataset.php @@ -4,64 +4,15 @@ declare (strict_types = 1); namespace Phpml\Dataset; -use Phpml\Exception\DatasetException; - -abstract class Dataset +interface Dataset { /** - * @var string + * @return array */ - protected $filepath; - - /** - * @var array - */ - private $samples = []; - - /** - * @var array - */ - private $lables = []; - - public function __construct() - { - $filepath = dirname(__FILE__) . '/../../../data/' . $this->filepath; - - if(!file_exists($filepath)) { - throw DatasetException::missingFile(basename($filepath)); - } - - $row = 0; - if (($handle = fopen($filepath, "r")) !== FALSE) { - while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { - $row++; - if($row==1) { - continue; - } - $this->samples[] = array_slice($data, 0, 4); - $this->lables[] = $data[4]; - } - fclose($handle); - } else { - throw DatasetException::cantOpenFile(basename($filepath)); - } - - } + public function getSamples(): array; /** * @return array */ - public function getSamples() - { - return $this->samples; - } - - /** - * @return array - */ - public function getLabels() - { - return $this->lables; - } - + public function getLabels(): array; } diff --git a/src/Phpml/Dataset/Iris.php b/src/Phpml/Dataset/Iris.php index b862a74..1353989 100644 --- a/src/Phpml/Dataset/Iris.php +++ b/src/Phpml/Dataset/Iris.php @@ -8,13 +8,12 @@ namespace Phpml\Dataset; * Classes: 3 * Samples per class: 50 * Samples total: 150 - * Features per sample: 4 + * Features per sample: 4. */ -class Iris extends Dataset +class Iris extends CsvDataset { /** * @var string */ protected $filepath = 'iris.csv'; - } diff --git a/src/Phpml/Exception/DatasetException.php b/src/Phpml/Exception/DatasetException.php index f12c979..7b99e64 100644 --- a/src/Phpml/Exception/DatasetException.php +++ b/src/Phpml/Exception/DatasetException.php @@ -18,5 +18,4 @@ class DatasetException extends \Exception { return new self(sprintf('Dataset file %s can\'t be open.', $filepath)); } - } diff --git a/src/Phpml/Exception/InvalidArgumentException.php b/src/Phpml/Exception/InvalidArgumentException.php index 70bf918..48006fa 100644 --- a/src/Phpml/Exception/InvalidArgumentException.php +++ b/src/Phpml/Exception/InvalidArgumentException.php @@ -13,4 +13,14 @@ class InvalidArgumentException extends \Exception { return new self('Size of given arguments not match'); } + + /** + * @param $name + * + * @return InvalidArgumentException + */ + public static function percentNotInRange($name) + { + return new self(sprintf('%s must be between 0.0 and 1.0', $name)); + } } diff --git a/tests/Phpml/Dataset/IrisTest.php b/tests/Phpml/Dataset/IrisTest.php index 6bce532..99e19ad 100644 --- a/tests/Phpml/Dataset/IrisTest.php +++ b/tests/Phpml/Dataset/IrisTest.php @@ -1,5 +1,6 @@ assertEquals(4, count($iris->getSamples()[0])); } - -} \ No newline at end of file +}