1. 程式人生 > >Get Your Data Ready For Machine Learning in R with Pre

Get Your Data Ready For Machine Learning in R with Pre

Preparing data is required to get the best results from machine learning algorithms.

In this post you will discover how to transform your data in order to best expose its structure to machine learning algorithms in R using the caret package.

You will work through 8 popular and powerful data transforms with recipes that you can study or copy and paste int your current or next machine learning project.

Let’s get started.

Pre-Process Your Machine Learning Dataset in R

Pre-Process Your Machine Learning Dataset in R
Photo by Fraser Cairns, some rights reserved.

Need For Data Pre-Processing

You want to get the best accuracy from machine learning algorithms on your datasets.

Some machine learning algorithms require the data to be in a specific form. Whereas other algorithms can perform better if the data is prepared in a specific way, but not always. Finally, your raw data may not be in the best format to best expose the underlying structure and relationships to the predicted variables.

It is important to prepare your data in such a way that it gives various different machine learning algorithms the best chance on your problem.

You need to pre-process your raw data as part of your machine learning project.

Data Pre-Processing Methods

It is hard to know which data-preprocessing methods to use.

You can use rules of thumb such as:

  • Instance based methods are more effective if the input attributes have the same scale.
  • Regression methods can work better of the input attributes are standardized.

These are heuristics, but not hard and fast laws of machine learning, because sometimes you can get better results if you ignore them.

You should try a range of data transforms with a range of different machine learning algorithms. This will help you discover both good representations for your data and algorithms that are better at exploiting the structure that those representations expose.

It is a good idea to spot check a number of transforms both in isolation as well as combinations of transforms.

In the next section you will discover how you can apply data transforms in order to prepare your data in R using the caret 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.

Data Pre-Processing With Caret in R

The caret package in R provides a number of useful data transforms.

These transforms can be used in two ways.

  • Standalone: Transforms can be modeled from training data and applied to multiple datasets. The model of the transform is prepared using the preProcess() function and applied to a dataset using the predict() function.
  • Training: Transforms can prepared and applied automatically during model evaluation. Transforms applied during training are prepared using the preProcess() and passed to the train() function via the preProcess argument.

A number of data preprocessing examples are presented in this section. They are presented using the standalone method, but you can just as easily use the prepared preprocessed model during model training.

All of the preprocessing examples in this section are for numerical data. Note that the preprocessing functions will skip over non-numeric data without raising an error.

You can learn more about the data transforms provided by the caret package by reading the help for the preProcess function by typing ?preProcess and by reading the Caret Pre-Processing page.

The data transforms presented are more likely to be useful for algorithms such as regression algorithms, instance-based methods (like kNN and LVQ), support vector machines and neural networks. They are less likely to be useful for tree and rule based methods.

Summary of Transform Methods

Below is a quick summary of all of the transform methods supported in the method argument of the preProcess() function in caret.

  • BoxCox“: apply a Box–Cox transform, values must be non-zero and positive.
  • YeoJohnson“: apply a Yeo-Johnson transform, like a BoxCox, but values can be negative.
  • expoTrans“: apply a power transform like BoxCox and YeoJohnson.
  • zv“: remove attributes with a zero variance (all the same value).
  • nzv“: remove attributes with a near zero variance (close to the same value).
  • center“: subtract mean from values.
  • scale“: divide values by standard deviation.
  • range“: normalize values.
  • pca“: transform data to the principal components.
  • ica“: transform data to the independent components.
  • spatialSign“: project data onto a unit circle.

The following sections will demonstrate some of the more popular methods.

1. Scale

The scale transform calculates the standard deviation for an attribute and divides each value by that standard deviation.

1234567891011121314 # load librarieslibrary(caret)# load the datasetdata(iris)# summarize datasummary(iris[,1:4])# calculate the pre-process parameters from the datasetpreprocessParams<-preProcess(iris[,1:4],method=c("scale"))# summarize transform parametersprint(preprocessParams)# transform the dataset using the parameterstransformed<-predict(preprocessParams,iris[,1:4])# summarize the transformed datasetsummary(transformed)

Running the recipe, you will see:

123456789101112131415161718192021   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   Median :5.800   Median :3.000   Median :4.350   Median :1.300   Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199   3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800   Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  Created from 150 samples and 4 variablesPre-processing:  - ignored (0)  - scaled (4)  Sepal.Length    Sepal.Width      Petal.Length     Petal.Width     Min.   :5.193   Min.   : 4.589   Min.   :0.5665   Min.   :0.1312   1st Qu.:6.159   1st Qu.: 6.424   1st Qu.:0.9064   1st Qu.:0.3936   Median :7.004   Median : 6.883   Median :2.4642   Median :1.7055   Mean   :7.057   Mean   : 7.014   Mean   :2.1288   Mean   :1.5734   3rd Qu.:7.729   3rd Qu.: 7.571   3rd Qu.:2.8890   3rd Qu.:2.3615   Max.   :9.540   Max.   :10.095   Max.   :3.9087   Max.   :3.2798

