qda plot please look at code

3 次查看(过去 30 天)
fafz1203
fafz1203 2016-9-24
评论: fafz1203 2016-9-24
Can anyone check what is wrong with this code? I want to plot a Quadratic Discriminant Analysis with a scatter plot instead I'm just getting a slightly curved line, please ignore the comments,
mu2 = [3 3];
s1 = [1 .5; .5 2];
s2 = [1 .5; .5 2];
R = chol(s1);
T = chol(s2);
tsmp1 = repmat(mu1, 1000, 1) + randn(1000,2)*R;
sampleY1 = zeros(1000,1);
% Step 3: sampleY2 means it is the label of tsmp2
tsmp2 = repmat(mu2, 1000, 1) + randn(1000,2)*T;
sampleY2 = ones(1000,1);
X = [tsmp1; tsmp2];
Y = [sampleY1; sampleY2];
gscatter(X(:,1), X(:,2), Y);
% Step1, if you want to use this method, you need two thing:
% - X as training data
% - Y as label
% C = ClassificationDiscriminant.fit(X,Y);
MdlQuadratic = fitcdiscr(X,Y,'DiscrimType','quadratic');
Class1 = 1;
Class2 = 2;
K = MdlQuadratic.Coeffs(Class1,Class2).Const;
L = MdlQuadratic.Coeffs(Class1,Class2).Linear;
Q = MdlQuadratic.Coeffs(Class1,Class2).Quadratic;
f = @(x1,x2) K + L(1)*x1 + L(2)*x2 + Q(1,1)*x1.^2 + ...
(Q(1,2)+Q(2,1))*x1.*x2 + Q(2,2)*x2.^2;
h2 = ezplot(f,[.9 7.1 0 2.5]);
h2.Color = 'r';
h2.LineWidth = 2;

回答(1 个)

Walter Roberson
Walter Roberson 2016-9-24
h2.LineStyle = 'none';
h2.Marker = '*';
  2 个评论
fafz1203
fafz1203 2016-9-24
Hey Walter, have you run my code?
fafz1203
fafz1203 2016-9-24
i want a full scatter plot of the two distributions and a quadratic decision boundary please take a look at this working code for a LINEAR decision boundary
mu2 = [3 3]; s1 = [1 .5; .5 2]; s2 = [1 .5; .5 2]; R = chol(s1); T = chol(s2); tsmp1 = repmat(mu1, 1000, 1) + randn(1000,2)*R; sampleY1 = zeros(1000,1);
% Step 3: sampleY2 means it is the label of tsmp2 tsmp2 = repmat(mu2, 1000, 1) + randn(1000,2)*T; sampleY2 = ones(1000,1);
X = [tsmp1; tsmp2]; Y = [sampleY1; sampleY2];
gscatter(X(:,1), X(:,2), Y);
% Step1, if you want to use this method, you need two thing: % - X as training data % - Y as label C = ClassificationDiscriminant.fit(X,Y);
Class1 = 1; Class2 = 2; K = C.Coeffs(Class1, Class2).Const; L = C.Coeffs(Class1, Class2).Linear; f = @(x,y) K + [x y]*L;
hold on; ezplot(f, [min(X(:,1)) max(X(:,1)) min(X(:,2)) max(X(:,2))]); hold on;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by