简体   繁体   中英

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]);

The above code is from php machine library named php ml. The sample and label are hardcoded in the above code. What i want to do is fill the $sample array from database. But the problem i am seeing is i cannot figure it out as you can see its $sample = [[],[],[]] . Is it array with in an array? And how to populate it

I have populated the $label successfully from db.

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

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

You can use recursive iteration to flatten your array. This will work for you based on your example data.

On another note, I would try to manipulate your query to provide you the results in the proper format.

This solution creates an unnecessary tax on your resources having to iterate across your array, which I am assuming is going to be fairly large, when a proper query will eliminate the need for this altogether.

Try this:

$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>';

This will output:

Sample:

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

Labels

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

Good luck!

That is how we can accomplish it. Thank you everyone

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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