MATLAB - 分类输出

发布于 2025-01-05 17:28:39 字数 2346 浏览 1 评论 0原文

我的程序使用来自用户的一定数量的聚类的 K 均值聚类。对于这个 k=4 但我想随后通过 matlabs 朴素贝叶斯分类器运行聚类信息。

有没有办法将簇分开并将它们输入到 matlab 中不同的朴素分类器中?

朴素贝叶斯:

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

K-means:

    %% 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

My programme uses K-means clustering of a set amount of clusters from the user. For this k=4 but I would like to run the clustered information through matlabs naive bayes classifier afterwards.

Is there a way to split the clusters up and feed them into different naive classifiers in matlab?

Naive Bayes:

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

K-means:

    %% 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)

something like outputing k clusters to a format k1,k2,k3 then having the naive classifier pick those up, instead of test it would be k1,k2.. etc

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

But I just dont know how to send the output of the k clusters in matlab to some type of format? (really new to this programme)

EDIT

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-01-12 17:28:39

试试这个:

% 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)

希望这有帮助..

Try this:

% 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);

And if you just want to filter out one of the clusters out of the data you can do something like that:

K1 = X(IDX==1)

Hope that was helpful..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文