public PrincipalComponents(Matrix <double> data) { var dt = data.Clone(); dt.Transpose(); sampleCov = dt * data * (1.0 / data.RowCount); //covDecomposition = new EigenvalueDecomposition(sampleCov); covDecomposition = sampleCov.Evd(); covDecomposition.Solve(sampleCov); var evs = new double[sampleCov.RowCount]; var indices = new int[sampleCov.RowCount]; for (var i = 0; i < sampleCov.RowCount; ++i) { evs[i] = covDecomposition.EigenValues[i].Real; indices[i] = i; } Array.Sort(evs, indices); var permutation = Matrix <double> .Build.Dense(sampleCov.RowCount, sampleCov.RowCount); for (var i = 0; i < sampleCov.RowCount; ++i) { permutation[indices[i], i] = 1.0; } var v = covDecomposition.EigenVectors; SortedComponents = v * permutation; SortedEigenvalues = Vector <double> .Build.DenseOfArray(evs); }
public PrincipalComponents(Matrix <double> covMatrix) { sampleCov = covMatrix; // covDecomposition = new EigenvalueDecomposition(sampleCov); covDecomposition = sampleCov.Evd(); covDecomposition.Solve(sampleCov); var evs = new double[sampleCov.RowCount]; var indices = new int[sampleCov.RowCount]; for (int i = 0; i < sampleCov.RowCount; ++i) { evs[i] = covDecomposition.EigenValues[i].Real; indices[i] = i; } Array.Sort(evs, indices); var permutation = Matrix <double> .Build.Dense(sampleCov.RowCount, sampleCov.RowCount); for (int i = 0; i < sampleCov.RowCount; ++i) { permutation[indices[i], i] = 1.0; } Matrix <double> v = covDecomposition.EigenVectors; SortedComponents = v * permutation; SortedEigenvalues = Vector <double> .Build.DenseOfArray(evs); }