Classifiying the data points
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
Hi i have a data set such as;
[x y z d], (100x4) matrices
x,y and z are coordinates and the d is the measure of a distance.Every d is corresponding with one point (x, y, z)
I want to create 10 classes from d and colorize the points (x,y,z) according to this classes and plot it
is anyone have idea?
Thank you
採用された回答
Image Analyst
2019 年 7 月 2 日
Try kmeans with 10 classes, and operating on the d values:
data = rand(100, 4)
x = data(:, 1);
y = data(:, 2)
z = data(:, 3);
d = data(:, 4);
numClasses = 10
% Find classes based on 4th column.
indexes = kmeans(d, numClasses);
% Make up a colormap with 10 colors
cmap = jet(10);
% Assign a color to each data point based on it's class
markerColors = zeros(10, 3); % 10 colors.
for k = 1 : size(data, 1)
thisClassNumber = indexes(k)
markerColors(k, :) = cmap(thisClassNumber, :);
end
scatter3(x, y, z, 50, markerColors, 'filled');
fontSize = 20;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
zlabel('Z', 'FontSize', fontSize);

There is a marker at each (x,y,z) location and its color depends on which of the 10 classes that point's "d" value got classified into.
10 件のコメント
Mehmet Volkan Ozdogan
2019 年 7 月 2 日
Thank you for your answer but i get the error given below;
Error using scatter3 (line 45)
Error in color/linetype argument.
Error in siniff (line 63)
scatter3(x1, y1, z1, 50, markerColors, 'filled ');
Image Analyst
2019 年 7 月 2 日
What does this do:
whos markerColors
What does that report to the command window?
It seems like markerColors doesn't have as many colors (rows) as there are elements in your x1 vector.
Mehmet Volkan Ozdogan
2019 年 7 月 3 日
x1 have 100x1. but i need to classify "d" in 10 class according to its value. x1, y1 and z1 are only the coordinates of data point. I don't want to classify the x1, y1 or z1 they are only coordinates.So i need only ten colors for classifying "d" and show this color on related (x1 y1 z1)
Thank you
Image Analyst
2019 年 7 月 3 日
That's exactly what I did. Look at the call to kmeans(). Do you see me passing x, y, or z into kmeans()? No. You only see me passing in d. Why do you think I classified x, y, and z? Show me where those are being passed in to kmeans().
Again, answer my question about whos markerColors. Better yet, attach your entire script and data file.
Mehmet Volkan Ozdogan
2019 年 7 月 3 日
clc;clear;
x=xlsread('ping1.xlsx', 'A:A');
y=xlsread('ping1.xlsx', 'B:B');
z=xlsread('ping1.xlsx', 'C:C');
a=xlsread('ping2.xlsx', 'A:A');
b=xlsread('ping2.xlsx', 'B:B');
c=xlsread('ping2.xlsx', 'C:C');
xyz=[x y z];
abc=[a b c];
rng(1);
[idx1,C1] = kmeans(xyz,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
[idx2,C2] = kmeans(abc,100,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
for i=1:100
t(i)=i
end
t=t';
for i=1:100
u(i)=i;
end
u=u';
[dist,idx3] = pdist2(xyz, C1, 'euclidean', 'Smallest',1);
newVar = xyz(idx3 ,:);
plot3(newVar(:,1), newVar(:,2), newVar(:,3), 'bx');
text(newVar(:,1)+0.02,newVar(:,2),newVar(:,3),num2str(t),'FontSize',7);
hold on;
xlabel ('x - axis', 'fontsize', 12);
ylabel ('y - axis', 'fontsize', 12);
zlabel ('z - axis', 'fontsize', 12);
grid
[dist2,idx4] = pdist2(abc, C2, 'euclidean', 'Smallest',1);
newVar2 = abc(idx4 ,:);
%plot3(newVar2(:,1), newVar2(:,2), newVar2(:,3), 'r*');
%text(newVar2(:,1),newVar2(:,2),newVar2(:,3),num2str(u),'FontSize',7);
axis equal;
newVar3 = mean (newVar);
newVar4 = mean (newVar2),
newVar5 = (newVar3 + newVar4)/ 2;
%plot3(newVar5(:,1), newVar5(:,2), newVar5(:,3), 'go');
m=[newVar(:,1) newVar(:,2) newVar(:,3)];
n=[newVar2(:,1) newVar2(:,2) newVar2(:,3)];
o=[newVar5(:,1) newVar5(:,2) newVar5(:,3)];
d1 = pdist2(newVar5,newVar); % distance(s) (many) between O and X
[~,idx5] = pdist2(newVar2,newVar,'euclidean','smallest',1); % indices of nearest * for EACH X
d2 = pdist2(newVar5,newVar2(idx5,:)); % distance(s) between O and * (nearest to X)
D=d2 - d1
x1 = m(:, 1 );
y1 = m(:, 2 );
z1 = m(:, 3 );
numClasses = 10
% Find classes based on 4th column .
indexes = kmeans(D(:), numClasses );
% Make up a colormap with 10 colors
cmap = jet(10 );
% Assign a color to each data point based on it's class
markerColors = zeros(10, 3); % 10 colors .
for k = 1 : size(m, 1 )
thisClassNumber = indexes(k )
markerColors(k, :) = cmap(thisClassNumber , :);
end
scatter3(x1, y1, z1, 50, markerColors, 'filled ');
fontSize = 20 ;
xlabel('X', 'FontSize', fontSize );
ylabel('Y', 'FontSize', fontSize );
zlabel('Z', 'FontSize', fontSize);
Image Analyst
2019 年 7 月 3 日
編集済み: Image Analyst
2019 年 7 月 3 日
See, you're passing in the first 3 columns to kmeans, which are the xyz coordinates. I didn't do that. I extracted column 4 and passed in that because only column 4 is your "d" value that you want to do cluster analysis on. Then, you shouldn't have 100 classes since you want only 10. You should do something like:
d1=xlsread('ping1.xlsx', 'D:D');
d2=xlsread('ping2.xlsx', 'D:D');
numClasses = 10;
[idx1,C1] = kmeans(d1,numClasses,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
[idx2,C2] = kmeans(d2,numClasses,'distance','sqEuclidean','MaxIter',500, 'Replicates', 10);
Mehmet Volkan Ozdogan
2019 年 7 月 3 日
The aim of first kmeans usage is different. I created a new classed xyz called (m) and abc called (n), then i calculate the distance between m and closest n and called it "d". Now i try to classify the d values and show the color on m. it is a little bit complicated i know.
But if you remove % from;
%plot3(newVar2(:,1), newVar2(:,2), newVar2(:,3), 'r*');
and
%plot3(newVar5(:,1), newVar5(:,2), newVar5(:,3), 'go');
it may be more clear
thanks
Image Analyst
2019 年 7 月 4 日
You added a space after "filled". It makes a difference. Somehow when you were altering the line of code I gave you, you added the space.
If you use 'filled ' instead of 'filled' then you will get the error you got. If you don't, you won't get the error.
Mehmet Volkan Ozdogan
2019 年 7 月 4 日
Thank you for the answer. the problem was space. Now it is working. I have one more question, is it possible to add a legend abour classification...
thank you again for your helps
Image Analyst
2019 年 7 月 4 日
I think you'd have to do it manually with text().
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で k-Means and k-Medoids Clustering についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
