mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-02-10 16:08:46 +00:00
iris dataset loader
This commit is contained in:
parent
7cbeaecffb
commit
e521fb8f80
@ -1,9 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\Dataset;
|
||||
|
||||
use Phpml\Exception\DatasetException;
|
||||
|
||||
abstract class Dataset
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,20 @@
|
||||
<?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
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $filepath = 'iris.csv';
|
||||
|
||||
}
|
||||
|
22
src/Phpml/Exception/DatasetException.php
Normal file
22
src/Phpml/Exception/DatasetException.php
Normal 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));
|
||||
}
|
||||
|
||||
}
|
23
tests/Phpml/Dataset/IrisTest.php
Normal file
23
tests/Phpml/Dataset/IrisTest.php
Normal 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]));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user