<commit_before><?php

declare (strict_types = 1);

namespace tests\Clustering;

use Phpml\Clustering\KMeans;

class KMeansTest extends \PHPUnit_Framework_TestCase
{
    public function testKMeansSamplesClustering()
    {
        $samples = [[1, 1], [8, 7], [1, 2], [7, 8], [2, 1], [8, 9]];

        $kmeans = new KMeans(2);
        $clusters = $kmeans->cluster($samples);

        $this->assertEquals(2, count($clusters));

        foreach ($samples as $index => $sample) {
            if (in_array($sample, $clusters[0]) || in_array($sample, $clusters[1])) {
                unset($samples[$index]);
            }
        }
        $this->assertEquals(0, count($samples));
    }

    public function testKMeansMoreSamplesClustering()
    {
        $samples = [
            [80, 55], [86, 59], [19, 85], [41, 47], [57, 58],
            [76, 22], [94, 60], [13, 93], [90, 48], [52, 54],
            [62, 46], [88, 44], [85, 24], [63, 14], [51, 40],
            [75, 31], [86, 62], [81, 95], [47, 22], [43, 95],
            [71, 19], [17, 65], [69, 21], [59, 60], [59, 12],
            [15, 22], [49, 93], [56, 35], [18, 20], [39, 59],
            [50, 15], [81, 36], [67, 62], [32, 15], [75, 65],
            [10, 47], [75, 18], [13, 45], [30, 62], [95, 79],
            [64, 11], [92, 14], [94, 49], [39, 13], [60, 68],
            [62, 10], [74, 44], [37, 42], [97, 60], [47, 73],
        ];

        $kmeans = new KMeans(4);
        $clusters = $kmeans->cluster($samples);

        $this->assertEquals(4, count($clusters));

        foreach ($samples as $index => $sample) {
            for ($i = 0; $i < 4; ++$i) {
                if (in_array($sample, $clusters[$i])) {
                    unset($samples[$index]);
                }
            }
        }
        $this->assertEquals(0, count($samples));
    }
}
<commit_msg>test kmeans init methods<commit_after><?php

declare (strict_types = 1);

namespace tests\Clustering;

use Phpml\Clustering\KMeans;

class KMeansTest extends \PHPUnit_Framework_TestCase
{
    public function testKMeansSamplesClustering()
    {
        $samples = [[1, 1], [8, 7], [1, 2], [7, 8], [2, 1], [8, 9]];

        $kmeans = new KMeans(2);
        $clusters = $kmeans->cluster($samples);

        $this->assertEquals(2, count($clusters));

        foreach ($samples as $index => $sample) {
            if (in_array($sample, $clusters[0]) || in_array($sample, $clusters[1])) {
                unset($samples[$index]);
            }
        }
        $this->assertEquals(0, count($samples));
    }

    public function testKMeansInitializationMethods()
    {
        $samples = [
            [180, 155], [186, 159], [119, 185], [141, 147], [157, 158],
            [176, 122], [194, 160], [113, 193], [190, 148], [152, 154],
            [162, 146], [188, 144], [185, 124], [163, 114], [151, 140],
            [175, 131], [186, 162], [181, 195], [147, 122], [143, 195],
            [171, 119], [117, 165], [169, 121], [159, 160], [159, 112],
            [115, 122], [149, 193], [156, 135], [118, 120], [139, 159],
            [150, 115], [181, 136], [167, 162], [132, 115], [175, 165],
            [110, 147], [175, 118], [113, 145], [130, 162], [195, 179],
            [164, 111], [192, 114], [194, 149], [139, 113], [160, 168],
            [162, 110], [174, 144], [137, 142], [197, 160], [147, 173],
        ];

        $kmeans = new KMeans(4, KMeans::INIT_KMEANS_PLUS_PLUS);
        $clusters = $kmeans->cluster($samples);
        $this->assertEquals(4, count($clusters));

        $kmeans = new KMeans(4, KMeans::INIT_RANDOM);
        $clusters = $kmeans->cluster($samples);
        $this->assertEquals(4, count($clusters));
    }
}

{'fraction_non_alphanumeric': 0.21389645776566757, 'fraction_numerical': 0.14850136239782016, 'mean_word_length': 2.2573203194321207, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '509713', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1281, 'n_words': 347}