您的位置:首页 > 编程语言 > MATLAB

低维数据通过核函数映射到高维空间(Gaussian Radial Basis Function)

2016-07-07 15:57 549 查看
参考

下面是一段matlab代码,可以实现利用
Gaussian Radial Basis Function
将低维数据映射到高维空间,以二维数据为例:

生成一个2D平面

figure;
axis([-10 10 -10 10])
hold on
grid on;




利用鼠标在该2D平面上取两组点

初始化

red = []; %存放第一组点,红色点
blue = []; %存放第二组点,蓝色点


下面开始手动获取(即用鼠标获取)红色点和蓝色点,尽量让红色点分布在外圆环、蓝色点分布在内圆环

手动获取红色点

%% 手动获取红色点
% Loop, picking up the points for the red class.
disp('---')
disp('Click in the graph for the red points, e.g. in a wide circular form')
disp('Left mouse button picks points.')
disp('Right mouse button picks last point.')
%button flag标记,1表示鼠标左键,0表示鼠标右键
but = 1;
n = 0;
while but == 1
[xi,yi,but] = c(1);
plot(xi,yi,'ro')
n = n+1;
red(:,n) = [xi;yi];
end
% 完成红色点的生成
disp('Finished collection red points')
disp('---')




手动获取蓝色点

%% 手动获取绿色点
% Loop again, picking up the points for the blue class
disp('Now click in the graph for the blue points, e.g. in a smaller circular form')
disp('Left mouse button picks points.')
disp('Right mouse button picks last point.')
%button flag标记,1表示鼠标左键,0表示鼠标右键
but = 1;
n = 0;
while but == 1
[xi,yi,but] = ginput(1);
plot(xi,yi,'bo')
n = n+1;
blue(:,n) = [xi;yi];
end
% 完成绿色点的生成
disp('Finished collection blue points')
disp('---')




从图中可以看到,红色点和蓝色点并不是线性可分的

利用Gaussian Radial Basis Function进行映射

%% 设置sigma和gamma的取值
% sigma = input('sigma = ? (default value: 1): ');
% if isempty(sigma)
%     sigma = 1;
% end
gamma = input('gamma = ? (default value: 1):  ');
if isempty(gamma)
gamma = 1;
end

%% 投影函数,即Gaussian Radial Basis Funtion
project = @(data, sigma) sum(exp(-(squareform( pdist(data, 'euclidean') .^ 2) ./ ( 2*sigma^2))));

%% 产生投影点
blue_z = project(blue', gamma);
red_z = project(red', gamma);

%% 绘制投影点
clf;
hold on;
grid on;
scatter3(red(1,:), red(2,:), red_z, 'r');
scatter3(blue(1,:), blue(2,:), blue_z, 'b');


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matlab 机器学习 数据