Unable to recreate k-means clustering example

5 views (last 30 days)
As per title, i'm unable to recreate the following example (k-means clustering - MATLAB kmeans - MathWorks Italia), the k means clustering of Fisher's iris dataset.
This is the code in question
load fisheriris
X = meas(:,3:4);
figure;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)'
rng(1); % For reproducibility
[idx,C] = kmeans(X,3);
x1 = min(X(:,1)):0.01:max(X(:,1));
x2 = min(X(:,2)):0.01:max(X(:,2));
[x1G,x2G] = meshgrid(x1,x2);
XGrid = [x1G(:),x2G(:)]; % Defines a fine grid on the plot
idx2Region = kmeans(XGrid,3,'MaxIter',1,'Start',C);
figure;
gscatter(XGrid(:,1),XGrid(:,2),idx2Region,...
[0,0.75,0.75;0.75,0,0.75;0.75,0.75,0],'..');
hold on;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';
legend('Region 1','Region 2','Region 3','Data','Location','SouthEast');
hold off;
An error occurs at line 16 involving the kmeans function
Error in Kmeans_dimostrativo (line 16)
[idx,C] = kmeans(X,3);
So i started digging in the kmeans function
function [label, mu, energy] = kmeans(X, m)
% Perform kmeans clustering.
% Input:
% X: d x n data matrix
% m: initialization parameter
% Output:
% label: 1 x n sample labels
% mu: d x k center of clusters
% energy: optimization target value
% Written by Mo Chen (sth4nth@gmail.com).
label = init(X, m);
n = numel(label);
idx = 1:n;
last = zeros(1,n);
while any(label ~= last)
[~,~,last(:)] = unique(label); % remove empty clusters
mu = X*normalize(sparse(idx,last,1),1); % compute cluster centers
[val,label] = min(dot(mu,mu,1)'/2-mu'*X,[],1); % assign sample labels
end
energy = dot(X(:),X(:),1)+2*sum(val);
function label = init(X, m)
[d,n] = size(X);
if numel(m) == 1 % random initialization
mu = X(:,randperm(n,m));
[~,label] = min(dot(mu,mu,1)'/2-mu'*X,[],1);
elseif all(size(m) == [1,n]) % init with labels
label = m;
elseif size(m,1) == d % init with seeds (centers)
[~,label] = min(dot(m,m,1)'/2-m'*X,[],1);
end
The following error messages are the ones relative to the kmeans function
Error using randperm
K must be less than or equal to N.
Error in kmeans>init (line 25)
mu = X(:,randperm(n,m));
Error in kmeans (line 11)
label = init(X, m);
I honestly don't know the reason for the errors, i took the script directly from the website.

Accepted Answer

Star Strider
Star Strider on 3 Aug 2024
Edited: Star Strider on 3 Aug 2024
It works correctly in R2024a. (The online documentation reflects the latest update to the current release, and may not run correctly in earlier releases.)
There are apparently version differences in kmeans between R2023b and R2024a, although I don’t see any mention of updates or bug fixes regarding it in rhe Release Notes for R2024a, or bug fixes in R2023b.
Copy the relevant code from your R2023b documentation, be certain that you have installed all the lates updaates to R2023b, or upgrade to R2024a.
load fisheriris
X = meas(:,3:4);
figure;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)'
rng(1); % For reproducibility
[idx,C] = kmeans(X,3);
x1 = min(X(:,1)):0.01:max(X(:,1));
x2 = min(X(:,2)):0.01:max(X(:,2));
[x1G,x2G] = meshgrid(x1,x2);
XGrid = [x1G(:),x2G(:)]; % Defines a fine grid on the plot
idx2Region = kmeans(XGrid,3,'MaxIter',1,'Start',C);
Warning: Failed to converge in 1 iterations.
figure;
gscatter(XGrid(:,1),XGrid(:,2),idx2Region,...
[0,0.75,0.75;0.75,0,0.75;0.75,0.75,0],'..');
hold on;
plot(X(:,1),X(:,2),'k*','MarkerSize',5);
title 'Fisher''s Iris Data';
xlabel 'Petal Lengths (cm)';
ylabel 'Petal Widths (cm)';
legend('Region 1','Region 2','Region 3','Data','Location','SouthEast');
hold off;
EDIT — Added clarification.
.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!