如何在 php 机器学习库中填充样本

How to populate sample in php machine learning library

$samples = [[0], [5], [10], [20], [25], [18], [30]];
$labels = ['fail', 'fail', 'pass', 'pass'];

$classifier = new NaiveBayes();
$classifier->train($samples, $labels);

echo $classifier->predict([14]);

以上代码来自php机器库phpml。 样本和标签在上面的代码中是硬编码的。我想要做的是从数据库中填充 $sample 数组。但我看到的问题是我无法弄清楚,因为你可以看到它的 $sample = [[],[],[]] 。它是数组中的数组吗?以及如何填充它

我已经从 db 成功填充了 $label。

$samples = [[0], [5], [10], [20], [25], [18], [30]];

这似乎 $samples 是一个包含样本 0、5、10 等的子数组的数组。 According to the NaiveBayes for PHP, 示例参数需要数组。

您可以使用递归迭代来展平数组。这将根据您的示例数据为您工作。

另一方面,我会尝试操纵您的查询,以正确的格式为您提供结果。

此解决方案对您的资源产生了不必要的负担,必须遍历您的数组,我假设这将相当大,当适当的查询将完全消除这种需要时。

试试这个:

$samples = [[0], [5], [10], [20], [25], [18], [30]];
$labels = ['fail', 'fail', 'pass', 'pass'];

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($samples));
$results = iterator_to_array($iterator, false);

echo '<pre>';
print_r($results);
print_r($labels);
echo '</pre>';

这将输出:

样本:

Array
(
    [0] => 0
    [1] => 5
    [2] => 10
    [3] => 20
    [4] => 25
    [5] => 18
    [6] => 30
)

标签

Array
(
    [0] => fail
    [1] => fail
    [2] => pass
    [3] => pass
)

祝你好运!

这就是我们可以实现的方式。谢谢大家

 while($row = mysqli_fetch_assoc($result)){

        array_push($samples, array($row['result_midterm']));
    }