Effacer les filtres
Effacer les filtres

How to cluster 1-d data using KDE

17 vues (au cours des 30 derniers jours)
Eunice Chieng
Eunice Chieng le 15 Avr 2024
Commenté : Eunice Chieng le 15 Avr 2024
Hello, I wanted to group one-dimensional data using KDE. I got the PDF using the KDE command and then found the local minimum in the PDF plot where the data is going to be split, but I'm not sure what to do next in order to output the actual clusters.
Thank you in advance for the help.
Here's the code:
[f1,xf1] = kde(input);
kdeplot = [f1, xf1];
[TF1,P] = islocalmin(f1);
plot(xf1,f1,xf1(TF1),f1(TF1),'r*')
% what comes next?

Réponse acceptée

Taylor
Taylor le 15 Avr 2024
Something like this?
% Generate sample data with two clusters
data = [randn(100,1); 5+randn(100,1)]; % 100 points around 0 and 100 points around 5
% Perform Kernel Density Estimation
[f, xi] = ksdensity(data);
% Plot the KDE
figure;
plot(xi, f);
xlabel('Data Values');
ylabel('Density');
title('Kernel Density Estimation');
% Find Clusters by Identifying Peaks
[pks, locs] = findpeaks(f, xi);
hold on;
plot(locs, pks, 'or'); % Plot peaks
legend('KDE', 'Peaks');
hold off;
% Function to find the nearest peak for a given point
function idx = findNearestPeak(point, peaks)
[~, idx] = min(abs(peaks - point));
end
% Assign points to the nearest cluster peak
clusterAssignments = arrayfun(@(x) findNearestPeak(x, locs), data);
% Visualize the Clustering Result
figure;
for i = 1:length(locs) % For each cluster
clusterData = data(clusterAssignments == i);
plot(clusterData, zeros(size(clusterData)), 'o', 'DisplayName', sprintf('Cluster %d', i));
hold on;
end
plot(locs, zeros(size(locs)), 'kx', 'MarkerSize', 10, 'DisplayName', 'Cluster Centers');
hold off;
xlabel('Data Values');
title('Data Clustering Result');
legend;
  1 commentaire
Eunice Chieng
Eunice Chieng le 15 Avr 2024
Thank you! This was really helpful.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by