From e521fb8f803b1855f24336f95328e99b98c24eab Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Wed, 6 Apr 2016 21:46:17 +0200 Subject: [PATCH] iris dataset loader --- src/Phpml/Dataset/Dataset.php | 58 ++++++++++++++++++++++++ src/Phpml/Dataset/Iris.php | 11 +++++ src/Phpml/Exception/DatasetException.php | 22 +++++++++ tests/Phpml/Dataset/IrisTest.php | 23 ++++++++++ 4 files changed, 114 insertions(+) create mode 100644 src/Phpml/Exception/DatasetException.php create mode 100644 tests/Phpml/Dataset/IrisTest.php diff --git a/src/Phpml/Dataset/Dataset.php b/src/Phpml/Dataset/Dataset.php index 316ee1d..cf1574e 100644 --- a/src/Phpml/Dataset/Dataset.php +++ b/src/Phpml/Dataset/Dataset.php @@ -1,9 +1,67 @@ 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() + { + return $this->samples; + } + + /** + * @return array + */ + public function getLabels() + { + return $this->lables; + } + } diff --git a/src/Phpml/Dataset/Iris.php b/src/Phpml/Dataset/Iris.php index 0565558..b862a74 100644 --- a/src/Phpml/Dataset/Iris.php +++ b/src/Phpml/Dataset/Iris.php @@ -1,9 +1,20 @@ assertEquals(150, count($iris->getSamples())); + $this->assertEquals(150, count($iris->getLabels())); + + // one sample features count + $this->assertEquals(4, count($iris->getSamples()[0])); + } + +} \ No newline at end of file