library(e1071)

data(iris)

## Split in train + test set
indexes <- sample(1:nrow(iris),as.integer(0.7*nrow(iris)))

trainData <- iris[indexes,]
testData <- iris[-indexes,]

model <- naiveBayes(x=trainData[,-5], y=trainData[, 5])
pred <- predict(model, testData)

confusionmatrix = as.matrix(table(Actual = testData[, 5], Predicted = pred))
accuracy = sum(diag(confusionmatrix))/length(pred)

print('Confusion Matrix')
print(confusionmatrix)
print('Accuracy')
print(accuracy)

plant1 <- data.frame(Sepal.Length=5.2, Sepal.Width=3.5, Petal.Length=1.4, Petal.Width=0.2, Species='Unk')
plant2 <- data.frame(Sepal.Length=5.7, Sepal.Width=1.4, Petal.Length=3.6, Petal.Width=1.3, Species='Unk')
plant3 <- data.frame(Sepal.Length=5.8, Sepal.Width=1.3, Petal.Length=5.1, Petal.Width=1.8, Species='Unk')

pred <- predict(model, plant1)
print(pred)

pred <- predict(model, plant2)
print(pred)

pred <- predict(model, plant3)
print(pred)