2. Center

The center transform calculates the mean for an attribute and subtracts it from each value.

1234567891011121314 # load librarieslibrary(caret)# load the datasetdata(iris)# summarize datasummary(iris[,1:4])# calculate the pre-process parameters from the datasetpreprocessParams<-preProcess(iris[,1:4],method=c("center"))# summarize transform parametersprint(preprocessParams)# transform the dataset using the parameterstransformed<-predict(preprocessParams,iris[,1:4])# summarize the transformed datasetsummary(transformed)

Running the recipe, you will see:

123456789101112131415161718192021   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   Median :5.800   Median :3.000   Median :4.350   Median :1.300   Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199   3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800   Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  Created from 150 samples and 4 variablesPre-processing:  - centered (4)  - ignored (0) Sepal.Length       Sepal.Width        Petal.Length     Petal.Width     Min.   :-1.54333   Min.   :-1.05733   Min.   :-2.758   Min.   :-1.0993   1st Qu.:-0.74333   1st Qu.:-0.25733   1st Qu.:-2.158   1st Qu.:-0.8993   Median :-0.04333   Median :-0.05733   Median : 0.592   Median : 0.1007   Mean   : 0.00000   Mean   : 0.00000   Mean   : 0.000   Mean   : 0.0000   3rd Qu.: 0.55667   3rd Qu.: 0.24267   3rd Qu.: 1.342   3rd Qu.: 0.6007   Max.   : 2.05667   Max.   : 1.34267   Max.   : 3.142   Max.   : 1.3007

3. Standardize

Combining the scale and center transforms will standardize your data. Attributes will have a mean value of 0 and a standard deviation of 1.

1234567891011121314 # load librarieslibrary(caret)# load the datasetdata(iris)# summarize datasummary(iris[,1:4])# calculate the pre-process parameters from the datasetpreprocessParams<-preProcess(iris[,1:4],method=c("center","scale"))# summarize transform parametersprint(preprocessParams)# transform the dataset using the parameterstransformed<-predict(preprocessParams,iris[,1:4])# summarize the transformed datasetsummary(transformed)

Notice how we can list multiple methods in a list when defining the preProcess procedure in caret. Running the recipe, you will see:

12345678910111213141516171819202122   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   Median :5.800   Median :3.000   Median :4.350   Median :1.300   Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199   3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800   Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  Created from 150 samples and 4 variablesPre-processing:  - centered (4)  - ignored (0)  - scaled (4) Sepal.Length       Sepal.Width       Petal.Length      Petal.Width     Min.   :-1.86378   Min.   :-2.4258   Min.   :-1.5623   Min.   :-1.4422   1st Qu.:-0.89767   1st Qu.:-0.5904   1st Qu.:-1.2225   1st Qu.:-1.1799   Median :-0.05233   Median :-0.1315   Median : 0.3354   Median : 0.1321   Mean   : 0.00000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   3rd Qu.: 0.67225   3rd Qu.: 0.5567   3rd Qu.: 0.7602   3rd Qu.: 0.7880   Max.   : 2.48370   Max.   : 3.0805   Max.   : 1.7799   Max.   : 1.7064

4. Normalize

Data values can be scaled into the range of [0, 1] which is called normalization.

1234567891011121314 # load librarieslibrary(caret)# load the datasetdata(iris)# summarize datasummary(iris[,1:4])# calculate the pre-process parameters from the datasetpreprocessParams<-preProcess(iris[,1:4],method=c("range"))# summarize transform parametersprint(preprocessParams)# transform the dataset using the parameterstransformed<-predict(preprocessParams,iris[,1:4])# summarize the transformed datasetsummary(transformed)

Running the recipe, you will see:

12345678910111213141516171819202122   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   Median :5.800   Median :3.000   Median :4.350   Median :1.300   Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199   3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800   Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  Created from 150 samples and 4 variablesPre-processing:  - ignored (0)  - re-scaling to [0, 1] (4)  Sepal.Length     Sepal.Width      Petal.Length     Petal.Width     Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.00000   1st Qu.:0.2222   1st Qu.:0.3333   1st Qu.:0.1017   1st Qu.:0.08333   Median :0.4167   Median :0.4167   Median :0.5678   Median :0.50000   Mean   :0.4287   Mean   :0.4406   Mean   :0.4675   Mean   :0.45806   3rd Qu.:0.5833   3rd Qu.:0.5417   3rd Qu.:0.6949   3rd Qu.:0.70833   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.00000

5. Box-Cox Transform

When an attribute has a Gaussian-like distribution but is shifted, this is called a skew. The distribution of an attribute can be shifted to reduce the skew and make it more Gaussian. The BoxCox transform can perform this operation (assumes all values are positive).

123456789101112131415 # load librarieslibrary(mlbench)library(caret)# load the datasetdata(PimaIndiansDiabetes)# summarize pedigree and agesummary(PimaIndiansDiabetes[,7:8])# calculate the pre-process parameters from the datasetpreprocessParams<