In this post you will discover 8 recipes for non-linear classification in R. Each recipe is ready for you to copy and paste and modify for your own problem.

All recipes in this post use the iris flowers dataset provided with R in the datasets package. The dataset describes the measurements if iris flowers and requires classification of each observation to one of three flower species.

## Mixture Discriminant Analysis

This recipe demonstrates the MDA method on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(mda) data(iris) # fit model fit <- mda(Species~., data=iris) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4]) # summarize accuracy table(predictions, iris$Species) |

Learn more about the **mda** function in the mda package.

## Quadratic Discriminant Analysis

QDA seeks a quadratic relationship between attributes that maximizes the distance between the classes.

This recipe demonstrates the QDA method on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(MASS) data(iris) # fit model fit <- qda(Species~., data=iris) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4])$class # summarize accuracy table(predictions, iris$Species) |

Learn more about the **qda** function in the MASS package.

### Need more Help with R for Machine Learning?

Take my free 14-day email course and discover how to use R on your project (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

## Regularized Discriminant Analysis

This recipe demonstrates the RDA method on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(klaR) data(iris) # fit model fit <- rda(Species~., data=iris, gamma=0.05, lambda=0.01) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4])$class # summarize accuracy table(predictions, iris$Species) |

Learn more about the **rda** function in the klaR package.

## Neural Network

A Neural Network (NN) is a graph of computational units that receive inputs and transfer the result into an output that is passed on. The units are ordered into layers to connect the features of an input vector to the features of an output vector. With training, such as the Back-Propagation algorithm, neural networks can be designed and trained to model the underlying relationship in data.

This recipe demonstrates a Neural Network on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(nnet) data(iris) # fit model fit <- nnet(Species~., data=iris, size=4, decay=0.0001, maxit=500) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4], type="class") # summarize accuracy table(predictions, iris$Species) |

Learn more about the **nnet** function in the nnet package.

## Flexible Discriminant Analysis

This recipe demonstrates the FDA method on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(mda) data(iris) # fit model fit <- fda(Species~., data=iris) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4]) # summarize accuracy table(predictions, iris$Species) |

Learn more about the **fda** function in the mda package.

## Support Vector Machine

Support Vector Machines (SVM) are a method that uses points in a transformed problem space that best separate classes into two groups. Classification for multiple classes is supported by a one-vs-all method. SVM also supports regression by modeling the function with a minimum amount of allowable error.

This recipe demonstrates the SVM method on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(kernlab) data(iris) # fit model fit <- ksvm(Species~., data=iris) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4], type="response") # summarize accuracy table(predictions, iris$Species) |

Learn more about the **ksvm** function in the kernlab package.

## k-Nearest Neighbors

The k-Nearest Neighbor (kNN) method makes predictions by locating similar cases to a given data instance (using a similarity function) and returning the average or majority of the most similar data instances.

This recipe demonstrate the kNN method on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(caret) data(iris) # fit model fit <- knn3(Species~., data=iris, k=5) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4], type="class") # summarize accuracy table(predictions, iris$Species) |

Learn more about the **knn3** function in the caret package.

## Naive Bayes

Naive Bayes uses Bayes Theorem to model the conditional relationship of each attribute to the class variable.

This recipe demonstrates Naive Bayes on the iris dataset.

1 2 3 4 5 6 7 8 9 10 11 |
# load the package library(e1071) data(iris) # fit model fit <- naiveBayes(Species~., data=iris) # summarize the fit summary(fit) # make predictions predictions <- predict(fit, iris[,1:4]) # summarize accuracy table(predictions, iris$Species) |

Learn more about the **naiveBayes** function in the e1071 package.

## Summary

In this post you discovered 8 recipes for non-linear classificaiton in R using the iris flowers dataset.

Each recipe is generic and ready for you to copy and paste and modify for your own problem.

Naive Bayes would generally be considered a linear classifier. The exception being if you are learning a Gaussian Naive Bayes (numerical feature set) and learning separate variances per class for each feature.

Tom Mitchell has a new book chapter that covers this topic pretty well: http://www.cs.cmu.edu/~tom/mlbook/NBayesLogReg.pdf

this example is good , but i know about more than this. as a example Neural Network different model, but it related only text data .

Hi, thanks for the post, I am looking at your QDA model and when I run summary(fit), it looks like this

Length Class Mode

prior 3 -none- numeric

counts 3 -none- numeric

means 12 -none- numeric

scaling 48 -none- numeric

ldet 3 -none- numeric

lev 3 -none- character

N 1 -none- numeric

call 3 -none- call

terms 3 terms call

xlevels 0 -none- list

Can you explain this summary? I also want to look at the variable importance in my model and test on images for later usage

No sorry, perhaps check the documentation for the mode?