フィルターのクリア

How to cluster 1-d data using KDE

17 ビュー (過去 30 日間)
Eunice Chieng
Eunice Chieng 2024 年 4 月 15 日
コメント済み: Eunice Chieng 2024 年 4 月 15 日
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?

採用された回答

Taylor
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;
  1 件のコメント
Eunice Chieng
Eunice Chieng 2024 年 4 月 15 日
Thank you! This was really helpful.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by