mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-09-29 15:39:00 +00:00
prepare cross validation random splitter
This commit is contained in:
parent
e521fb8f80
commit
649cbdb9a6
65
src/Phpml/Dataset/CsvDataset.php
Normal file
65
src/Phpml/Dataset/CsvDataset.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace Phpml\Dataset;
|
||||||
|
|
||||||
|
use Phpml\Exception\DatasetException;
|
||||||
|
|
||||||
|
abstract class CsvDataset implements 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(): array
|
||||||
|
{
|
||||||
|
return $this->samples;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getLabels(): array
|
||||||
|
{
|
||||||
|
return $this->lables;
|
||||||
|
}
|
||||||
|
}
|
@ -4,64 +4,15 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace Phpml\Dataset;
|
namespace Phpml\Dataset;
|
||||||
|
|
||||||
use Phpml\Exception\DatasetException;
|
interface Dataset
|
||||||
|
|
||||||
abstract class Dataset
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected $filepath;
|
public function getSamples(): array;
|
||||||
|
|
||||||
/**
|
|
||||||
* @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
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getSamples()
|
public function getLabels(): array;
|
||||||
{
|
|
||||||
return $this->samples;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getLabels()
|
|
||||||
{
|
|
||||||
return $this->lables;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,12 @@ namespace Phpml\Dataset;
|
|||||||
* Classes: 3
|
* Classes: 3
|
||||||
* Samples per class: 50
|
* Samples per class: 50
|
||||||
* Samples total: 150
|
* Samples total: 150
|
||||||
* Features per sample: 4
|
* Features per sample: 4.
|
||||||
*/
|
*/
|
||||||
class Iris extends Dataset
|
class Iris extends CsvDataset
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $filepath = 'iris.csv';
|
protected $filepath = 'iris.csv';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,5 +18,4 @@ class DatasetException extends \Exception
|
|||||||
{
|
{
|
||||||
return new self(sprintf('Dataset file %s can\'t be open.', $filepath));
|
return new self(sprintf('Dataset file %s can\'t be open.', $filepath));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,4 +13,14 @@ class InvalidArgumentException extends \Exception
|
|||||||
{
|
{
|
||||||
return new self('Size of given arguments not match');
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare (strict_types = 1);
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace tests\Phpml\Dataset;
|
namespace tests\Phpml\Dataset;
|
||||||
@ -7,7 +8,6 @@ use Phpml\Dataset\Iris;
|
|||||||
|
|
||||||
class IrisTest extends \PHPUnit_Framework_TestCase
|
class IrisTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testLoadingIrisDataset()
|
public function testLoadingIrisDataset()
|
||||||
{
|
{
|
||||||
$iris = new Iris();
|
$iris = new Iris();
|
||||||
@ -19,5 +19,4 @@ class IrisTest extends \PHPUnit_Framework_TestCase
|
|||||||
// one sample features count
|
// one sample features count
|
||||||
$this->assertEquals(4, count($iris->getSamples()[0]));
|
$this->assertEquals(4, count($iris->getSamples()[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user