繁体   English   中英

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

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

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

这似乎$samples是一个数组,其中包含每个样本0、5、10等的子数组。 根据 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']));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM