This was my error:
Fatal error: Uncaught TypeError: Phpml\CrossValidation\Split::__construct(): Argument #1 ($dataset) must be of type Phpml\Dataset\Dataset, array given, called in C:\xampp\htdocs\490\testing2.php on line 23 and defined in C:\xampp\htdocs\490\vendor\php-ai\php-ml\src\CrossValidation\Split.php:32 Stack trace: #0 C:\xampp\htdocs\490\testing2.php(23): Phpml\CrossValidation\Split->__construct(Array, Array, 0.3) #1 {main} thrown in C:\xampp\htdocs\490\vendor\php-ai\php-ml\src\CrossValidation\Split.php on line 32
I am trying to use php-ml linear regression to predict the price of this NYC 2019 airbnb dataset. I removed all null values and am left with columns in the order: index, room_type, price, minimum_nights, number_of_reviews, neighbourhood group. My target is price and for now I am just using minimum nights and number of reviews as my predictors. This is my code:
<?php
require 'vendor/autoload.php';
use Phpml\Regression\LeastSquares;
use Phpml\Metric\Regression;
use Phpml\CrossValidation\RandomSplit;
use Phpml\Dataset\CsvDataset;
// Load the data
$dataset = new \Phpml\Dataset\CsvDataset(filepath: "./data/NYC2019datareduced.csv", features: 5, headingRow: true);
$samples = [];
$targets = [];
foreach ($dataset->getSamples() as $sample) {
// Selecting the features (excluding the target variable 'price')
$samples[] = [$sample[3], $sample[4]];
$targets[] = $sample[2];
}
// Split the data into training and testing sets
$randomSplit = new \Phpml\CrossValidation\RandomSplit($samples, $targets, 0.3);
// Train the model
$regression = new \Phpml\Regression\LeastSquares();
$regression->train($randomSplit->getTrainSamples(), $randomSplit->getTrainLabels());
// Test the model
$predicted = $regression->predict($randomSplit->getTestSamples());
$r2 = \Phpml\Metric\Regression::r2Score($randomSplit->getTestLabels(), $predicted);
echo "R2 score: $r2\n";
// Predict the price for a new listing
$newListing = [1,45]; // Replace with your own data
$predictedPrice = $regression->predict([$newListing]);
echo "Predicted price for the new listing: $predictedPrice[0]\n";
Looking at the error message, my focus should be on line 23 with this code: $randomSplit = new \Phpml\CrossValidation\RandomSplit($samples, $targets, 0.3);
Originally it was $randomSplit = new RandomSplit($samples, $targets, 0.3)
; but I changed the syntax. I also tried to see if maybe I should replace samples and targets in the parenthesis with $dataset, but I'm not having any luck. .