K-means clustering - results and plotting a continuous curve
16 次查看(过去 30 天)
显示 更早的评论
I am very new to Matlab, and I'm trying to classify some data using K-means. This is what I have:
numClusters = 4;
idx_1 = kmeans([X_1 smoothY_1],numClusters,'Replicates', 5);
[numDataPoints,numDimensions] = size(smoothY_1);
Colors = hsv(numClusters);
for i = 1 : numDataPoints
plot(X_1(i),smoothY_1(i),'.','Color',Colors(idx_1(i),:))
hold on
end
The output I got was
I realized that it seems as if what the K-means clustering did was simply divide the graph into numClusters segments and that's it. I've tried with different values of numClusters and each gave me equally divided segments. Surely this can't be right?
Another question I have is about plotting the results. Both X_1 and smoothY_1 are "1825x1 double" arrays. I'm trying to plot a continuous curve, but I only have output if I use '.' in the LineSpec. Using '-' will not give me any output. How do I plot a continuous curve?
Thank you.
ETA: I have plotted the graph in line mode thanks to @Hamoon.
There are actually 3 data sets that I'm trying to cluster using K-means.
They were all generated from the same system and consists of 4 distinct operational states. It doesn't seem right to me that the 4 states are all equally divided segments. I thought it is more likely that the long segment after the biggest spike belongs to 1 cluster, rather than 3 different clusters.
Is there any clustering algorithm I should use? Or do I need to do some pre-processing before I use K-means, like perform K-means based on the difference between adjacent points, rather than on the X,Y points themselves?
Thank you.
0 个评论
回答(2 个)
Hamoon
2015-9-22
1. K-means is a clustering method, it's NOT a classification algorithm, but the way you can then use its output for association. What kind of output do you expect? If you are not happy with this output you probably don't want a clustering method.
2. you are plotting the points one by one, so '-' doesn't give you what you want, you can use this:
for i = 1 : numClusters
idxThis = idx_1==i;
plot(X_1(idxThis),smoothY_1(idxThis),'-','Color',Colors(i,:)) % It also works without '-'
hold on
end
axis([0 1800 0 15])
8 个评论
Kirby Fears
2015-9-24
Don't know enough about those algorithms to help. You'd probably need the toolbox.
I tested k-means using moments of the distribution to try identifying different modes. Pasting it here in case it works for you or helps at all.
%%setting up data with different distributions
mode{1}=2*randn(1,400)-0.5;
mode{2}=4*randn(1,400);
mode{3}=2*randn(1,400)+0.5;
mode{4}=0.5*randn(1,400)+1;
Y1=[mode{1} mode{2} mode{3} mode{4}];
clear mode;
X1=1:numel(Y1);
%%clustering
numClusters=4;
windowsize=16;
[mu, sig]=deal(NaN(1,numel(Y1)));
for iter=windowsize+1:numel(Y1),
mu(iter) = mean(Y1(iter-windowsize:iter));
sig(iter) = std(Y1(iter-windowsize:iter));
end
mu(1:windowsize)=mu(windowsize+1);
sig(1:windowsize)=sig(windowsize+1);
% Try combinations of X1, sig, and mu for clustering
idx1=kmeans(zscore([X1' sig']),numClusters,'Replicate',5);
pointclust=repmat(idx1,1,numClusters)==repmat(1:numClusters,numel(idx1),1);
colors=hsv(numClusters);
% plot index to see cluster assignments
figure(2);
plot(idx1);
% plot colored clusters
figure(1);
for j=1:numClusters,
plot(X1(pointclust(:,j)),Y1(pointclust(:,j)),'.','Color',colors(j,:));
if j==1,
hold on;
end;
end,
hold off;
Kirby Fears
2015-9-22
编辑:Kirby Fears
2015-9-22
kmeans is working exactly as expected for the input you're providing. The best 4 centroids are along your line. Perhaps you can review the wiki page to see why.
Your code calls the plot() function for each point separately. I made a few changes so you can call the plot function only once per cluster, and it plots in line mode as requested:
X1=(1:1825)';
Y1=randn(1825,1);
numClusters=4;
idx1=kmeans([X1 Y1],numClusters,'Replicates',5);
pointclust=repmat(idx1,1,numClusters)==repmat(1:numClusters,numel(idx1),1);
colors=hsv(numClusters);
for j=1:numClusters,
plot(X1(pointclust(:,j)),Y1(pointclust(:,j)),'Color',colors(j,:));
if j==1,
hold on;
end;
end,
hold off;
3 个评论
Kirby Fears
2015-9-22
Rayne,
Is X1 a time variable, and are you trying to cluster with respect to time as well?
If you're exclusively trying to cluster the "Y1" variable, you could try using kmeans with Y1 as input instead of [X1 Y1].
Since this is a one-dimensional clustering, it will simply group the Y1 values into 4 ranges: high, med-high, med-low, low.
To determine what method you'd like for categorization or clustering, you need to first be very precise about what values you want to categorize or cluster.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cluster Analysis and Anomaly Detection 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!