getRealEigenvalues(); $eigVects = $eig->getEigenvectors(); $totalEigVal = array_sum($eigVals); // Sort eigenvalues in descending order arsort($eigVals); $explainedVar = 0.0; $vectors = []; $values = []; foreach ($eigVals as $i => $eigVal) { $explainedVar += $eigVal / $totalEigVal; $vectors[] = $eigVects[$i]; $values[] = $eigVal; if ($this->numFeatures !== null) { if (count($vectors) == $this->numFeatures) { break; } } else { if ($explainedVar >= $this->totalVariance) { break; } } } $this->eigValues = $values; $this->eigVectors = $vectors; } /** * Returns the reduced data */ protected function reduce(array $data) : array { $m1 = new Matrix($data); $m2 = new Matrix($this->eigVectors); return $m1->multiply($m2->transpose())->toArray(); } }