pca.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 #  \author Advait Jain (Healthcare Robotics Lab, Georgia Tech.)
00029 
00030 
00031 #
00032 # standard stuff, but with a bit of visualization.
00033 #
00034 
00035 import numpy as np, math
00036 
00037 # @return numpy matrix where each column is a new sample.
00038 def sample_gaussian(mean, cov, n_samples):
00039     mean = np.array(mean).flatten()
00040     cov = np.array(cov)
00041     s = np.random.multivariate_normal(mean, cov, (n_samples,))
00042     return np.matrix(s).T
00043 
00044 # x - numpy matrix. each column is a new datapoint
00045 def pca(x):
00046     U, sig, _ = np.linalg.svd(np.matrix(np.cov(x)))
00047     return U, sig
00048 
00049 # untested.
00050 def dimen_reduc(x, n_dim):
00051     U, sig = pca(x)
00052     proj_mat = np.matrix(U[:,0:n_dim])
00053     mn = np.mean(x, 1)
00054     x_projected = proj_mat.T * (x - mn)
00055     return x_projected.A
00056 
00057 
00058 if __name__ == '__main__':
00059     import matplotlib.pyplot as pp
00060     import roslib; roslib.load_manifest('hrl_lib')
00061     import hrl_lib.matplotlib_util as mpu
00062 
00063     pp.axis('equal')
00064 
00065     mn = np.matrix((1., 5.)).T
00066     cov = np.matrix([[2.,50.],[50, 100]])
00067 
00068     s = sample_gaussian(mn, cov, 1000)
00069     P = np.cov(s)
00070     mpu.plot_ellipse_cov(mn, P, 'k', 'w')
00071 
00072     pp.plot(s[0,:].A1, s[1,:].A1, '.y', ms=3)
00073 
00074     U, sig = pca(s)
00075     mn = np.mean(s, 1)
00076     pp.plot(mn[0,:].A1, mn[1,:].A1, 'ok', ms=5)
00077 
00078     p1 = mn + U[:,0] * math.sqrt(sig[0])
00079     p2 = mn + U[:,1] * math.sqrt(sig[1])
00080     pp.plot([p1[0,0], mn[0,0], p2[0,0]], [p1[1,0], mn[1,0], p2[1,0]],
00081             'k-', linewidth=2)
00082 
00083     pp.show()
00084 
00085 
00086 
00087 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06