/** * Select a subset of the principal components using a * {@link ComponentSelector}. Calling this method throws away any extra * basis vectors and eigenvalues. * * @param selector * the {@link ComponentSelector} to apply */ public void selectSubset(ComponentSelector selector) { selector.select(this); }
@Override public void select(PrincipalComponentAnalysis pca) { pca.selectSubset(n); } }
@Override public void learnBasis(double[][] data) { inner.learnBasis(data); this.basis = inner.getBasis(); this.eigenvalues = inner.getEigenValues(); this.mean = inner.getMean(); }
/** * Construct a {@link PointDistributionModel} from the given data and * {@link Constraint}. * * @param constraint * @param data */ public PointDistributionModel(Constraint constraint, List<PointList> data) { this.constraint = constraint; // align mean = GeneralisedProcrustesAnalysis.alignPoints(data, 5, 10); // build data matrix final Matrix m = buildDataMatrix(data); // perform pca this.pc = new SvdPrincipalComponentAnalysis(); pc.learnBasis(m); numComponents = this.pc.getEigenValues().length; }
/** * Construct with the given number of principal components. * * @param numComponents * the number of PCs */ public EigenImages(int numComponents) { this.numComponents = numComponents; pca = new FeatureVectorPCA(new ThinSvdPrincipalComponentAnalysis(numComponents)); }
@Override protected void learnBasisNorm(Matrix norm) { inner.learnBasis(norm); }
/** * Set the number of components of the PDM * * @param n * number of components */ public void setNumComponents(int n) { pc.selectSubset(n); numComponents = this.pc.getEigenValues().length; }
/** * Select a subset of the principal components such that all remaining * components have a cumulative energy less than the given value. * * Calling this method throws away any extra basis vectors and eigenvalues. * * @param threshold * threshold on the cumulative energy. */ public void selectSubsetEnergyThreshold(double threshold) { final double[] energy = getCumulativeEnergies(); for (int i = 1; i < energy.length; i++) { if (energy[i] < threshold) { selectSubset(i - 1); return; } } }
/** * Default constructor, using an {@link SvdPrincipalComponentAnalysis}. */ public FeatureVectorPCA() { this.inner = new SvdPrincipalComponentAnalysis(); }
/** * Learn the principal components of the given data matrix. Each row * corresponds to an observation with the number of dimensions equal to the * number of columns. * * @param data * the data */ public void learnBasis(Matrix data) { final Matrix norm = this.buildNormalisedDataMatrix(data); learnBasisNorm(norm); }
private static void projectPCA( MemoryLocalFeatureList<FloatDSIFTKeypoint> loadDSIFT, PrincipalComponentAnalysis pca) { for (final FloatDSIFTKeypoint kp : loadDSIFT) { kp.descriptor = ArrayUtils.convertToFloat(pca.project(ArrayUtils.convertToDouble(kp.descriptor))); final int nf = kp.descriptor.length; kp.descriptor = Arrays.copyOf(kp.descriptor, nf + 2); kp.descriptor[nf] = (kp.x / 125f) - 0.5f; kp.descriptor[nf + 1] = (kp.y / 160f) - 0.5f; } loadDSIFT.resetVecLength(); }
/** * Get the standard deviations (sqrt of eigenvalues) of the principal * components. * * @return vector of standard deviations */ public double[] getStandardDeviations() { return getStandardDeviations(eigenvalues.length); }
@Override public void select(PrincipalComponentAnalysis pca) { pca.selectSubsetEnergyThreshold(threshold); } }
@Override public void select(PrincipalComponentAnalysis pca) { pca.selectSubsetPercentageEnergy(percentage); } }
/** * Construct with the given number of principal components. * * @param numComponents * the number of PCs */ public EigenImages(int numComponents) { this.numComponents = numComponents; pca = new FeatureVectorPCA(new ThinSvdPrincipalComponentAnalysis(numComponents)); }
/** * Learn the principal components of the given data list. Each item * corresponds to an observation with the number of dimensions equal to the * length of the array. * * @param data * the data */ public void learnBasis(List<double[]> data) { learnBasis(data.toArray(new double[data.size()][])); }
/** * Set the number of components of the PDM using a {@link ComponentSelector} * . * * @param selector * the {@link ComponentSelector} to apply. */ public void setNumComponents(ComponentSelector selector) { pc.selectSubset(selector); numComponents = this.pc.getEigenValues().length; }
/** * Select a subset of the principal components such that all remaining * components have a certain percentage cumulative energy of the total. The * percentage is calculated relative to the total energy of the eigenvalues. * Bear in mind that if not all the eigenvalues were calculated, or if some * have previously been removed through {@link #selectSubset(int)}, * {@link #selectSubsetEnergyThreshold(double)} or * {@link #selectSubsetPercentageEnergy(double)}, then the percentage * calculation only factors in the remaining eigenvalues that are available * to it. * * Calling this method throws away any extra basis vectors and eigenvalues. * * @param percentage * percentage of the total cumulative energy to retain [0..1]. */ public void selectSubsetPercentageEnergy(double percentage) { final double[] energy = getCumulativeEnergies(); final double total = energy[energy.length - 1]; for (int i = 1; i < energy.length; i++) { if (energy[i] / total > percentage) { selectSubset(i - 1); return; } } }
/** * Compute the standard deviations of the shape components, multiplied by * the given value. * * @param multiplier * the multiplier * @return the multiplied standard deviations */ public double[] getStandardDeviations(double multiplier) { final double[] rngs = pc.getStandardDeviations(); for (int i = 0; i < rngs.length; i++) { rngs[i] = rngs[i] * multiplier; } return rngs; }
/** * Learn the principal components of the given data array. Each row * corresponds to an observation with the number of dimensions equal to the * number of columns. * * @param data * the data */ public void learnBasis(double[][] data) { learnBasis(new Matrix(data)); }