php-ml/src/Dataset/CsvDataset.php

53 lines
1.3 KiB
PHP
Raw Normal View History

<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
namespace Phpml\Dataset;
use Phpml\Exception\FileException;
2016-04-07 20:19:04 +00:00
class CsvDataset extends ArrayDataset
{
/**
* @var array
*/
protected $columnNames = [];
/**
* @throws FileException
*/
2017-08-21 06:08:54 +00:00
public function __construct(string $filepath, int $features, bool $headingRow = true, string $delimiter = ',', int $maxLineLength = 0)
{
if (!file_exists($filepath)) {
throw new FileException(sprintf('File "%s" missing.', basename($filepath)));
}
$handle = fopen($filepath, 'rb');
if ($handle === false) {
throw new FileException(sprintf('File "%s" can\'t be open.', basename($filepath)));
2016-04-16 19:26:58 +00:00
}
2016-04-16 19:34:50 +00:00
if ($headingRow) {
2017-08-21 06:08:54 +00:00
$data = fgetcsv($handle, $maxLineLength, $delimiter);
2018-10-28 06:44:52 +00:00
$this->columnNames = array_slice((array) $data, 0, $features);
} else {
$this->columnNames = range(0, $features - 1);
2016-04-16 19:34:50 +00:00
}
$samples = $targets = [];
while ($data = fgetcsv($handle, $maxLineLength, $delimiter)) {
$samples[] = array_slice($data, 0, $features);
$targets[] = $data[$features];
}
2016-04-16 19:26:58 +00:00
fclose($handle);
parent::__construct($samples, $targets);
}
public function getColumnNames(): array
{
return $this->columnNames;
}
}