**KMeans algorithm**including key aspects and formula/

**R command**when you are working on

**clustering tasks**. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

- Key aspects of applying KMeans algorithm
- KMeans Algorithm – R Command

###### Key aspects of applying KMeans Algorithm

Key aspects of applying KMeans algorithm are following:

**Selecting a right combination of features set**: On the data set on which you may observe some of the following:- There are one or more features having non-numeric or character data sets. As KMeans requires a data frame containing only numeric data, the challenge is to define a numeric representation of character related features or exclude character-related features from analysis.
- There are one or more features having missing data. There are different techniques one use to take care of missing data. In case of numeric feature set, one may use technique such as finding means (sometimes using aggregate function) and assigning the missing values with mean. In case, the feature has nominal data, one could use dummy coding technique to come up with new set of variables.

Thus, it may so happen that you may use only a select set fo features from a given data set and do the analysis on those features set.

- Other key aspect which is common across different algorithm is to
**apply normalization to data set**. One could use either min-max normalization or z-score standardization technique to achieve data normalization. Following is example representing scaling of data in the data frame, someDF. Pay attention to scale() function.someDF_z <- as.data.frame(lapply(someDF, scale))

**Selecting an optimum number of clusters**: Selecting an optimum of clusters (represents K) is the key. There are different perspectives around this and I shall talk about it in another blog.

###### KMeans Algorithm – R Commands

There is a kmeans() function in stats package in R. Note that stats package is included by default in R installation. If it is not there, you may want to install this package.

Following is the formulae:

# KMeans function applied on some data frame, someDF, where only a # set of features having numeric values were selected; Notice 4:9 kmeansDF <- kmeans(someDF[4:9], 5)

- Bayesian thinking & Real-life Examples - February 2, 2023
- True Error vs Sample Error: Difference - January 30, 2023
- Confidence Intervals Formula, Examples - January 29, 2023

## Leave a Reply