Finalize Your Machine Learning Model

Once you have an accurate model on your test harness you are nearly, done. But not yet.

There are still a number of tasks to do to finalize your model. The whole idea of creating an accurate model for your dataset was to make predictions on unseen data.

There are three tasks you may be concerned with:

Making new predictions on unseen data.

Creating a standalone model using all training data.

Saving your model to file for later loading and making predictions on new data.

Once you have finalized your model you are ready to make use of it. You could use the R model directly. You could also discover the key internal representation found by the learning algorithm (like the coefficients in a linear model) and use them in a new implementation of the prediction algorithm on another platform.

In the next section, you will look at how you can finalize your machine learning model 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).

Finalize Predictive Model in R

Caret is an excellent tool that you can use to find good or even best machine learning algorithms and parameters for machine learning algorithms.

But what do you do after you have discovered a model that is accurate enough to use?

Once you have found a good model in R, you have three main concerns:

Making new predictions using your tuned caret model.

Creating a standalone model using the entire training dataset.

Saving/Loading a standalone model to file.

This section will step you through how to achieve each of these tasks in R.

1. Make Predictions On New Data

You can make new predictions using a model you have tuned using caret using the predict.train() function.

In the recipe below, the dataset is split into a validation dataset and a training dataset. The validation dataset could just as easily be a new dataset stored in a separate file and loaded as a data frame.

A good model of the data is found using LDA. We can see that caret provides access to the best model from a training run in the finalModel variable.

We can use that model to make predictions by calling predict using the fit from train which will automatically use the final model. We must specify the data one which to make predictions via the newdata argument.

Running the example, we can see that the estimated accuracy on the training dataset was 76.91%. Using the finalModel in the fit, we can see that the accuracy on the hold out validation dataset was 77.78%, very similar to our estimate.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Resampling results

Accuracy Kappa Accuracy SD Kappa SD

0.7691169 0.45993 0.06210884 0.1537133

...

Confusion Matrix and Statistics

Reference

Prediction neg pos

neg 85 19

pos 15 34

Accuracy : 0.7778

95% CI : (0.7036, 0.8409)

No Information Rate : 0.6536

P-Value [Acc > NIR] : 0.000586

Kappa : 0.5004

Mcnemar's Test P-Value : 0.606905

Sensitivity : 0.8500

Specificity : 0.6415

Pos Pred Value : 0.8173

Neg Pred Value : 0.6939

Prevalence : 0.6536

Detection Rate : 0.5556

Detection Prevalence : 0.6797

Balanced Accuracy : 0.7458

'Positive' Class : neg

2. Create A Standalone Model

In this example, we have tuned a random forest with 3 different values for mtry and ntree set to 2000. By printing the fit and the finalModel, we can see that the most accurate value for mtry was 2.

Now that we know a good algorithm (random forest) and the good configuration (mtry=2, ntree=2000) we can create the final model directly using all of the training data. We can lookup the “rf” random forest implementation used by caret in the Caret List of Models and note that it is using the randomForest package and in turn the randomForest() function.

The example creates a new model directly and uses it to make predictions on the new data, this case simulated as the verification dataset.

We can see that the estimated accuracy of the optimal configuration was 85.07%. We can see that the accuracy of the final standalone model trained on all of the training dataset and predicting for the validation dataset was 82.93%.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

Random Forest

167 samples

60 predictor

2 classes: 'M', 'R'

No pre-processing

Resampling: Cross-Validated (10 fold, repeated 3 times)

Summary of sample sizes: 151, 150, 150, 150, 151, 150, ...

Resampling results across tuning parameters:

mtry Accuracy Kappa Accuracy SD Kappa SD

2 0.8507353 0.6968343 0.07745360 0.1579125

31 0.8064951 0.6085348 0.09373438 0.1904946

60 0.7927696 0.5813335 0.08768147 0.1780100

Accuracy was used to select the optimal model using the largest value.

The final value used for the model was mtry = 2.

...

Call:

randomForest(x = x, y = y, ntree = 2000, mtry = param$mtry)

Type of random forest: classification

Number of trees: 2000

No. of variables tried at each split: 2

OOB estimate of error rate: 14.37%

Confusion matrix:

M R class.error

M 83 6 0.06741573

R 18 60 0.23076923

...

Confusion Matrix and Statistics

Reference

Prediction M R

M 20 5

R 2 14

Accuracy : 0.8293

95% CI : (0.6794, 0.9285)

No Information Rate : 0.5366

P-Value [Acc > NIR] : 8.511e-05

Kappa : 0.653

Mcnemar's Test P-Value : 0.4497

Sensitivity : 0.9091

Specificity : 0.7368

Pos Pred Value : 0.8000

Neg Pred Value : 0.8750

Prevalence : 0.5366

Detection Rate : 0.4878

Detection Prevalence : 0.6098

Balanced Accuracy : 0.8230

'Positive' Class : M

Some simpler models, like linear models can output their coefficients. This is useful, because from these, you can implement the simple prediction procedure in your language of choice and use the coefficients to get the same accuracy. This gets more difficult as the complexity of the representation increases.

3. Save and Load Your Model

You can save your best models to a file so that you can load them up later and make predictions.

In this example we split the Sonar dataset into a training dataset and a validation dataset. We take our validation dataset as new data to test our final model. We train the final model using the training dataset and our optimal parameters, then save it to a file called final_model.rds in the local working directory.

