001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.ml.pca; 031 032import java.util.Collection; 033 034import org.openimaj.feature.DoubleFV; 035import org.openimaj.feature.FeatureVector; 036import org.openimaj.math.matrix.algorithm.pca.PrincipalComponentAnalysis; 037import org.openimaj.math.matrix.algorithm.pca.SvdPrincipalComponentAnalysis; 038 039import Jama.Matrix; 040 041/** 042 * Principal Components Analysis wrapper for {@link FeatureVector}s. 043 * 044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 045 */ 046public class FeatureVectorPCA extends PrincipalComponentAnalysis { 047 PrincipalComponentAnalysis inner; 048 049 /** 050 * Default constructor, using an {@link SvdPrincipalComponentAnalysis}. 051 */ 052 public FeatureVectorPCA() { 053 this.inner = new SvdPrincipalComponentAnalysis(); 054 } 055 056 /** 057 * Construct with the given {@link PrincipalComponentAnalysis} object. 058 * 059 * @param inner 060 * PCA algorithm. 061 */ 062 public FeatureVectorPCA(PrincipalComponentAnalysis inner) { 063 this.inner = inner; 064 } 065 066 /** 067 * Learn the PCA basis of the given feature vectors. 068 * 069 * @param data 070 * the feature vectors to apply PCA to. 071 */ 072 public void learnBasis(FeatureVector[] data) { 073 final double[][] d = new double[data.length][]; 074 075 for (int i = 0; i < data.length; i++) { 076 d[i] = data[i].asDoubleVector(); 077 } 078 079 learnBasis(d); 080 } 081 082 /** 083 * Learn the PCA basis of the given feature vectors. 084 * 085 * @param data 086 * the feature vectors to apply PCA to. 087 */ 088 public void learnBasis(Collection<? extends FeatureVector> data) { 089 final double[][] d = new double[data.size()][]; 090 091 int i = 0; 092 for (final FeatureVector fv : data) { 093 d[i++] = fv.asDoubleVector(); 094 } 095 096 learnBasis(d); 097 } 098 099 /** 100 * Project a vector by the basis. The vector is normalised by subtracting 101 * the mean and then multiplied by the basis. 102 * 103 * @param vector 104 * the vector to project 105 * @return projected vector 106 */ 107 public DoubleFV project(FeatureVector vector) { 108 return new DoubleFV(project(vector.asDoubleVector())); 109 } 110 111 @Override 112 public void learnBasis(double[][] data) { 113 inner.learnBasis(data); 114 this.basis = inner.getBasis(); 115 this.eigenvalues = inner.getEigenValues(); 116 this.mean = inner.getMean(); 117 } 118 119 @Override 120 protected void learnBasisNorm(Matrix norm) { 121 inner.learnBasis(norm); 122 } 123 124 /** 125 * Generate a new "observation" as a linear combination of the principal 126 * components (PC): mean + PC * scaling. 127 * 128 * If the scaling vector is shorter than the number of components, it will 129 * be zero-padded. If it is longer, it will be truncated. 130 * 131 * @param scalings 132 * the weighting for each PC 133 * @return generated observation 134 */ 135 public DoubleFV generate(DoubleFV scalings) { 136 return new DoubleFV(generate(scalings.values)); 137 } 138}