How can i know the points in the cluster ?
5 views (last 30 days)
Show older comments
i'm using dentist peak clustering for 5000 points and got already the clusteres shape but need to know which points belong for each cluster
function cluster_dp(filename, percent)
shapeset = load(filename);
showShapeSet(shapeset);
distset = shapeset2distset(shapeset);
%dc = computeDc(distset, percent);
dc = 0.048;
fprintf('average percentage of neighbours (hard coded): %5.6f\n', percent);
fprintf('Computing Rho with gaussian kernel of radius: %12.6f\n', dc);
rhos = getLocalDensity(distset, dc);
%rho_delta = load('vc/rho_delta.txt');
%rhos = rho_delta(:, 1)';
[deltas, nneigh] = getDistanceToHigherDensity(distset, rhos);
%deltas = rho_delta(:, 2)';
showDeltas(rhos, deltas);
[min_rho, min_delta] = selectRect();
filter = (rhos > min_rho) & (deltas > min_delta);
cluster_num = sum(filter);
fprintf('rho: %f, delta: %f, number of clusters: %i \n', min_rho, min_delta, cluster_num);
ords = find(filter);
cluster = zeros(size(rhos));
color = 1;
for i = 1:size(ords, 2)
cluster(ords(i)) = color;
color = color + 1;
end
[sorted_rhos, rords] = sort(rhos, 'descend');
for i = 1:size(rords, 2)
if cluster(rords(i)) == 0
neigh_cluster = cluster(nneigh(rords(i)));
assert(neigh_cluster ~= 0, 'neigh_cluster has not assign!');
cluster(rords(i)) = neigh_cluster;
end
end
showColorShape(shapeset, cluster, cluster_num, ords);
halo = cluster;
dc_filter = (distset ~= 0) & (distset < dc);
elnum = size(distset, 1);
rhos_matrix = repmat(rhos', 1, elnum);
rhos_items = repmat(rhos, elnum, 1);
rho_filter = rhos_matrix > rhos_items;
cluster_filter = repmat(cluster', 1, elnum) ~= repmat(cluster, elnum, 1);
halo_filter = dc_filter & rho_filter & cluster_filter;
bord_rhos = zeros(size(halo_filter));
bord_rhos(halo_filter) = (rhos_matrix(halo_filter) + rhos_items(halo_filter)) / 2;
r = max(bord_rhos, [], 1);
c = max(bord_rhos, [], 2);
d = [r; c'];
e = max(d, [], 1);
[row, col, v] = find(e);
cluster_rho = zeros(cluster_num);
for i = col
if e(i) > cluster_rho(cluster(i))
cluster_rho(cluster(i)) = e(i);
end
end
for i = 1:elnum
if rhos(i) < cluster_rho(cluster(i))
halo(i) = 0;
end
end
showElementCount(cluster, halo, cluster_num);
showHaloShape(shapeset, cluster, halo, cluster_num, ords);
end
function showElementCount(cluster, halo, cluster_num)
for i = 0:cluster_num
halo_filter = (halo == i);
cluster_filter = (cluster == i);
nc = length(find(cluster_filter));
nh = length(find(halo_filter));
fprintf('CLUSTER: %i, ELEMENTS: %i, CORE: %i, HALO: %i\n', i, nc, nh, nc-nh);
end
end
function [min_rho, min_delta] = selectRect()
subplot(2,2,2);
rect = getrect
%fprintf('rect(x:%i y:%i width:%i height:%i)\n', rectangle(1), rectangle(2), rectangle(3), rectangle(4));
min_rho = rect(1);
min_delta = rect(2);
end
function showHaloShape(shapeset, cluster, halo, cluster_num, ords)
subplot(2,2,4);
hold on;
cmap = colormap;
for i = 0:cluster_num
filter = (halo == i);
x = shapeset(:, 1)';
y = shapeset(:, 2)';
xx = x(filter);
yy = y(filter);
ic = int8(i * 32.0 / cluster_num) + 1;
%fprintf('i: %d, cluster_element: %d\n', i, size(xx, 2));
tt=plot(xx, yy, 'o', 'MarkerSize', 2, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
end
for i = 1:size(ords, 2)
color = cluster(ords(i));
x = shapeset(ords(i), 1);
y = shapeset(ords(i), 2);
ic = int8(color * 64.0 / cluster_num);
tt=plot([x], [y], 'o', 'MarkerSize', 10, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
end
text = strcat('HaloShape: ', num2str(cluster_num));
title (text, 'FontSize', 15.0);
xlabel ('x');
ylabel ('y');
end
function showColorShape(shapeset, cluster, cluster_num, ords)
subplot(2,2,3);
hold on;
cmap = colormap;
color = lines(6); % Generate color values
for i = 0:cluster_num
filter = (cluster == i);
x = shapeset(:, 1)';
y = shapeset(:, 2)';
xx = x(filter);
yy = y(filter);
ic = int8(i * 32.0 / cluster_num) + 1;
%fprintf('i: %d, cluster_element: %d\n', i, size(xx, 2));
tt=plot(xx, yy, 'o', 'MarkerSize', 2, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
%C = hsv(6);
%gscatter(xx,yy,color(1:3,:))
end
for i = 1:size(ords, 2)
color = cluster(ords(i));
x = shapeset(ords(i), 1);
y = shapeset(ords(i), 2);
ic = int8(color * 64.0 / cluster_num);
tt=plot([x], [y], 'o', 'MarkerSize', 10, 'MarkerFaceColor', cmap(ic,:), 'MarkerEdgeColor', cmap(ic,:));
end
text = strcat('ColorShape: ', num2str(cluster_num));
title (text, 'FontSize', 15.0);
xlabel ('x');
ylabel ('y');
end
0 Comments
Answers (1)
Omega
on 22 Dec 2023
Hi Sam,
In your MATLAB code, you are performing clustering using a density-based algorithm, which is similar to the Density Peak Clustering (DPC) method.
To find out which points belong to each cluster, the code uses the "cluster" array, where each element's index corresponds to a point in the dataset, and the value at that index corresponds to the cluster number to which the point belongs. For example, if "cluster(10) = 3", it means that the 10th point in your dataset belongs to cluster number 3.
The "showElementCount" function already provides you with the number of points in each cluster and halo. If you want to extract the actual points that belong to each cluster, you can use the following MATLAB code snippet:
function points_in_clusters = getPointsInClusters(shapeset, cluster)
cluster_num = max(cluster); % Get the number of clusters
points_in_clusters = cell(cluster_num + 1, 1); % Initialize cell array to hold points for each cluster
for i = 0:cluster_num
cluster_filter = (cluster == i);
points_in_clusters{i+1} = shapeset(cluster_filter, :); % Extract points for cluster i
end
end
You can call this function after the clustering process is complete to get a cell array where each cell contains the points for a particular cluster:
points_in_clusters = getPointsInClusters(shapeset, cluster);
Each cell in "points_in_clusters" will then contain the (x, y) coordinates of the points belonging to that cluster, with "points_in_clusters{1}" corresponding to cluster 0, "points_in_clusters{2}" to cluster 1, and so on. Remember that MATLAB indexing starts from 1, so you have to adjust the indices when accessing the elements of a cell array or any array in MATLAB.
Hope it helps!
0 Comments
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!