R

R 기초 30 - Naive Bayes 분류 모델

코딩탕탕 2022. 10. 27. 15:59

 

 

# Naive Bayes 분류 모델
# 조건부 확률 사용 P(A|B)

install.packages("e1071")
library("e1071")
dim(iris)
set.seed(123)
idx <- sample(1:nrow(iris), nrow(iris) * 0.7)
train <- iris[idx, ]
test <- iris[-idx, ]
dim(train)
dim(test)

model <- naiveBayes(Species ~ ., data=train, replace=TRUE)
model

pred <- predict(model, test, type='class')
t <- table(pred, test$Species)
t

sum(diag(t)) / nrow(test)
#데이터만 바꿔주면 미리 만들어논 코드를 재사용할 수 있다