1. 程式人生 > >Spot Check Machine Learning Algorithms in R (algorithms to try on your next project)

Spot Check Machine Learning Algorithms in R (algorithms to try on your next project)

Spot checking machine learning algorithms is how you find the best algorithm for your dataset.

But what algorithms should you spot check?

In this post you discover the 8 machine learning algorithms you should spot check on your data.

You also get recipes of each algorithm that you can copy and paste into your current or next machine learning project in R.

Let’s get started.

Spot Check Machine Learning Algorithms in R

Spot Check Machine Learning Algorithms in R
Photo by Nuclear Regulatory Commission, some rights reserved.

Best Algorithm For Your Dataset

You cannot know which algorithm will work best on your dataset before hand.

You must use trial and error to discover a short list of algorithms that do well on your problem that you can then double down on and tune further. I call this process spot checking.

The question is not:

What algorithm should I use on my dataset?

Instead it is:

What algorithms should I spot check on my dataset?

Which Algorithms To Spot Check

You can guess at what algorithms might do well on your dataset, and this can be a good starting point.

I recommend trying a mixture of algorithms and see what is good at picking out the structure in your data.

  • Try a mixture of algorithm representations (e.g. instances and trees).
  • Try a mixture of learning algorithms (e.g. different algorithms for learning the same type of representation).
  • Try a mixture of modeling types (e.g. linear and non-linear functions or parametric and non-parametric).

Let’s get specific. In the next section, we will look algorithms that you can use to spot check on your next machine learning project in R.

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.

Algorithms To Spot Check in R

There are hundreds of machine learning algorithms available in R.

I would recommend exploring many of them, especially, if making accurate predictions on your dataset is important and you have the time.

Often you don’t have the time, so you need to know the few algorithms that you absolutely must test on your problem.

In this section you will discover the linear and nonlinear algorithms you should spot check on your problem in R. This excludes ensemble algorithms such as as boosting and bagging, that can come later once you have a baseline.

Each algorithm will be presented from two perspectives:

  1. The package and function used to train and make predictions for the algorithm.
  2. The caret wrapper for the algorithm.

You need to know which package and function to use for a given algorithm. This is needed when:

  • You are researching the algorithm parameters and how to get the most from the algorithm.
  • You have a discovered the best algorithm to use and need to prepare a final model.

You need to know how to use each algorithm with caret, so that you can efficiently evaluate the accuracy of the algorithm on unseen data using the preprocessing, algorithm evaluation and tuning capabilities of caret.

Two standard datasets are used to demonstrate the algorithms:

  • Boston Housing dataset for regression (BostonHousing from the mlbench library).
  • Pima Indians Diabetes dataset for classification (PimaIndiansDiabetes from the mlbench library).

Algorithms are presented in two groups:

  • Linear Algorithms that are simpler methods that have a strong bias but are fast to train.
  • Nonlinear Algorithms that are more complex methods that have a large variance but are often more accurate.

Each recipe presented in this section is complete and will produce a result, so that you can copy and paste it into your current or next machine learning project.

Let’s get started.

Linear Algorithms

These are methods that make large assumptions about the form of the function being modeled. As such they are have a high bias but are often fast to train.

The final models are also often easy (or easier) to interpret, making them desirable as final models. If the results are suitably accurate, you may not need to move onto non-linear methods if a linear algorithm.

1. Linear Regression

The lm() function is in the stats library and creates a linear regression model using ordinary least squares.

12345678910111213 # load the librarylibrary(mlbench)# load datadata(BostonHousing)# fit modelfit<-lm(medv~.,BostonHousing)# summarize the fitprint(fit)# make predictionspredictions<-predict(fit,BostonHousing)# summarize accuracymse<-mean((BostonHousing$medv-predictions)^2)print(mse)

The lm implementation can be used in caret as follows:

1234567891011 # load librarieslibrary(caret)library(mlbench)# load datasetdata(BostonHousing)# trainset.seed(7)control<-trainControl(method="cv",number=5)fit.lm<-train(medv~.,data=BostonHousing,method="lm",metric="RMSE",preProc=c("center","scale"),trControl=control)# summarize fitprint(fit.lm)

