iris dataset loader

This commit is contained in:
Arkadiusz Kondas 2016-04-06 21:46:17 +02:00
parent 7cbeaecffb
commit e521fb8f80
4 changed files with 114 additions and 0 deletions

View File

@ -1,9 +1,67 @@
<?php <?php
declare (strict_types = 1);
namespace Phpml\Dataset;
use Phpml\Exception\DatasetException;
abstract class Dataset abstract class Dataset
{ {
/** /**
* @var string * @var string
*/ */
protected $filepath; 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));
}
}
/**
* @return array
*/
public function getSamples()
{
return $this->samples;
}
/**
* @return array
*/
public function getLabels()
{
return $this->lables;
}
} }

View File

@ -1,9 +1,20 @@
<?php <?php
declare (strict_types = 1);
namespace Phpml\Dataset;
/**
* Classes: 3
* Samples per class: 50
* Samples total: 150
* Features per sample: 4
*/
class Iris extends Dataset class Iris extends Dataset
{ {
/** /**
* @var string * @var string
*/ */
protected $filepath = 'iris.csv'; protected $filepath = 'iris.csv';
} }

View File

@ -0,0 +1,22 @@
<?php
declare (strict_types = 1);
namespace Phpml\Exception;
class DatasetException extends \Exception
{
/**
* @return DatasetException
*/
public static function missingFile($filepath)
{
return new self(sprintf('Dataset file %s missing.', $filepath));
}
public static function cantOpenFile($filepath)
{
return new self(sprintf('Dataset file %s can\'t be open.', $filepath));
}
}

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types = 1);
namespace tests\Phpml\Dataset;
use Phpml\Dataset\Iris;
class IrisTest extends \PHPUnit_Framework_TestCase
{
public function testLoadingIrisDataset()
{
$iris = new Iris();
// whole dataset
$this->assertEquals(150, count($iris->getSamples()));
$this->assertEquals(150, count($iris->getLabels()));
// one sample features count
$this->assertEquals(4, count($iris->getSamples()[0]));
}
}