如何在特定方向上投影数据

时间:2014-11-08 17:57:46

标签: matlab machine-learning

例如,我有2个数据集,它们都是2-D。 现在的问题是如何将这两个数据投影到线性方程上。

%%% Here is the data %%%
%%% The data are Gaussian distributed with means m1 and  m2, %%%
%%% and have a common covariance matrix C %%%
m1 = [0 3]'; %%% mean of data set 1
m2 = [3 2.5]';  %%% mean of data set 2
C1 = [2 1;1 2]; %%% covariance matrix %%%
C2 = [2 1;1 2]; %%% covariance matrix %%%
X1 = mvnrnd(m1,C1,N);
X2 = mvnrnd(m2,C2,N);
plot(X1(:,1),X1(:,2),'bx',X2(:,1),X2(:,2),'ro');
grid on

1 个答案:

答案 0 :(得分:2)

这是一个假设您想要对线进行正交投影的解决方案。可能不是最有效的实现,但至少步骤很容易理解。

该行由ab参数定义为:     y = a * x + b;

N = 3;
%%% Here is the data %%%
%%% The data are Gaussian distributed with means m1 and  m2, %%%
%%% and have a common covariance matrix C %%%
m1 = [0 3]'; %%% mean of data set 1
m2 = [3 2.5]';  %%% mean of data set 2
C1 = [2 1;1 2]; %%% covariance matrix %%%
C2 = [2 1;1 2]; %%% covariance matrix %%%
X1 = mvnrnd(m1,C1,N);
X2 = mvnrnd(m2,C2,N);
figure
plot(X1(:,1),X1(:,2),'bx',X2(:,1),X2(:,2),'rx');
grid on
axis equal

% equation of the line
% y = a*x + b 
a = 1;
b = 1;

% normalized directional vector of the line
s = [a, a^2];
s = s./norm(s);

% translate points for easier work
X1(:,2) = X1(:,2)-b;
X2(:,2) = X2(:,2)-b;

% projection
X1p = (X1*s')*s;
X2p = (X2*s')*s;

% tranlate the points back
X1p(:,2) = X1p(:,2)+b;
X2p(:,2) = X2p(:,2)+b;

xx = -4:8;
hold on
plot(xx, a*xx+b, 'green')
plot(X1p(:,1),X1p(:,2),'bo',X2p(:,1),X2p(:,2),'ro')
xlabel('x')
ylabel('y')
title('Orthogonal projection')

x是原始点,而o是其投影。 enter image description here