2

Ich implementiere den K-Means-Algorithmus für gegebene 4-dimensionale Daten mit k = # von Cluster und ich laufe ungefähr 5 Mal mit verschiedenen Anfangspunkten.Wie berechnet man die Summe der quadrierten Fehler in k-Mittelwert Clustering Matlab?

  1. Wie kann ich die Gesamtsumme der quadratischen Fehler (SSE) nach jedem Lauf berechnen?

4 Dimention 1 to 4 and blow 
 
x1 \t 1 \t 2 \t 3 \t 4 
 
x2 \t 5 \t 6 \t 7 \t 8 
 
x3 \t 9 \t 10 \t 11 \t 12 
 
x4 \t 13 \t 14 \t 15 \t 16 
 
x5 \t 17 \t 18 \t 19 \t 20

Ich werde mehr als glücklich, wenn jemand mir dabei helfen kann. Danke

+0

Haben Sie Probleme mit der eingebauten 'Kmeans' Funktion, oder bauen Sie eine von Grund auf neu? –

+0

@LeanderMoesinger Vielen Dank für Ihren Kommentar. Eigentlich bin ich in der Lage, die kmeans eingebaute Funktion zu verwenden, aber im Beispiel in Matlab Hilfe konnte ich nicht verstehen, wie ich den Mittelwert, die Mitte, die Größe des Clusters und die Liste der Daten berechnen soll, die jedem Cluster zugewiesen sind. – Bilgin

Antwort

1

Die kmeans() Funktion gibt bereits alles, was Sie wollen, direkt. Es hat die folgende Syntax für 3 Cluster:

[idx,CentreCoordinates,SEE] = kmeans(yourData,3); 

wo

  • idx das Etikett jeder Beobachtung ist (Wert 1 bis 3 in diesem Fall)
  • CentreCoordinates sind die Koordinaten der Clusterzentren (jede Zeile ist ein Zentrum)
  • SEE ist die aufsummierte euklidische Entfernung jeder Beobachtung zu ihrem nächsten Clusterzentrum - der SEE.

Da Sie nicht wirklich die Indizes benötigen, können Sie die erste Ausgabe der Funktion mit ~ (Tilde) ignorieren:

[~,CentreCoordinates,SEE] = kmeans(yourData,3); 
+0

Wenn Sie beim Lesen der .xls-Datei Probleme haben, sollten Sie 'yourData = xlsread ('Pfad/zu/Datei/Dateiname.xls', 'B: E');' tun. So liest es nur Spalten 2: 5, da Spalte 1 nutzlos ist. –

1

Dieser Code ist mit der eingebauten MATLAB-Funktion ‚k-Mittel ". Sie müssen es mit Ihrem eigenen Algorithmus für k-means modifizieren. Es zeigt die Berechnung von Cluster-Centoirds und die Summe der Quadratfehler (auch als Distrotion bezeichnet).

