Search code examples
phpcomposer-php

How to solve this issue with php ML?


Fatal error: Uncaught ArgumentCountError: Too few arguments to function Phpml\Regression\LeastSquares::predict(), 0 passed in C:\xampp\htdocs\490\testing.php on line 18 and exactly 1 expected in C:\xampp\htdocs\490\vendor\php-ai\php-ml\src\Helper\Predictable.php:12

Stack trace:
#0 C:\xampp\htdocs\490\testing.php(18): Phpml\Regression\LeastSquares->predict()
#1 {main} 
thrown in C:\xampp\htdocs\490\vendor\php-ai\php-ml\src\Helper\Predictable.php on line 12

I installed composer and php ML and this was my code:

<?php
require 'vendor/autoload.php';

//Loading the data 

$data = new \Phpml\Dataset\CsvDataset(filepath: "./data/insurance.csv", features: 1, headingRow:          true);

// preprocessing the data
$dataset = new \Phpml\CrossValidation\RandomSplit($data, testSize: 0.2, seed: 156);
//$dataset->getTrainSamples();
//$dataset->getTrainLabels();
//$dataset->getTestSamples();
//$dataset->getTestLabels();
// Training
$regression = new \Phpml\Regression\LeastSquares();
$regression->train($dataset->getTrainSamples(),$dataset->getTrainLabels());

$regression->predict();
// Evaluating machihne learning models
$score = \Phpml\Metric\Regression::r2Score($dataset->getTestLabels(),$predict);
echo "r2score is : " . $score;
// Making predictions with training models

Any help would be appreciated

I tried to see if the issue was with my file path. I'm not 100% sure with the syntax but I still receive this error.


Solution

  • For LeastSquares Linear Regression, to predict sample target value use predict method with sample to check (as array).

    For example, if the value you want to check is 123, then

    $regression->predict([123]);
    

    which may return something like 4.01 (just as an example)

    In your code, you have just used $regression->predict();, which obviously is missing the parameter.