在Matlab中可视化地将数据分为两类 [英] Splitting data into two classes visually in matlab

查看:334
本文介绍了在Matlab中可视化地将数据分为两类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据簇,每个簇都有x,y(坐标)和一个值来知道它的类型(1 class1,2 class 2).我已经绘制了这些数据,但我想用边界来分割这些类(视觉上).做这种事情的功能是什么.我尝试过轮廓,但没有帮助!

I have two clusters of data each cluster has x,y (coordinates) and a value to know it's type(1 class1,2 class 2).I have plotted these data but i would like to split these classes with boundary(visually). what is the function to do such thing. i tried contour but it did not help!

推荐答案

请考虑以下分类问题(使用虹膜数据集):

Consider this classification problem (using the Iris dataset):

如您所见,除了事先知道边界方程的容易分离的簇之外,找到边界并不是一件容易的事...

As you can see, except for easily separable clusters for which you know the equation of the boundary beforehand, finding the boundary is not a trivial task...

一个想法是使用判别分析函数

One idea is to use the discriminant analysis function classify to find the boundary (you have a choice between linear and quadratic boundary).

下面是一个完整的示例来说明该过程.该代码需要统计工具箱:

The following is a complete example to illustrate the procedure. The code requires the Statistics Toolbox:

%# load Iris dataset (make it binary-class with 2 features)
load fisheriris
data = meas(:,1:2);
labels = species;
labels(~strcmp(labels,'versicolor')) = {'non-versicolor'};

NUM_K = numel(unique(labels));      %# number of classes
numInst = size(data,1);             %# number of instances

%# visualize data
figure(1)
gscatter(data(:,1), data(:,2), labels, 'rb', '*o', ...
    10, 'on', 'sepal length', 'sepal width')
title('Iris dataset'), box on, axis tight

%# params
classifierType = 'quadratic';       %# 'quadratic', 'linear'
npoints = 100;
clrLite = [1 0.6 0.6 ; 0.6 1 0.6 ; 0.6 0.6 1];
clrDark = [0.7 0 0 ; 0 0.7 0 ; 0 0 0.7];

%# discriminant analysis
%# classify the grid space of these two dimensions
mn = min(data); mx = max(data);
[X,Y] = meshgrid( linspace(mn(1),mx(1),npoints) , linspace(mn(2),mx(2),npoints) );
X = X(:); Y = Y(:);
[C,err,P,logp,coeff] = classify([X Y], data, labels, classifierType);

%# find incorrectly classified training data
[CPred,err] = classify(data, data, labels, classifierType);
bad = ~strcmp(CPred,labels);

%# plot grid classification color-coded
figure(2), hold on
image(X, Y, reshape(grp2idx(C),npoints,npoints))
axis xy, colormap(clrLite)

%# plot data points (correctly and incorrectly classified)
gscatter(data(:,1), data(:,2), labels, clrDark, '.', 20, 'on');

%# mark incorrectly classified data
plot(data(bad,1), data(bad,2), 'kx', 'MarkerSize',10)
axis([mn(1) mx(1) mn(2) mx(2)])

%# draw decision boundaries between pairs of clusters
for i=1:NUM_K
    for j=i+1:NUM_K
        if strcmp(coeff(i,j).type, 'quadratic')
            K = coeff(i,j).const;
            L = coeff(i,j).linear;
            Q = coeff(i,j).quadratic;
            f = sprintf('0 = %g + %g*x + %g*y + %g*x^2 + %g*x.*y + %g*y.^2',...
                K,L,Q(1,1),Q(1,2)+Q(2,1),Q(2,2));
        else
            K = coeff(i,j).const;
            L = coeff(i,j).linear;
            f = sprintf('0 = %g + %g*x + %g*y', K,L(1),L(2));
        end
        h2 = ezplot(f, [mn(1) mx(1) mn(2) mx(2)]);
        set(h2, 'Color','k', 'LineWidth',2)
    end
end

xlabel('sepal length'), ylabel('sepal width')
title( sprintf('accuracy = %.2f%%', 100*(1-sum(bad)/numInst)) )

hold off

这篇关于在Matlab中可视化地将数据分为两类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