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!
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:
If your data contains only categorical variables:
Use k-modes. [R]
If your data contains categorical and metric variables:
Use k-prototypes. [R]
How does k-means work?
- The algorithm randomly initializes k points in your data. These are your cluster centers.
- 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.
- It chooses the centroid of each of the k clusters as the new cluster center.
- 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.