clc; close all; clear all; 
data = readtable('data.txt'); % Importing the data-set 
d1 = table2array(data(:, 2)); % Data in first dimension 
d2 = table2array(data(:, 3)); % Data in second dimension 
d3 = table2array(data(:, 4)); % Data in third dimension 
d4 = table2array(data(:, 5)); % Data in fourth dimension 
X = [d1, d2, d3, d4]; % Combining the data into a matrix 
k = 3; % Number of clusters 
idx = kmeans(X, 3); % Alpplying the k-means using inbuilt funciton 
%% Separating the data in different dimension 
d1_1 = d1(idx == 1); % d1 for the data in cluster 1 
d2_1 = d2(idx == 1); % d2 for the data in cluster 1 
d3_1 = d3(idx == 1); % d3 for the data in cluster 1 
d4_1 = d4(idx == 1); % d4 for the data in cluster 1 
%============================== 
d1_2 = d1(idx == 2); % d1 for the data in cluster 2 
d2_2 = d2(idx == 2); % d2 for the data in cluster 2 
d3_2 = d3(idx == 2); % d3 for the data in cluster 2 
d4_2 = d4(idx == 2); % d4 for the data in cluster 2 
%============================== 
d1_3 = d1(idx == 3); % d1 for the data in cluster 3 
d2_3 = d2(idx == 3); % d2 for the data in cluster 3 
d3_3 = d3(idx == 3); % d3 for the data in cluster 3 
d4_3 = d4(idx == 3); % d4 for the data in cluster 3 
%% Finding the co-ordinates of the cluster centroids 
c1_d1 = mean(d1_1); % d1 value of the centroid for cluster 1 
c1_d2 = mean(d2_1); % d2 value of the centroid for cluster 1 
c1_d3 = mean(d3_1); % d2 value of the centroid for cluster 1 
c1_d4 = mean(d4_1); % d2 value of the centroid for cluster 1 
%==================================== 
c2_d1 = mean(d1_2); % d1 value of the centroid for cluster 2 
c2_d2 = mean(d2_2); % d2 value of the centroid for cluster 2 
c2_d3 = mean(d3_2); % d2 value of the centroid for cluster 2 
c2_d4 = mean(d4_2); % d2 value of the centroid for cluster 2 
%==================================== 
c3_d1 = mean(d1_3); % d1 value of the centroid for cluster 3 
c3_d2 = mean(d2_3); % d2 value of the centroid for cluster 3 
c3_d3 = mean(d3_3); % d2 value of the centroid for cluster 3 
c3_d4 = mean(d4_3); % d2 value of the centroid for cluster 3 
%% Calculating the distortion 
distortion = 0; % Initialization 
for n1 = 1 : length(d1_1)  
    distortion = distortion + (((c1_d1 - d1_1(n1)).^2) + ((c1_d2 - d2_1(n1)).^2) + ... 
                ((c1_d3 - d3_1(n1)).^2) + ((c1_d4 - d4_1(n1)).^2));             
end 
for n2 = 1 : length(d1_2)  
    distortion = distortion + (((c2_d1 - d1_2(n2)).^2) + ((c2_d2 - d2_2(n2)).^2) + ... 
                ((c2_d3 - d3_2(n2)).^2) + ((c2_d4 - d4_2(n2)).^2));             
end 
for n3 = 1 : length(d1_3)  
    distortion = distortion + (((c3_d1 - d1_3(n3)).^2) + ((c3_d2 - d2_3(n3)).^2) + ... 
                ((c3_d3 - d3_3(n3)).^2) + ((c3_d4 - d4_3(n3)).^2));             
end 
fprintf('The unnormalized sum of square error is %f\n', distortion); 
fprintf('The co-ordinate of the cluster 1 is \t d1 = %f, d2 = %f, d3 = %f, d4 = %f\n', c1_d1, c1_d2, c1_d3, c1_d4); 
fprintf('The co-ordinate of the cluster 2 is \t d1 = %f, d2 = %f, d3 = %f, d4 = %f\n', c2_d1, c2_d2, c2_d3, c2_d4); 
fprintf('The co-ordinate of the cluster 3 is \t d1 = %f, d2 = %f, d3 = %f, d4 = %f\n', c3_d1, c3_d2, c3_d3, c3_d4); 
+0

Sie können die Antworten überprüfen, indem Sie den Befehl '[idx, CentreCoordinates, SEE] = kmeans (X, 3);' –

+0

Danke für Ihren Kommentar und Code. Ich habe versucht, Ihren Code in meiner Seite laufen zu lassen, um zu sehen, was passiert, aber ich bekomme Fehler wie: 'Variabler Index überschreitet Tabellenabmessungen'. in Zeile 6: 'd4 = table2array (data (:, 5)); % Daten in der vierten Dimension '. Bitte lassen Sie mich wissen, warum ich diesen Fehler bekomme. danke – Bilgin

+0

In ** meiner Version **, als ich die Daten speicherte und mit 'data = readtable (' data.txt ') importierte;' die erste Spalte enthält die Datennummer, die zweite Spalte enthält die d1-Werte, die dritte Spalte verweist auf d2-Werte, die vierte Spalte enthält d3-Werte und ** die fünfte Spalte enthält die d4-Werte **. Das passiert in 'd4 = table2array (data (:, 5));'. Ich nehme an, dass Sie in Ihrer Version die Datennummer ** nicht ** gespeichert haben und deshalb wahrscheinlich den Fehler bekommen. –

Verwandte Themen