Info

This question is closed. Reopen it to edit or answer.

How do I classify this data?

1 view (last 30 days)
Jon Camilleri
Jon Camilleri on 24 Nov 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
I am trying to come up with a useful machine learning algorithm, and, having reviewed a lot of code that seems useless to myself, I thought I would do some initial validation criteria based on simple averages and minima/maxima.
The next question is how to classify the information in a function that seems to work well for my data, since I am still in the teething phases of my learning curve.
function ExampleKMeans
n = 100;
r1 = repmat([1,1],n,1) + 2.*randn(n,2);
r2 = repmat([1,10],n,1) + 2.*randn(n,2);
r3 = repmat([10,10],n,1) + 2.*randn(n,2);
%r4 = repmat([10,1],n,1) + 2.*randn(n,2);
data = [r1;r2;r3];
mn = min(data);
mx = max(data);
figure;
subplot(3,4,1);
plot(data(:,1),data(:,2),'r+');
set(gca,'xlim',[mn(1) mx(1)],'ylim',[mn(2),mx(2)]);
title('Original Data');
for k = 2:10
idx = kmeans(data,k);
validity = DunnIndex(data,idx,k);
subplot(3,4,k);
gscatter(data(:,1),data(:,2),idx);
set(gca,'xlim',[mn(1) mx(1)],'ylim',[mn(2),mx(2)]);
title(['Dunns Validity Measure: ',num2str(validity)]);
end
function validity = DunnIndex(data,idx,K)
N = size(data,1);
totalsum = 0;
for i = 1:K
x = data(idx == i,:);
zi = mean(x);
for j = 1:size(x,1)
totalsum = totalsum + sum((x(j,:)-zi).^2);
end
end
intra = totalsum/N;
distmatrix = zeros(K,K);
for i = 1:K-1
xi = data(idx == i,:);
zi = mean(xi);
for j = i+1:K
xj = data(idx == j,:);
zj = mean(xj);
distmatrix(i,j) = sum((zi-zj).^2);
end
end
inter = min(distmatrix(find(distmatrix)));
validity = intra/inter;
function scaledData = minmaxscaling(data)
mn = min(data);
mx = max(data);
for i = 1:size(data,1)
scaledData(i,:) = (data(i,:) - mn)./(mx-mn);
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!