声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 5231|回复: 4

[综合讨论] 求助:核主成分KPCA的程序代码

[复制链接]
发表于 2006-9-13 15:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
小女子目前正在写一片关于绩效评价的论文,需要用到核主成分分析法,不知道有没有高手曾今做过这种程序,希望不吝指教。。。在下先谢了

[ 本帖最后由 lxq 于 2006-9-15 08:27 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-9-14 18:03 | 显示全部楼层
KPCA的提出者亲自写的程序

  1. % Kernel PCA toy example for k(x,y)=exp(-||x-y||^2/rbf_var), cf. Fig. 4 in
  2. % @article{SchSmoMue98,
  3. %   author    = "B.~{Sch"olkopf} and A.~Smola and K.-R.~{M"uller}",
  4. %   title     = "Nonlinear component analysis as a kernel Eigenvalue problem",
  5. %   journal =         {Neural Computation},
  6. %   volume    = 10,
  7. %   issue     = 5,
  8. %   pages     = "1299 -- 1319",
  9. %   year      = 1998}
  10. % This file can be downloaded from http://www.kernel-machines.org.
  11. % Last modified: 4 July 2003


  12. % parameters
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. rbf_var = 0.1;
  15. xnum = 4;
  16. ynum = 2;
  17. max_ev = xnum*ynum;
  18. % (extract features from the first <max_ev> Eigenvectors)
  19. x_test_num = 15;
  20. y_test_num = 15;
  21. cluster_pos = [-0.5 -0.2; 0 0.6; 0.5 0];
  22. cluster_size = 30;

  23. % generate a toy data set
  24. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  25. num_clusters = size(cluster_pos,1);
  26. train_num = num_clusters*cluster_size;
  27. patterns = zeros(train_num, 2);
  28. range = 1;
  29. randn('seed', 0);
  30. for i=1:num_clusters,
  31.   patterns((i-1)*cluster_size+1:i*cluster_size,1) = cluster_pos(i,1)+0.1*randn(cluster_size,1);
  32.   patterns((i-1)*cluster_size+1:i*cluster_size,2) = cluster_pos(i,2)+0.1*randn(cluster_size,1);
  33. end
  34. test_num = x_test_num*y_test_num;
  35. x_range = -range:(2*range/(x_test_num - 1)):range;
  36. y_offset = 0.5;
  37. y_range = -range+ y_offset:(2*range/(y_test_num - 1)):range+ y_offset;
  38. [xs, ys] = meshgrid(x_range, y_range);
  39. test_patterns(:, 1) = xs(:);
  40. test_patterns(:, 2) = ys(:);
  41. cov_size = train_num;  % use all patterns to compute the covariance matrix

  42. % carry out Kernel PCA
  43. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  44. for i=1:cov_size,
  45.   for j=i:cov_size,
  46.     K(i,j) = exp(-norm(patterns(i,:)-patterns(j,:))^2/rbf_var);
  47.     K(j,i) = K(i,j);
  48.   end
  49. end
  50. unit = ones(cov_size, cov_size)/cov_size;
  51. % centering in feature space!
  52. K_n = K - unit*K - K*unit + unit*K*unit;

  53. [evecs,evals] = eig(K_n);
  54. evals = real(diag(evals));
  55. for i=1:cov_size,
  56.   evecs(:,i) = evecs(:,i)/(sqrt(evals(i)));
  57. end

  58. % extract features
  59. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  60. %  do not need the following here - only need test point features
  61. %unit_train = ones(train_num,cov_size)/cov_size;
  62. %for i=1:train_num,
  63. %  for j=1:cov_size,
  64. %    K_train(i,j) = exp(-norm(patterns(i,:)-patterns(j,:))^2/rbf_var);
  65. %  end
  66. %end
  67. %K_train_n = K_train - unit_train*K - K_train*unit + unit_train*K*unit;
  68. %features = zeros(train_num, max_ev);
  69. %features = K_train_n * evecs(:,1:max_ev);

  70. unit_test = ones(test_num,cov_size)/cov_size;
  71. K_test = zeros(test_num,cov_size);
  72. for i=1:test_num,
  73.   for j=1:cov_size,
  74.     K_test(i,j) = exp(-norm(test_patterns(i,:)-patterns(j,:))^2/rbf_var);
  75.   end
  76. end
  77. K_test_n = K_test - unit_test*K - K_test*unit + unit_test*K*unit;
  78. test_features = zeros(test_num, max_ev);
  79. test_features = K_test_n * evecs(:,1:max_ev);

  80. % plot it
  81. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  82. figure(1); clf
  83. for n = 1:max_ev,
  84.   subplot(ynum, xnum, n);
  85.   axis([-range range -range+y_offset range+y_offset]);
  86.   imag = reshape(test_features(:,n), y_test_num, x_test_num);
  87.   axis('xy')
  88.   colormap(gray);
  89.   hold on;
  90.   pcolor(x_range, y_range, imag);
  91.   shading interp
  92.   contour(x_range, y_range, imag, 9, 'b');
  93.   plot(patterns(:,1), patterns(:,2), 'r.')
  94.   text(-1,1.65,sprintf('Eigenvalue=%4.3f', evals(n)));
  95.   hold off;
  96. end
复制代码

评分

1

查看全部评分

发表于 2009-11-9 07:09 | 显示全部楼层
这个代码太经典太经典 每次看都陶醉
发表于 2012-4-22 20:04 | 显示全部楼层
请问kpca的提出者是谁?有网站没有?
发表于 2012-11-21 21:03 | 显示全部楼层
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-29 02:42 , Processed in 0.057692 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表