To reduce the dimension of a given data set using Quadratic discriminant analysis and build a machine learning model
qda(formula,data) – To compute the Quadratic discriminant analysis
Iris data set
Load the required libraries
Load the data set
Split the data frame for train and test
Compute the Quadratic Discriminant Analysis using the train data
Predict using the test data
Compute the confusion Matrix
Build the naive bayes model using train data
Predict using the test data
Compute the confusion matrix
Compare the confusion matrix obtained from the two model
#load the required libraries
require(MASS)
library(caret)
library(naivebayes)
#Load the data set
data=read.csv(‘/……./iris.csv’)
#To Split 70% of data as training data
smp_size train_ind train1 test1 #Perform linear discriminant analysis
qda_model <-qda(x=train1[,1:4],grouping=train1$species)
#Take the summary
summary(qda_model)
pred1=predict(qda_model,test1[,1:4])
pred=pred1$class
cat(“\nThe confusion matrix for QDA model is \n”)
print(confusionMatrix(pred,as.factor(test1$species)))
#Build the naive bayes model using the original train data
nb1 #Predict using the original test data
pred2=predict(nb1,test1[,1:4])
#Compute the confusion matrix
cat(“\nThe confusion matrix for Naive bayes model is \n”)
print(confusionMatrix(pred2,as.factor(test1$species)))