Assistance Required for Clustering Code Implementation

1 visualizzazione (ultimi 30 giorni)
Dear Matlab experts,
I am experiencing some issues with my clustering code, and I would like assistance in resolving these problems.
Thank you very much in advance.
Best regards,
J1
% MATLAB 코드 - 데이터 로드 및 클러스터링
% 1. 데이터 로드
dataPath = "C:\Users\hhyan\Desktop\분배이론\그림2\GPT data_1.csv";
opts = detectImportOptions(dataPath); % 데이터 가져오기 옵션 감지
data = readtable(dataPath, opts); % 테이블 형식으로 데이터 읽기
% 2. 분석에 사용할 컬럼 선택
selectedCols = ["GDPpercapita", "GDPgrowth", "Gini"]; % 원하는 컬럼 선택
selectedData = data(:, selectedCols); % 해당 컬럼만 추출
% 3. 데이터 전처리
selectedData = rmmissing(selectedData); % 결측치 제거
% 4. 숫자 데이터로 변환
selectedData.GDPpercapita = str2double(strrep(selectedData.GDPpercapita, ',', ''));
selectedData.GDPgrowth = str2double(selectedData.GDPgrowth);
selectedData.Gini = str2double(selectedData.Gini);
% 5. 클러스터링 수행 (예: K-Means)
k = 3; % 클러스터의 수
[idx, centroids] = kmeans(table2array(selectedData), k); % kmeans 클러스터링
% 6. 클러스터 결과를 데이터에 추가
idx_table = array2table(idx, 'VariableNames', {'Cluster'}); % 클러스터 번호를 테이블로 변환
if height(idx_table) == height(data) % 행 개수 일치 확인
data = [data idx_table]; % 기존 데이터와 병합
else
error("클러스터링 결과의 길이가 데이터셋의 행 개수와 일치하지 않습니다.");
end
% 7. 클러스터 결과 시각화
figure; % 새로운 그래프 창 열기
scatter(selectedData.GDPpercapita, selectedData.GDPgrowth, 50, idx, 'filled');
hold on;
scatter(centroids(:, 1), centroids(:, 2), 100, 'kx', 'LineWidth', 3); % 센트로이드 표시
xlabel('1인당 GDP');
ylabel('GDP 성장률');
title('국가별 클러스터링');
hold off;
% 8. 클러스터 결과 저장
writetable(data, "C:\Users\hhyan\Desktop\분배이론\그림2\Clustered_data.csv"); % 클러스터 결과 저장

Risposte (1)

Drishti
Drishti il 13 Ago 2024
Hello Redwood,
I recognize that you are facing difficulty in implementing the clustering algorithm on the provided dataset ‘GPT data_1.csv’.
I reproduced the issues in MATLAB R2024a and investigated the root cause. The conversion of the required columns into double was identified as the cause of the errors. As per the code provided, the ‘GDPpercapita’ was the only column that needs to be converted to ‘double’ from ‘cell array’.
You can refer to the below code:
%removing commas
selectedData.GDPpercapita = strrep(selectedData.GDPpercapita, ',', '');
% Initialize a new array to store the converted values
numericGDPpercapita = nan(height(selectedData), 1);
% Convert each element of the cell array to a double
for i = 1:height(selectedData)
numericGDPpercapita(i) = str2double(selectedData.GDPpercapita{i});
end
% Assign the numeric array back to the table
selectedData.GDPpercapita = numericGDPpercapita;
You can also refer to the MATLAB Documentation of ‘str2double’ function.
I have attached the plot we are getting after resolving issues from the provided code.
Hope it works.

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by