How to cluster 1-d data using KDE

22 views (last 30 days)
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?

Accepted Answer

Taylor
Taylor on 15 Apr 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;

More Answers (0)

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!