I have a data set which has 2488 samples and each sample has 13 features.Now I want to perform cluster on this data set in R but I found k-means method usually for two dimensions data.So can any one help me? Many thanks!


Short answer

You can use any number of dimensions for k-means. Therefore, you can use the standard k-means library for R as long as:

  • your data contains only metric variables and
  • every column is scaled to the same range.

See this related question.

If your data is not scaled:

Scale it.

If your data contains only categorical variables:

Use k-modes. [R]

If your data contains categorical and metric variables:

Use k-prototypes. [R]

Long answer

How does k-means work?

  1. The algorithm randomly initializes k points in your data. These are your cluster centers.
  2. It assigns every data point to the nearest cluster center. Usually, this is done by calculating the Euclidian distance of the data point to each of the k cluster centers. Now you have k clusters.
  3. It chooses the centroid of each of the k clusters as the new cluster center.
  4. The steps 2 and 3 are repeated until the solution converges or the maximum number of steps is reached.

What this means

The Euclidian distance can be calculated in n-dimensional space. Therefore, you can use any number of dimensions you want.

However, if your data is not scaled, the distance calculations will be different for every feature. Therefore, you need to scale it.

Since Euclidian distance won't make a lot of sense with categorical or with mixed data, you have to use an algorithm that uses Hamming distance instead for these use cases. k-modes uses only Hamming distance. k-prototypes uses Hamming distance for categorical and Euclidian distance for metric variables.

| improve this answer | |
  • $\begingroup$ Thank you for your answer! I'm still wondering how can I visualize the result since the photo can only demonstration 2-d view. what the X and Y axis will be? $\endgroup$ – guohui li Aug 7 '19 at 2:38
  • 1
    $\begingroup$ @guohuili Regarding your question: Visualizing multidimensional data is a very interesting and broad topic. I recommend putting this in a separate question as you will get more answers with higher quality then and it will also help others in the future that have a similar question. $\endgroup$ – georg-un Aug 7 '19 at 9:16

K-means is in no way restricted to 2d data.

Hat is just the lecture toy examples because our screens tend to be 2 dimensional, so that is easier to visualize.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.