How does Quadratic Discriminant Analysis work in R

How does Quadratic Discriminant Analysis work in R

Recipe Objective

How does Quadratic Discriminant Analysis work?

A quadratic discriminant analysis is a technique used to classify a target variable from a class of independent variables into two or more classes, i.e., multi class classification. This technique is a non-linear equivalent to linear discriminant analysis. The following recipe explains how Quadratic Discriminant Analysis works.

Step 1 - Install the necessary libraries

install.packages('ggplot2')
install.packages('caret') # for general data preparation and model fitting
install.packages("e1071")
library(ggplot2)
library(caret)
library(MASS)
library(e1071)

Step 2 - Read a dataset and explore the data

data <- iris # reads the dataset
head(data) # head() returns the top 6 rows of the dataframe
summary(data) # returns the statistical summary of the data columns
dim(data)

This dataset has 150 rows and 5 columns. The quadratic discriminant analysis model is used to analysis the dataset into three categories called setosa, versicolor, virginica based on different independent variables.

Step 3 - Train and Test data

# createDataPartition() function from the caret package to split the original dataset into a training and testing set and split data into training (80%) and testing set (20%)
parts = createDataPartition(data$Species, p = 0.8, list = F)
train = data[parts, ]
test = data[-parts, ]

Step 4 - Create a qda model

# train a model using our training data
model_qda <- qda(Species~., data=train)
model_qda

The above output interprets : Prior probabilities represent the proportions of each species in the training dataset. Group means are used to display the mean value for each predictor variable for each species.

Step 5 - Make predictions on the test dataset

#use model to make predictions on test data
pred_test <- predict(model_qda, test)
# Returns the prediction values of test data along with the confusion matrix
pred_test
confusionMatrix(pred_test$class,test$Species)

The prediction : setosa : correctly classified all 10. versicolor : correctly classified 9 and wrongly classified 1 as virginica. virginica : correctly classified all 10. The model gives a good accuracy of 96.67%

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.