读取.csv文件PHP-ML
我使用PHPML并获取一个包含6列和数千行的.csv文件,我希望每个数组的第5个元素(列)都保存在$samples中。我尝试了以下方法,它给出了每个数组的第一个元素读取.csv文件PHP-ML,php,machine-learning,php-ml,Php,Machine Learning,Php Ml,我使用PHPML并获取一个包含6列和数千行的.csv文件,我希望每个数组的第5个元素(列)都保存在$samples中。我尝试了以下方法,它给出了每个数组的第一个元素 $dataset = new CsvDataset('myNewCsvFile.csv', 1); $samples = []; foreach ($dataset->getSamples() as $sample) { $samples[] = $sample[0];
$dataset = new CsvDataset('myNewCsvFile.csv', 1);
$samples = [];
foreach ($dataset->getSamples() as $sample) {
$samples[] = $sample[0];
}
我不明白这为什么不能在我的.csv中完全读取,如果我将样本[0]的索引更改为任何其他索引,则会出现以下错误:
Notice: Undefined offset: 1 in C:\xampp\htdocs\test\index.php on line 19
这对我来说意味着它只读取每行的第一列,如果我打印出数组样本,这就证明了我得到了我期望的结果,如下所示:
Array
(
[0] => 1157
[1] => 1157
[2] => 1157
[3] => 1157
[4] => 1157
[5] => 1157
[6] => 1157
[7] => 1157
[8] => 1157
[9] => 1157
[10] => 1157
[11] => 1157
[12] => 1157
[13] => 1157
[14] => 1157
[15] => 1157
[16] => 1157
[17] => 1157
[18] => 1157
[19] => 1157
[20] => 1157
[21] => 1157
[22] => 1157
等等,这是正确的。有人能解释为什么我的完整.csv文件中没有读到这个吗 作为,
您应该向构造函数传递许多列作为第二个参数读取
你只需要一个专栏。在下面的代码片段中,我添加了另一个数字
以获取更多列
use Phpml\Dataset\CsvDataset;
// the third argument makes it ignore the heading row
$dataset = new CsvDataset('myNewCsvFile.csv',4,true);
foreach ($dataset->getSamples() as $sample) {
print_r($sample);
}
产出:
Array
(
[0] => 101
[1] => 201
[2] => 301
[3] => 401
)
Array
(
[0] => 102
[1] => 202
[2] => 302
[3] => 402
)
而myNewCsvFile.csv
是
┌─────┬─────┬─────┬─────┬─────┬─────┐
│ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │
├─────┼─────┼─────┼─────┼─────┼─────┤
│ 101 │ 201 │ 301 │ 401 │ 501 │ 601 │
│ 102 │ 202 │ 302 │ 402 │ 502 │ 602 │
└─────┴─────┴─────┴─────┴─────┴─────┘
作为,,
您应该向构造函数传递许多列作为第二个参数读取
你只需要一个专栏。在下面的代码片段中,我添加了另一个数字
以获取更多列
use Phpml\Dataset\CsvDataset;
// the third argument makes it ignore the heading row
$dataset = new CsvDataset('myNewCsvFile.csv',4,true);
foreach ($dataset->getSamples() as $sample) {
print_r($sample);
}
产出:
Array
(
[0] => 101
[1] => 201
[2] => 301
[3] => 401
)
Array
(
[0] => 102
[1] => 202
[2] => 302
[3] => 402
)
而myNewCsvFile.csv
是
┌─────┬─────┬─────┬─────┬─────┬─────┐
│ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │
├─────┼─────┼─────┼─────┼─────┼─────┤
│ 101 │ 201 │ 301 │ 401 │ 501 │ 601 │
│ 102 │ 202 │ 302 │ 402 │ 502 │ 602 │
└─────┴─────┴─────┴─────┴─────┴─────┘