How to cluster 1-d data using KDE
21 次查看(过去 30 天)
显示 更早的评论
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.
I got the idea to use KDE from this post in Stack Overflow. https://stackoverflow.com/questions/35094454/how-would-one-use-kernel-density-estimation-as-a-1d-clustering-method-in-scikit/35151947#35151947
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?
0 个评论
采纳的回答
Taylor
2024-4-15
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;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!