Created
July 10, 2016 08:53
-
-
Save somerealnumbers/4f5433ae088f48e2a4861204f4a7cb5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ################################################# | |
| # un-parameterised | |
| require(dplyr) | |
| df = iris[,sapply(X = iris | |
| , function(X) is.numeric(X))] | |
| df.cluster = df %>% kmeans(x = . | |
| , centers = 4) | |
| df %>% plot(main = '4 Cluster Solution' | |
| , col = df.cluster$cluster) | |
| ################################################# | |
| # partially parameterised | |
| require(dplyr) | |
| nClust = 4 | |
| df = iris[,sapply(X = iris | |
| , function(X) is.numeric(X))] | |
| df.cluster = df %>% kmeans(x = . | |
| , centers = nClust) | |
| df %>% plot(main = paste(nClust, 'Cluster Solution') | |
| , col = df.cluster$cluster) | |
| ################################################# | |
| # parameterised | |
| find_clusters = function(inputData, nClust){ | |
| df = inputData[,sapply(X = inputData | |
| , function(X) is.numeric(X))] | |
| df.cluster = df %>% kmeans(x = . | |
| , centers = nClust) | |
| df %>% plot(main = paste(nClust, 'Cluster Solution') | |
| , col = df.cluster$cluster) | |
| } | |
| find_clusters(inputData = iris, nClust = 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment