Alosid
Alosid

Reputation: 11

K-means Coding Implementation

I am looking for an implementation of k-means that will out where each row of data belongs too.

I have found other links like Matlab:K-means clustering But they do not help.

So I am looking for something like this. If my data is as follows

1, 2, 4, 5, 6, 7, 8, 9
1, 4, 7, 8, 9, 4, 5, 6

I would like to know that Row 1 Belongs to Cluster A and Row 2 Belongs to Cluster B and so on.

Does anyone know if Matlab can show me that, if so how? If not does anyone have a link to some code that would be able to do that?

Upvotes: 1

Views: 4861

Answers (1)

Sam Roberts
Sam Roberts

Reputation: 24127

Yes, the kmeans command from Statistics Toolbox will do this. Here's an example using the Fisher Iris dataset that is supplied with the toolbox. meas is a 100x4 dataset of four anatomical variables (petal length, petal width, sepal length, sepal width) measured on 150 irises. The output variable, which I've here called clusterIndex, tells you which cluster each row of the dataset falls into, and can be used, for example, as a variable to color points in a plot.

>> load fisheriris
>> k = 3;
>> clusterIndex = kmeans(meas,3);
>> scatter(meas(:,1),meas(:,2),[],clusterIndex,'filled')

enter image description here

Upvotes: 4

Related Questions