protected function splitImpurity(array $groups) : float
    {
        $n = array_sum(array_map('count', $groups));

        $impurity = 0.;

        foreach ($groups as $dataset) {
            $k = $dataset->numRows();

            if ($k < 2) {
                continue 1;
            }

            $counts = array_count_values($dataset->labels());

            $p = 0.;
    
            foreach ($counts as $count) {
                $p += 1. - ($count / $n) ** 2;
            }

            $impurity += ($k / $n) * $p;
        }

        return $impurity;
    }