필터 지우기
필터 지우기

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