Advertisement
Guest User

KernelPCA

a guest
Apr 21st, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1.  
  2. import math as m
  3. import sys
  4. import numpy as n
  5. from numpy import *
  6. import matplotlib.pyplot as p
  7. import graph as g
  8.  
  9. d=[[0 for x in xrange(50)] for x in xrange(2)]
  10. for x in range(50):
  11.     d[0][x]=x
  12.        
  13. i=1
  14. while(i<51):
  15.     yy2=m.log(i)
  16.     d[1][i-1]=yy2
  17.     i=i+1
  18. d2=[[0 for x in xrange(50)] for x in xrange(2)]
  19. for x in range(50):
  20.     d2[0][x]=x
  21.  
  22.        
  23. i=1
  24. while(i<51):
  25.     yy2=m.log(i)-1
  26.     d2[1][i-1]=yy2
  27.     i=i+1
  28. p.plot(d[0][:],d[1][:],'x',d2[0][:],d2[1][:],'o')
  29. p.title('input data')
  30. p.show()
  31. data=hstack((d,d2))
  32.  
  33.  
  34. parameter_list = [[data,0.01,1.0], [data,0.05,2.0]]
  35.  
  36. def preprocessor_kernelpca_modular (data, threshold, width):
  37.     from shogun.Features import RealFeatures
  38.     from shogun.Preprocessor import KernelPCA
  39.     from shogun.Kernel import GaussianKernel
  40.     features = RealFeatures(data)
  41.    
  42.     kernel=GaussianKernel(features,features,width)
  43.        
  44.     preprocessor=KernelPCA(kernel)
  45.  
  46.     preprocessor.init(features)
  47.     preprocessor.set_target_dim(2)
  48.     preprocessor.apply_to_feature_matrix(features)
  49.     X=preprocessor.get_transformation_matrix()
  50.     X2=preprocessor.apply_to_feature_matrix(features)
  51. #   print 'X=',X2[2][50:100]
  52.     print 'apply to feature matrix=%d, ',n.shape(X2)
  53.     p.plot(X2,'o')
  54.     p.title('apply_to_feature_matrix')
  55.     p.show()
  56.     #sys.exit(0)
  57.     print 'type of features=%',(type(X))
  58.     print 'le X=\n',len(X[0])
  59.     print 'size of the original data is= ,and the returned matrix is',n.shape(data),n.shape(X2)
  60.     lx0=len(X2)
  61.     lx1=len(X2[0])
  62.     modified_d1=[[0 for x in xrange(len(d[0]))] for x in xrange(lx0)]
  63.     modified_d2=[[0 for x in xrange(int(len(d2[0])))] for x in xrange(lx0)]
  64.        
  65.  
  66.     #print 'X2[:][len(d[0]):]',X2
  67.     for i in range(lx0):
  68.         for j in range(len(d[0])):
  69.             modified_d1[i][j]=X2[i][j]
  70.     for i in range(lx0):
  71.         for j in range(lx1-len(d[0])):
  72.             modified_d2[i][j]=X2[i][j+len(d[0])]       
  73.     p.plot(modified_d1,'o',modified_d2,'x')
  74.     p.title('final data')
  75.     p.show()   
  76.                
  77.     return features
  78.  
  79.  
  80.  
  81. if __name__=='__main__':
  82.     print('KernelPCA')
  83.     preprocessor_kernelpca_modular(*parameter_list[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement