我想修改样本数组和标签,然后传递给predict()函数。我必须这样做,因为我想从other.xls文件中获取样本和标签并存储在数组中,以便它们适合此代码。 这是原始代码:
$samples = [[5, 1, 1], [1, 5, 1], [1, 1, 5]];// is this array() form?
$labels = ['a', 'b', 'c'];
$classifier = new NaiveBayes();
$classifier->train($samples, $labels);
$classifier->predict([3, 1, 1]) //return a
//other file.php
predict(array $sample){
$predictions = [];
foreach ($this->targets as $index => $label) {
$predictions[$label] = 0;
foreach ($sample as $token => $count) {
if (array_key_exists($token, $this->samples[$index])) {
$predictions[$label] += $count * $this->samples[$index][$token];
}
}
}
然后我将代码修改为:
$samples = array("samples1","samples2", "samples3",...,"samplesN");
$labels = array("a","c","b");
但这让我错了:
Uncaught TypeError:array_key_exists()期望参数2为数组,字符串在
中给出
好奇这是什么类型的数组[[...]]?与普通数组(..)相同吗? 有人能帮助我吗?我确定重新制作阵列是错误的