How to plot k-means output?

15 views (last 30 days)
Mnr
Mnr on 23 Mar 2014
Commented: Image Analyst on 18 Oct 2020
Hello all,
I have some data in 8 text files, I have used 5 of them as my training data and the rest as the testing data. I have classified them using k-means. I would like to visualize the output; in other words, I would like to see the membership of each data point in a graph. Can anybody help me please? Thanks in advance.

Accepted Answer

Image Analyst
Image Analyst on 23 Mar 2014
How about using scatter and have each class be plotted with different color markers? I think that's the most common way.
  4 Comments
Fatemeh Shomal Zadeh
Fatemeh Shomal Zadeh on 18 Oct 2020
Hi,
I am using your idea and takethis code, but when I am running it, my data are not spread like scatter plot. All of them are in the same line. can you help me regarding that please?
Image Analyst
Image Analyst on 18 Oct 2020
I copied and pasted the code:
x = rand(1000, 1); % 1000 random x,y points
y = rand(1000, 1); % 1000 random x,y points
% Create classes. Class 1 is if x<y. Class 2 otherwise
classes = (x < y);
% Make class 1 blue and class 2 red
classColors = zeros(length(x), 3);
markerSizes = zeros(length(x), 1);
for row = 1 : length(x)
if classes(row)
% Class 1 = blue
classColors(row,:) = [0, 0, 1];
markerSizes(row) = 10;
else
% Class 2 = red
classColors(row, :) = [1, 0, 0];
markerSizes(row) = 40;
end
end
scatter(x,y, markerSizes, classColors, 'LineWidth', 2);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
and it gives this image.
Not sure what you did to change it since you forgot to attach your modified code. Attach it with your data and code to read in your data and we'll see if we can fix it. By the way, I do have the stats toolbox now, so I can do kmeans() if needed.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!