Unable to recreate k-means clustering example
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.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. 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. k-means clustering, statistics MATLAB Answers — New Questions