# How to determine x and y in 2 dimensional K-means clustering?

Recently, we were taught K-means clustering. I understood the basic idea of the algorithm and successfully implemented it for data with a single dimensional. Now we are told to implement it for two dimensional data. As far as I understood x and y can be two attributes of a dataset but our professor said otherwise. She said that the we have to determine x and y of an attribute in the data set to cluster the data. She used a simple 2D matrix as an example. This has got me confused. How can one determine x and y of an atttribute? Row number and column number seems silly to me to use it for distance calculation.

So, my question is how does one determine x and y for 2-D k means clustering?

As per this question the two attributes (weight and height) are used as x and y. Is this correct?

I see no problem with the example of clustering on two numerical attributes like height and weight like in the example.

The only thing I can think of is somewhere lost in translation, your professor was trying to explain the concept of reducing many dimensions (attributes) to two and then clustering on those derived dimensions. That is a common technique for trying to visually spot clusters in a complex data set.

K-Means is an algorithm primarily used to find clusters in higher dimensional datasets. I have never come across K-Means being used for single dimensional datasets.

So, a 2D K-Means (or even higher dimensional) works this way:

1. Decide the number of clusters you want (k value).
2. Initialize k number of random 2D (or higher dimensional) points (centroids).
3. Each point in your dataset gets assigned the centroid it lies closest to.
4. The centroid positions then gets updated to the average of all points that were assigned the said centroid. Repeat from step 3 till stopping condition reached.

And for your second question, yes, that is a correct method, and the answer to that question is correctly explained.

• I saw once K-means used for 1D problems: finding the more K more relevant "colors" in a grey scale image. Cluster's centers were then used to define the only remaining grey values (image compression). – Manu H Jun 18 '19 at 7:53

"Distance" in k-means is the sum of squares over all attributes. It does not matter how many attributes you have.

d(a, b) := sum_i (a_i - b_i)²


Where i iterates over your attributes, a is a data vector and b is a centroid vector.

Don't assume there is one, or two attributes. It's simply a parameter of the data set, how many variables it has. Could be 42 variables.