K表示当肘部曲线是平滑曲线时找到肘部

Anu*_*gar 7 matlab cluster-analysis variance k-means

我试图使用以下代码绘制k的肘部:

load CSDmat %mydata
for k = 2:20
    opts = statset('MaxIter', 500, 'Display', 'off');
    [IDX1,C1,sumd1,D1] = kmeans(CSDmat,k,'Replicates',5,'options',opts,'distance','correlation');% kmeans matlab
    [yy,ii] = min(D1');      %% assign points to nearest center

    distort = 0;
    distort_across = 0;
    clear clusts;
    for nn=1:k
        I = find(ii==nn);       %% indices of points in cluster nn
        J = find(ii~=nn);       %% indices of points not in cluster nn
        clusts{nn} = I;         %% save into clusts cell array
        if (length(I)>0)
            mu(nn,:) = mean(CSDmat(I,:));               %% update mean
            %% Compute within class distortion
            muB = repmat(mu(nn,:),length(I),1);
            distort = distort+sum(sum((CSDmat(I,:)-muB).^2));
            %% Compute across class distortion
            muB = repmat(mu(nn,:),length(J),1);
            distort_across = distort_across + sum(sum((CSDmat(J,:)-muB).^2));
        end
    end
    %% Set distortion as the ratio between the within
    %% class scatter and the across class scatter
    distort = distort/(distort_across+eps);

        bestD(k)=distort;
        bestC=clusts;
end
figure; plot(bestD);
Run Code Online (Sandbox Code Playgroud)

的值bestD(簇方差内/间簇方差)是

[
0.401970132754914
0.193697163350293
0.119427184084282
0.0872681777446508
0.0687948264457301
0.0566215549396577
0.0481117619129058
0.0420491551659459
0.0361696583755145
0.0320384092689509
0.0288948343304147
0.0262373245283877
0.0239462330460614
0.0218350896369853
0.0201506779033703
0.0186757121130685
0.0176258625858971
0.0163239661159014
0.0154933431470081
]
Run Code Online (Sandbox Code Playgroud)

该代码改编自2005年3月加州理工学院的Lihi Zelnik-Manor.

群集方差内与群集方差之间的绘图比率是平滑曲线,膝盖像曲线一样平滑,bestD上面给出的绘图数据.我们如何为这些图找到膝盖?

jes*_*ana 0

我认为最好只使用“类内失真”作为优化参数:

%% Compute within class distortion
muB = repmat(mu(nn,:),length(I),1);
distort = distort+sum(sum((CSDmat(I,:)-muB).^2));
Run Code Online (Sandbox Code Playgroud)

使用此值时无需将该值除以“actor_across”。如果你计算它的“导数”:

unexplained_error = within_class_distortion;
derivative = diff(unexplained_error);
plot(derivative)
Run Code Online (Sandbox Code Playgroud)

导数 (k) 告诉您通过添加新簇,无法解释的误差减少了多少。我建议当此错误的减少量小于您获得的第一次减少量的十倍时,停止添加簇。

for (i=1:length(derivative))
    if (derivative(i) < derivative(1)/10)
         break
    end
end
k_opt = i+1;
Run Code Online (Sandbox Code Playgroud)

事实上,获得最佳簇数的方法取决于应用程序,但我认为您可以使用此建议获得良好的 k 值。