See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]
1 <?php 2 3 declare(strict_types=1); 4 5 namespace Phpml\Dataset; 6 7 use Phpml\Exception\FileException; 8 9 class CsvDataset extends ArrayDataset 10 { 11 /** 12 * @var array 13 */ 14 protected $columnNames = []; 15 16 /** 17 * @throws FileException 18 */ 19 public function __construct(string $filepath, int $features, bool $headingRow = true, string $delimiter = ',', int $maxLineLength = 0) 20 { 21 if (!file_exists($filepath)) { 22 throw new FileException(sprintf('File "%s" missing.', basename($filepath))); 23 } 24 25 $handle = fopen($filepath, 'rb'); 26 if ($handle === false) { 27 throw new FileException(sprintf('File "%s" can\'t be open.', basename($filepath))); 28 } 29 30 if ($headingRow) { 31 $data = fgetcsv($handle, $maxLineLength, $delimiter); 32 $this->columnNames = array_slice((array) $data, 0, $features); 33 } else { 34 $this->columnNames = range(0, $features - 1); 35 } 36 37 $samples = $targets = []; 38 while ($data = fgetcsv($handle, $maxLineLength, $delimiter)) { 39 $samples[] = array_slice($data, 0, $features); 40 $targets[] = $data[$features]; 41 } 42 43 fclose($handle); 44 45 parent::__construct($samples, $targets); 46 } 47 48 public function getColumnNames(): array 49 { 50 return $this->columnNames; 51 } 52 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body