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;
            }

            $variance = Stats::variance($dataset->labels());

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

        return $impurity;
    }