Skip to content

Instantly share code, notes, and snippets.

@yunho0130
Created October 26, 2016 03:03
Show Gist options
  • Save yunho0130/c3b82868d61bf81e71bb1bbbe97b1199 to your computer and use it in GitHub Desktop.
Save yunho0130/c3b82868d61bf81e71bb1bbbe97b1199 to your computer and use it in GitHub Desktop.
k-mean을 활용한 클러스터링(Clustering)을 R을 활용한 예제 입니다.
# Reference Address
# http://datascienceplus.com/k-means-clustering-in-r/
# iris 데이터를 활용
library(datasets)
head(iris)
# ggplot2로 시각화
library(ggplot2)
ggplot(iris, aes(Petal.Length, Petal.Width, color = Species)) + geom_point()
# k-mean을 통한 분류
set.seed(20)
irisCluster <- kmeans(iris[, 3:4], 3, nstart = 20)
irisCluster
# 각 종별로 클러스터 비교
table(irisCluster$cluster, iris$Species)
# 해당 결과 시각화
irisCluster$cluster <- as.factor(irisCluster$cluster)
ggplot(iris, aes(Petal.Length, Petal.Width, color = irisCluster$cluster)) + geom_point()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment