0
$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.

neubert
  • 15,947
  • 24
  • 120
  • 212
Husnain
  • 137
  • 1
  • 9
  • Please give us a few more clues by showing us your code – RiggsFolly Nov 07 '18 at 09:21
  • @RiggsFolly edited – Husnain Nov 07 '18 at 09:26
  • Your sample size and label size are not the same. – Joseph_J Nov 07 '18 at 09:31
  • @Joseph_J you are right joseph. But that's not my question i want to populate samples from database. And what do we call $sample = [ [ ], [ ] ] why the double brackets are used? Is it getting array with in array or something i am missing in php? – Husnain Nov 07 '18 at 09:34
  • Lets see your code for your queries including the fetches for your samples. – Joseph_J Nov 07 '18 at 09:42
  • Yes, it's an array in an array. The reason why each sample value is an array is so that multiple values can be passed each data point, so more than one value can be evaluated & trained, in parallel. See Multiple Linear Regression for more info on this concept. – FredTheWebGuy Jan 20 '19 at 18:08

3 Answers3

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

This seems like $samples is an array that contains sub-arrays for each of the sample 0, 5, 10 etc. According to the NaiveBayes for PHP, the sample paramter expects array.

Osama Ibrahim
  • 148
  • 10
0

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!

Joseph_J
  • 3,654
  • 2
  • 13
  • 22
0

That is how we can accomplish it. Thank you everyone

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

        array_push($samples, array($row['result_midterm']));
    }
Husnain
  • 137
  • 1
  • 9