"K-means 군집분석" #3. iris 예제 (1) 데이터 살펴보기
R 내장데이터셋인 iris 데이터를 이용할 것입니다. head 함수와 str함수를 이용하여 데이터를 살펴봅시다.
> head(iris,5)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
> str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
변수의 개수는 5개입니다.
Sepal.Length : 꽃받침 길이 / 연속형
Sepal.Width : 꽃받침 너비 / 연속형
Petal.Length : 꽃잎 길이 / 연속형
Petal.Width : 꽃잎 너비 / 연속형
Species : 종 / 범주형
예제이므로 시각화가 가능하도록, 두개의 연속형 변수를 x와 y로 선택하고 k mean clustering을 해보겠습니다.
꽃잎 길이를 x로 너비를 y로 선정하였습니다. 그래프를 그려보면 아래와 같습니다.
plot(iris$Petal.Length,iris$Petal.width,ann=FALSE)
title(xlab="petal length",ylab="petal width")
box("outer", col="gray")
꽃의 종(species)들을 색으로 구분하면 아래와 같습니다 .
plot(iris$Petal.Length,iris$Petal.width,ann=FALSE,
col=c("red","blue","green")[unclass(iris$Species)])
title(xlab="petal length",ylab="petal width")
box("outer", col="gray")
댓글