【问题标题】:MATLAB - Classification outputMATLAB - 分类输出
【发布时间】:2012-03-07 01:17:45
【问题描述】:

我的程序使用来自用户的一组集群的 K-means 聚类。对于这个 k=4 但我想之后通过 matlabs 朴素贝叶斯分类器运行聚类信息。

有没有办法在 matlab 中拆分集群并将它们输入到不同的朴素分类器中?

朴素贝叶斯:

class  = classify(test,training, target_class, 'diaglinear');

K 均值:

    %% generate sample data
K = 4;
numObservarations = 5000;
dimensions = 42;
%% cluster
opts = statset('MaxIter', 500, 'Display', 'iter');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, K, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',3);
%% plot data+clusters
figure, hold on
scatter3(data(:,1),data(:,2),data(:,3), 5, clustIDX, 'filled')
scatter3(clusters(:,1),clusters(:,2),clusters(:,3), 100, (1:K)', 'filled')
hold off, xlabel('x'), ylabel('y'), zlabel('z')
%% plot clusters quality
figure
[silh,h] = silhouette(data, clustIDX);
avrgScore = mean(silh);
%% Assign data to clusters
% calculate distance (squared) of all instances to each cluster centroid
D = zeros(numObservarations, K);     % init distances
for k=1:K
%d = sum((x-y).^2).^0.5
D(:,k) = sum( ((data - repmat(clusters(k,:),numObservarations,1)).^2), 2);
end
% find  for all instances the cluster closet to it
[minDists, clusterIndices] = min(D, [], 2);
% compare it with what you expect it to be
sum(clusterIndices == clustIDX)

类似于将 k 个簇输出为 k1,k2,k3 格式,然后让天真的分类器拾取它们,而不是测试它会是 k1,k2.. 等

class  = classify(k1,training, target_class, 'diaglinear');

但我只是不知道如何将 matlab 中 k 个簇的输出发送到某种格式? (对这个程序真的很陌生)

编辑

training = [1;0;-1;-2;4;0]; % this is the sample data.
target_class = ['posi';'zero';'negi';'negi';'posi';'zero'];% This should have the same number of rows as training data. The elements and the class on the same row should correspond.
% target_class are the different target classes for the training data; here 'positive' and 'negetive' are the two classes for the given training data

% Training and Testing the classifier (between positive and negative)
test = 10*randn(10,1) % this is for testing. I am generating random numbers.
class  = classify(test,training, target_class, 'diaglinear')  % This command classifies the test data depening on the given training data using a Naive Bayes classifier

% diaglinear is for naive bayes classifier; there is also diagquadratic

【问题讨论】:

    标签: matlab cluster-analysis classification bayesian k-means


    【解决方案1】:

    试试这个:

    % create 100 random points (this is the training data)
    X = rand(100,3);
    
    % cluster into 5 clusters
    K = 5;
    [IDX, C] = kmeans(X, K);
    
    % now let us say you have new data and you want 
    % to classify it based on the training:
    SAMPLE = rand(10,3);
    CLASS = classify(SAMPLE,X,IDX);
    

    如果您只想从数据中过滤掉其中一个集群,您可以执行以下操作:

    K1 = X(IDX==1)
    

    希望对您有所帮助..

    【讨论】:

    • Zenpoy 非常感谢!但是,当您使用 SAMLE 作为测试数据时,您不会使用 K1 吗?还是我混淆了测试、培训、目标类?我认为 target_class 将是每个分类行的标签,训练将是特定数据以学习如何识别,而测试数据将是第一个要验证的样本数据,如果它可以对您要查找的内容进行分类? (即针对我的特定问题的集群之一)
    • 我不确定,但我认为你混淆了一些东西。根据文档help classify: CLASS =分类(SAMPLE,TRAINING,GROUP)将SAMPLE中的每一行数据分类到TRAINING中的组之一。 SAMPLE 和 TRAINING 必须是具有相同列数的矩阵。 GROUP 是 TRAINING 的分组变量。它的唯一值定义组,每个元素定义对应的 TRAINING 行属于哪个组。 GROUP 可以是分类变量、数值向量、字符串数组或字符串元胞数组。
    • 啊等等,有多种选择是的,您可以将它们分组,但您也可以单独对它们进行分类。请参阅我上面的编辑代码。请注意,我使用训练数据训练它们并使用目标类对它们进行分类。然后我用随机数“测试”分类器。输出是分类为正数和负数的类。在我的示例中,我将简单地使用我的集群之一作为测试机制。
    • @Garrith - 我仍然不明白您为什么要针对其中一个 kmeans 集群“测试”分类器。如果您的数据训练数据是:[-100 -1 1 2] 并且您的标签是 [neg neg pos pos],则 2-means 算法会将其分类为 [1 2 2 2]。您是否期望您的分类器会在标签 2 上产生“错误”?
    • 由于我正在开发一个更复杂的系统,以上只是示例。不适合实际使用。我尝试了一个多分类系统,某些算法比其他算法更擅长识别特征。例如,我首先在 kmeans 上测试数据,然后确定根据哪些标签创建了哪些集群,然后我可以专门训练一个简单的分类器,以更加确定和准确地从特定集群中提取一个关键组件。
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 2012-07-17
    • 2013-10-21
    • 2019-01-30
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多