The model is serialized. It can be loaded at a later time by calling readRDS() and assigning the object that is loaded (in this case a random forest fit) to a variable name. The loaded random forest is then used to make predictions on new data, in this case the validation dataset.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

# load libraries

library(caret)

library(mlbench)

library(randomForest)

library(doMC)

registerDoMC(cores=8)

# load dataset

data(Sonar)

set.seed(7)

# create 80%/20% for training and validation datasets

validation_index<-createDataPartition(Sonar$Class,p=0.80,list=FALSE)

validation<-Sonar[-validation_index,]

training<-Sonar[validation_index,]

# create final standalone model using all training data

set.seed(7)

final_model<-randomForest(Class~.,training,mtry=2,ntree=2000)

# save the model to disk

saveRDS(final_model,"./final_model.rds")

# later...

# load the model

super_model<-readRDS("./final_model.rds")

print(super_model)

# make a predictions on "new data" using the final model

final_predictions<-predict(super_model,validation[,1:60])

confusionMatrix(final_predictions,validation$Class)

We can see that the accuracy on the validation dataset was 82.93%.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Confusion Matrix and Statistics

Reference

Prediction M R

M 20 5

R 2 14

Accuracy : 0.8293

95% CI : (0.6794, 0.9285)

No Information Rate : 0.5366

P-Value [Acc > NIR] : 8.511e-05

Kappa : 0.653

Mcnemar's Test P-Value : 0.4497

Sensitivity : 0.9091

Specificity : 0.7368

Pos Pred Value : 0.8000

Neg Pred Value : 0.8750

Prevalence : 0.5366

Detection Rate : 0.4878

Detection Prevalence : 0.6098

Balanced Accuracy : 0.8230

'Positive' Class : M

Summary

In this post you discovered three recipes for working with final predictive models:

How to make predictions using the best model from caret tuning.

How to create a standalone model using the parameters found during caret tuning.

How to save and later load a standalone model and use it to make predictions.

You can work through these recipes to understand them better. You can also use them as a template and copy-and-paste them into your current or next machine learning project.

Next Step

Did you try out these recipes?

Start your R interactive environment.

Type or copy-paste the recipes above and try them out.

Use the built-in help in R to learn more about the functions used.

Do you have a question. Ask it in the comments and I will do my best to answer it.

36 Responses to Save And Finalize Your Machine Learning Model in R

Hi. I’ve been working through the examples, and some models of my own, and i have a question on preprocessing my data.

When training my models, I used the preProcess argument to center and scale the data.

When I have a new data set to run through my model after training, does the model know to preprocess that data as well? Or do i have to manually scale and center the new data set before applying the model to it? What about after saving and reloading the final model?

I’m going a bit too far maybe, but I’ve been wondering recently if it is possible to save a trained model in some kind of standard format so that it can be (1) sent over a network and (2) parsed even by a different language (I dunno, Java for example).

Are you aware of anything similar, for R or (possibly) other machine learning / statistics suites?

I have a short question concerning the “standalone model”. Why is it necessary to use it after using the caret one? I though bought would be equivalent (i.e. caret using the randomForest one in this example of “rf”-method) and am now a bit confused.

Excellent work and quite helpful for us who is in learning mode. Just one question here. Suppose, I’ve 100 data-points and I divide into train & test (80 & 20). I fit the model and see accuracy as 75%. My question is how do I implement this model so that when 101st and 102nd data arrives, this model runs and provides me some classification?

The “finalize machine learning models” article above is helpful, and I can’t wait to try it out. However, I notice that it’s making one prediction. Is here a way to get multiple predictions at once, based on criteria, and give them as a list? Like this:

Dear Sir,
I am doing dissertation in load balancing in distributed computing system in which I want to predict the future incoming jobs on the basis of past load information given in the real time dataset.So how can I apply this technique in my work?

I am using caret, and I need to print the function of a ramdom forest best model, I am talking about all the trees, that are 100.
I know with model$finalModel I can print the function of neuronal network or tree with rpart, but this command don’t print all the trees of random forest or the function of boosted trees
Do you Know how I can do it

I have a built a highly accurate random forest model for a student enrollment prediction project ,let’s say, for this year. Now I have saved the model and want to use this model for data belonging to ,let’s assume, last year. But the number of variables in my last year’s data has changed. How to approach this situation? Is there any way to scale this model to fit the new case?

Would like to know if the following process (essentially what you did in this post) is considered best practice or common practice in ML and DS:
1. Perform cross-validation to tune hyperparameters
2. using optimised hyperparameters to train the ENTIRE training dataset to arrive at an optimal accuracy?

And also, in part 3 on saving and loading the model, I am encountering problems with the doMC package in R and I understand that doMC is not usable in windows. Could you suggest alternative workaround for this?

Hello. After choosing the best model, how am I loading it and where can I find my results? For example I want to predict the price of an equity tomorrow. How can I load my optimal neural network, how can input the new inputs and where can I find my actual Results?

A very good artical much helpful and I want to ask you one thing regarding deployment of my “R-language – Telecom Churn” prediction model into my production environment) ,I am clear upto saving stand alone program and how to predict on new test data,I joined recently in my job so i want to know about this,could you help me regarding this or please share relevent links about how to consume “Prediction Model” .