2016-04-06 20:38:08 +00:00
|
|
|
<?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
|
|
|
|
*/
|
2016-04-07 20:12:36 +00:00
|
|
|
private $labels = [];
|
2016-04-06 20:38:08 +00:00
|
|
|
|
|
|
|
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);
|
2016-04-07 20:12:36 +00:00
|
|
|
$this->labels[] = $data[4];
|
2016-04-06 20:38:08 +00:00
|
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
} else {
|
|
|
|
throw DatasetException::cantOpenFile(basename($filepath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSamples(): array
|
|
|
|
{
|
|
|
|
return $this->samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getLabels(): array
|
|
|
|
{
|
2016-04-07 20:12:36 +00:00
|
|
|
return $this->labels;
|
2016-04-06 20:38:08 +00:00
|
|
|
}
|
|
|
|
}
|