2. Logistic Regression

The glm function is in the stats library and creates a generalized linear model. It can be configured to perform a logistic regression suitable for binary classification problems.

12345678910111213 # load the librarylibrary(mlbench)# Load the datasetdata(PimaIndiansDiabetes)# fit modelfit<-glm(diabetes~.,data=PimaIndiansDiabetes,family=binomial(link='logit'))# summarize the fitprint(fit)# make predictionsprobabilities<-predict(fit,PimaIndiansDiabetes[,1:8],type='response')predictions<-ifelse(probabilities>0.5,'pos','neg')# summarize accuracytable(predictions,PimaIndiansDiabetes$diabetes)

The glm algorithm can be used in caret as follows:

1234567891011 # load librarieslibrary(caret)library(mlbench)# Load the datasetdata(PimaIndiansDiabetes)# trainset.seed(7)control<-trainControl(method="cv",number=5)fit.glm<-train(diabetes~.,data=PimaIndiansDiabetes,method="glm",metric="Accuracy",preProc=c("center","scale"),trControl=control)# summarize fitprint(fit.glm)

3. Linear Discriminant Analysis

The lda function is in the MASS library and creates a linear model of a classification problem.

12345678910111213 # load the librarieslibrary(MASS)library(mlbench)# Load the datasetdata(PimaIndiansDiabetes)# fit modelfit<-lda(diabetes~.,data=PimaIndiansDiabetes)# summarize the fitprint(fit)# make predictionspredictions<-predict(fit,PimaIndiansDiabetes[,1:8])$class# summarize accuracytable(predictions,PimaIndiansDiabetes$diabetes)

The lda algorithm can be used in caret as follows:

1234567891011 # load librarieslibrary(caret)library(mlbench)# Load the datasetdata(PimaIndiansDiabetes)# trainset.seed(7)control<-trainControl(method="cv",number=5)fit.lda<-train(diabetes~.,data=PimaIndiansDiabetes,method="lda",metric="Accuracy",preProc=c("center","scale"),trControl=control)# summarize fitprint(fit.lda)

4. Regularized Regression

The glmnet function is in the glmnet library and can be used for classification or regression.

Classification Example:

123456789101112131415 # load the librarylibrary(glmnet)library(mlbench)# load datadata(PimaIndiansDiabetes)x<-as.matrix(PimaIndiansDiabetes[,1:8])y<-as.matrix(PimaIndiansDiabetes[,9])# fit modelfit<-glmnet(x,y,family="binomial",alpha=0.5,lambda=0.001)# summarize the fitprint(fit)# make predictionspredictions<-predict(fit,x,type="class")# summarize accuracytable(predictions,PimaIndiansDiabetes$diabetes)

Regression Example:

1234567891011121314151617 # load the librarieslibrary(glmnet)library(mlbench)# load datadata(BostonHousing)BostonHousing$chas<-as.numeric(as.character(BostonHousing$chas))x<-as.matrix(BostonHousing[,1:13])y<-as.matrix(BostonHousing[,14])# fit modelfit<-glmnet(x,y,family="gaussian",alpha=0.5,lambda=0.001)# summarize the fitprint(fit)# make predictionspredictions<-predict(fit,x,type="link")# summarize accuracymse<-mean((y-predictions)^2)print(mse)

It can also be configured to perform three important types of regularization: lasso, ridge and elastic net by configuring the alpha parameter to 1, 0 or in [0,1] respectively.

The glmnet implementation can be used in caret for classification as follows:

123456789101112 # load librarieslibrary(caret)library(mlbench)library(glmnet)# Load the datasetdata(PimaIndiansDiabetes)# trainset.seed(7)control<-trainControl(method="cv",number=5)fit.glmnet<-train(diabetes~.,data=PimaIndiansDiabetes,method="glmnet",metric="Accuracy",preProc=c("center","scale"),trControl=control)# summarize fitprint(fit.glmnet)

The glmnet