'Php8 KMeans Clustering Undefined array key 0 in KMeans.php

ı was looking clustering topic in php with KMeans using php php-ai/php-ml library.

First of all my php -v output is;

PHP 8.1.5 (cli) (built: Apr 21 2022 10:14:45) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies

So ı was reading offical documentation and ı was trying simple examples. ı tried this examples;

require 'vendor/autoload.php';
use Phpml\Clustering\KMeans;


$sample1 = [[1, 1], [8, 7], [1, 2], [7, 8], [2, 1], [8, 9]];
$samples = [ 'Label1' => [1, 1], 'Label2' => [8, 7], 'Label3' => [1, 2]];

$kmeans = new KMeans(2);
$data = $kmeans->cluster($sample1);
echo json_encode($data);

When ı run this code there is no any issue/error. But then when ı change $data variable like this ı got error. My data variable; $data = $kmeans->cluster($samples);

Then ı run simple script and my error in console was;

PHP Warning:  Undefined array key 0 in 

/home/.../Desktop/Sprojects/laravelandcrawlers/crawlers/vendor/php-ai/php-ml/src/Phpml/Clustering/KMeans.php on line 48
PHP Fatal error:  Uncaught LogicException: a space dimension cannot be null or negative in /home/.../Desktop/Sprojects/laravelandcrawlers/crawlers/vendor/php-ai/php-ml/src/Phpml/Clustering/KMeans/Space.php:25
Stack trace:
#0 /home/.../Desktop/Sprojects/laravelandcrawlers/crawlers/vendor/php-ai/php-ml/src/Phpml/Clustering/KMeans.php(48): Phpml\Clustering\KMeans\Space->__construct()
#1 /home/.../Desktop/Sprojects/laravelandcrawlers/crawlers/firstcrawler.php(49): Phpml\Clustering\KMeans->cluster()
#2 {main}
  thrown in /home/.../Desktop/Sprojects/laravelandcrawlers/crawlers/vendor/php-ai/php-ml/src/Phpml/Clustering/KMeans/Space.php on line 25

How can we solve this issue? Why we got error when ı used $samples array? ı was researching about it but did not find any solutions or helpful resource so that ı want to open question there.

--update

According to this link ı guess there is no support for >=8.0 thats why we got issue ı guess. So ı wonder if there is a any library we can use for clustering? or even if there is a exist library about this topic?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source