# 나이브 베이즈 기법 적용을 위해
e1071 패키지 로딩
> library(e1071)
# 나이브 베이즈 모델 수행
# 훈련데이터를 통해 품종을 맞추는 모델
학습 수행
# 모델 수행시 Laplace smoothing 평할을 수행합니다.
>
naive.result <- naiveBayes(iris_train, iris_train$Species, laplace = 1)
> naive.result
Naive Bayes
Classifier for Discrete Predictors
Call:
naiveBayes.default(x = iris_train, y = iris_train$Species, laplace = 1)
A-priori
probabilities:
iris_train$Species
setosa versicolor virginica
0.3333333 0.3333333
0.3333333
Conditional
probabilities:
Sepal.Length
iris_train$Species [,1] [,2]
setosa 4.982857 0.3807776
versicolor 5.945714 0.4942867
virginica 6.591429 0.6661441
Sepal.Width
iris_train$Species [,1] [,2]
setosa 3.402857 0.3425559
versicolor 2.754286 0.3032596
virginica 2.991429 0.2993831
Petal.Length
iris_train$Species [,1] [,2]
setosa 1.428571 0.1544848
versicolor 4.297143 0.4604893
virginica 5.562857 0.5678057
Petal.Width
iris_train$Species [,1]
[,2]
setosa 0.2371429 0.09727376
versicolor 1.3371429 0.19263760
virginica 2.0371429 0.28705986
Species
iris_train$Species setosa
versicolor
setosa 0.94736842 0.02631579
versicolor 0.02631579
0.94736842
virginica 0.02631579 0.02631579
Species
iris_train$Species virginica
setosa 0.02631579
versicolor 0.02631579
virginica 0.94736842
# 테스트 데이터로 모델 평가
> naive.pred<-predict(naive.result, iris_test, type="class")
> naive.pred
[1] setosa setosa setosa setosa
[5] setosa setosa setosa setosa
[9] setosa setosa setosa setosa
[13] setosa setosa setosa versicolor
[17] versicolor versicolor versicolor versicolor
[21] versicolor versicolor versicolor versicolor
[25] versicolor versicolor versicolor versicolor
[29] versicolor versicolor virginica
virginica
[33] virginica virginica virginica
virginica
[37] virginica virginica virginica
virginica
[41] virginica virginica virginica
virginica
[45] virginica
Levels: setosa versicolor virginica
# 나이브베이즈 분류 결과 도출
> table(naive.pred, iris_test$Species)
naive.pred setosa versicolor
virginica
setosa 15
0 0
versicolor 0 15 0
virginica 0 0 15
|