![](/img/trans.png)
[英]How to prepare data outputted from sql in php to run through the PHP machine learning Library
[英]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.