问题
I am trying to compare various classifiers on my data, such as LDA and SVM etc, by visually investigate the separation hyperplane.
Currently I am using ClassificationDiscriminant as the LDA classifier, unlike SVM can draw the hyperplane on the graph, I could not find a way to plot the hyperplane of the LDA classifier.
The following script is how I produce a sample data and get it been classified using ClassificationDiscriminant:
%% Data & Label
X = [randn(100,2); randn(150,2) + 1.5];
Y = [zeros(100,1); ones(150,1)];
%% Plot
gscatter(X(:,1),X(:,2),Y);
%% Train LDA Classifier
C = ClassificationDiscriminant.fit(X,Y);
Can anyone please help me to plot the hyperplane of separation from C
? Any suggestion would be great help.
Furthermore, the sample above is in 2D, however, I'm also interested in plotting them in 3D (i.e. sample data X
has three columns). It would be more great if you can help.
回答1:
Borrowing from the example in Matlab's classify()
documentation:
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))]);
Note that the above example plots the pairwise boundary between two classes in 2 dimensional space. If you have additional classes, you will have to modify Class1
and Class2
appropriately. I'm sure with some additional work you can find an extension of this function to N dimensional space.
来源:https://stackoverflow.com/questions/16344003/plotting-the-hyperplane-of-lda-classificationdiscriminant