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 * * @param array $data * * @return array */ protected function reduce(array $data) { $m1 = new Matrix($data); $m2 = new Matrix($this->eigVectors); return $m1->multiply($m2->transpose())->toArray(); } }