如何在php机器学习库中填充示例

时间:2018-11-07 09:18:44

标签: php machine-learning

$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 ml的php机器库。 样本和标签在上面的代码中进行了硬编码。我想做的是从数据库中填充$ sample数组。但是我看到的问题是我无法弄清楚,因为您可以看到它的$ sample = [[],[],[]]。数组中有数组吗?以及如何填充

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

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

此解决方案对必须在整个数组中进行迭代的资源造成不必要的负担,我认为这将是相当大的,这时通过适当的查询将完全不需要这样做。

尝试一下:

$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
)

祝你好运!

答案 2 :(得分:0)

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

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

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