Azzera filtri
Azzera filtri

Assistance Required for Clustering Code Implementation

3 visualizzazioni (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 (0)

Categorie

Scopri di più su Introduction to Installation and Licensing 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