Last Updated on

You need to know how well your algorithms perform on unseen data.

The best way to evaluate the performance of an algorithm would be to make predictions for new data to which you already know the answers. The second best way is to use clever techniques from statistics called resampling methods that allow you to make accurate estimates for how well your algorithm will perform on new data.

In this post you will discover how you can estimate the accuracy of your machine learning algorithms using resampling methods in Python and scikit-learn.

Discover how to prepare data with pandas, fit and evaluate models with scikit-learn, and more in my new book, with 16 step-by-step tutorials, 3 projects, and full python code.

Let’s get started.

**Update Jan/2017**: Updated to reflect changes to the scikit-learn API in version 0.18.**Update Oct/2017**: Updated print statements to work with Python 3.**Update March/2018**: Added alternate link to download the dataset as the original appears to have been taken down.

## About The Recipes

Resampling methods are demonstrated in this post using small code recipes in Python.

Each recipe is designed to be standalone so that you can copy-and-paste it into your project and use it immediately.

The Pima Indians onset of diabetes dataset is used in each recipe. This is a binary classification problem where all of the input variables are numeric. In each recipe it is downloaded directly from the UCI Machine Learning repository (update: download from here). You can replace it with your own dataset as needed.

## Evaluate Your Machine Learning Algorithms

Why can’t you train your machine learning algorithm on your dataset and use predictions from this same dataset to evaluate machine learning algorithms?

The simple answer is overfitting.

Imagine an algorithm that remembers every observation it is shown. If you evaluated your machine learning algorithm on the same dataset used to train the algorithm, then an algorithm like this would have a perfect score on the training dataset. But the predictions it made on new data would be terrible.

We must evaluate our machine learning algorithms on data that is not used to train the algorithm.

The evaluation is an estimate that we can use to talk about how well we think the algorithm may actually do in practice. It is not a guarantee of performance.

Once we estimate the performance of our algorithm, we can then re-train the final algorithm on the entire training dataset and get it ready for operational use.

Next up we are going to look at four different techniques that we can use to split up our training dataset and create useful estimates of performance for our machine learning algorithms:

- Train and Test Sets.
- K-fold Cross Validation.
- Leave One Out Cross Validation.
- Repeated Random Test-Train Splits.

We will start with the simplest method called Train and Test Sets.

### Need help with Machine Learning in Python?

Take my free 2-week email course and discover data prep, algorithms and more (with code).

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

## 1. Split into Train and Test Sets

The simplest method that we can use to evaluate the performance of a machine learning algorithm is to use different training and testing datasets.

We can take our original dataset, split it into two parts. Train the algorithm on the first part, make predictions on the second part and evaluate the predictions against the expected results.

The size of the split can depend on the size and specifics of your dataset, although it is common to use 67% of the data for training and the remaining 33% for testing.

This algorithm evaluation technique is very fast. It is ideal for large datasets (millions of records) where there is strong evidence that both splits of the data are representative of the underlying problem. Because of the speed, it is useful to use this approach when the algorithm you are investigating is slow to train.

A downside of this technique is that it can have a high variance. This means that differences in the training and test dataset can result in meaningful differences in the estimate of accuracy.

In the example below we split the data Pima Indians dataset into 67%/33% split for training and test and evaluate the accuracy of a Logistic Regression model.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Evaluate using a train and a test set import pandas from sklearn import model_selection from sklearn.linear_model import LogisticRegression url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv" names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] dataframe = pandas.read_csv(url, names=names) array = dataframe.values X = array[:,0:8] Y = array[:,8] test_size = 0.33 seed = 7 X_train, X_test, Y_train, Y_test = model_selection.train_test_split(X, Y, test_size=test_size, random_state=seed) model = LogisticRegression() model.fit(X_train, Y_train) result = model.score(X_test, Y_test) print("Accuracy: %.3f%%" % (result*100.0)) |

We can see that the estimated accuracy for the model was approximately 75%. Note that in addition to specifying the size of the split, we also specify the random seed. Because the split of the data is random, we want to ensure that the results are reproducible. By specifying the random seed we ensure that we get the same random numbers each time we run the code.

This is important if we want to compare this result to the estimated accuracy of another machine learning algorithm or the same algorithm with a different configuration. To ensure the comparison was apples-for-apples, we must ensure that they are trained and tested on the same data.

1 |
Accuracy: 75.591% |

## 2. K-fold Cross Validation

Cross validation is an approach that you can use to estimate the performance of a machine learning algorithm with less variance than a single train-test set split.

It works by splitting the dataset into k-parts (e.g. k=5 or k=10). Each split of the data is called a fold. The algorithm is trained on k-1 folds with one held back and tested on the held back fold. This is repeated so that each fold of the dataset is given a chance to be the held back test set.

After running cross validation you end up with k different performance scores that you can summarize using a mean and a standard deviation.

The result is a more reliable estimate of the performance of the algorithm on new data given your test data. It is more accurate because the algorithm is trained and evaluated multiple times on different data.

The choice of k must allow the size of each test partition to be large enough to be a reasonable sample of the problem, whilst allowing enough repetitions of the train-test evaluation of the algorithm to provide a fair estimate of the algorithms performance on unseen data. For modest sized datasets in the thousands or tens of thousands of records, k values of 3, 5 and 10 are common.

In the example below we use 10-fold cross validation.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Evaluate using Cross Validation import pandas from sklearn import model_selection from sklearn.linear_model import LogisticRegression url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv" names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] dataframe = pandas.read_csv(url, names=names) array = dataframe.values X = array[:,0:8] Y = array[:,8] num_instances = len(X) seed = 7 kfold = model_selection.KFold(n_splits=10, random_state=seed) model = LogisticRegression() results = model_selection.cross_val_score(model, X, Y, cv=kfold) print("Accuracy: %.3f%% (%.3f%%)" % (results.mean()*100.0, results.std()*100.0)) |

You can see that we report both the mean and the standard deviation of the performance measure. When summarizing performance measures, it is a good practice to summarize the distribution of the measures, in this case assuming a Gaussian distribution of performance (a very reasonable assumption) and recording the mean and standard deviation.

1 |
Accuracy: 76.951% (4.841%) |

## 3. Leave One Out Cross Validation

You can configure cross validation so that the size of the fold is 1 (k is set to the number of observations in your dataset). This variation of cross validation is called leave-one-out cross validation.

The result is a large number of performance measures that can be summarized in an effort to give a more reasonable estimate of the accuracy of your model on unseen data. A downside is that it can be a computationally more expensive procedure than k-fold cross validation.

In the example below we use leave-one-out cross validation.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Evaluate using Leave One Out Cross Validation import pandas from sklearn import model_selection from sklearn.linear_model import LogisticRegression url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv" names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] dataframe = pandas.read_csv(url, names=names) array = dataframe.values X = array[:,0:8] Y = array[:,8] num_folds = 10 num_instances = len(X) loocv = model_selection.LeaveOneOut() model = LogisticRegression() results = model_selection.cross_val_score(model, X, Y, cv=loocv) print("Accuracy: %.3f%% (%.3f%%)" % (results.mean()*100.0, results.std()*100.0)) |

You can see in the standard deviation that the score has more variance than the k-fold cross validation results described above.

1 |
Accuracy: 76.823% (42.196%) |

## 4. Repeated Random Test-Train Splits

Another variation on k-fold cross validation is to create a random split of the data like the train/test split described above, but repeat the process of splitting and evaluation of the algorithm multiple times, like cross validation.

This has the speed of using a train/test split and the reduction in variance in the estimated performance of k-fold cross validation. You can also repeat the process many more times as need. A down side is that repetitions may include much of the same data in the train or the test split from run to run, introducing redundancy into the evaluation.

The example below splits the data into a 67%/33% train/test split and repeats the process 10 times.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Evaluate using Shuffle Split Cross Validation import pandas from sklearn import model_selection from sklearn.linear_model import LogisticRegression url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv" names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class'] dataframe = pandas.read_csv(url, names=names) array = dataframe.values X = array[:,0:8] Y = array[:,8] num_samples = 10 test_size = 0.33 num_instances = len(X) seed = 7 kfold = model_selection.ShuffleSplit(n_splits=10, test_size=test_size, random_state=seed) model = LogisticRegression() results = model_selection.cross_val_score(model, X, Y, cv=kfold) print("Accuracy: %.3f%% (%.3f%%)" % (results.mean()*100.0, results.std()*100.0)) |

We can see that the distribution of the performance measure is on par with k-fold cross validation above.

1 |
Accuracy: 76.496% (1.698%) |

## What Techniques to Use When

- Generally k-fold cross validation is the gold-standard for evaluating the performance of a machine learning algorithm on unseen data with k set to 3, 5, or 10.
- Using a train/test split is good for speed when using a slow algorithm and produces performance estimates with lower bias when using large datasets.
- Techniques like leave-one-out cross validation and repeated random splits can be useful intermediates when trying to balance variance in the estimated performance, model training speed and dataset size.

The best advice is to experiment and find a technique for your problem that is fast and produces reasonable estimates of performance that you can use to make decisions. If in doubt, use 10-fold cross validation.

## Summary

In this post you discovered statistical techniques that you can use to estimate the performance of your machine learning algorithms, called resampling.

Specifically, you learned about:

- Train and Test Sets.
- Cross Validation.
- Leave One Out Cross Validation.
- Repeated Random Test-Train Splits.

Do you have any questions about resampling methods or this post? Ask your question in the comments and I will do my best to answer it.

Mr. Brownlee, thanks for your brilliant explanation. I only suggest you to verify some modifications in Scikit Learn module in newer version. Consider, for example, replacing “from sklearn import cross_validation” for “from sklearn.model_selection import cross_val_score, KFold” and correlate changes in lines 15 and 17 of fourth part code.

“

Thanks for the suggestion Vinicius, I’ll schedule the change ASAP.

Thanks for such expalantory topics. I am learning data science from scratch and i found it very helpful. Can you please tell me, how to find mean square error for above mentioned techniques. I have evaluated rmse for test train but how to do it for k-fold and looc. Thanks.

Hi. Is it OK to use the entire dataset for testing after subjecting it to partition and split along with 10 fold cross validation? There is only a slight variation wrt results compared to random split..

Data used to estimate the skill (test) cannot be used to train the model (train).

If it is, the estimate will be biased.

Thanks for your reply.

Hi Jason. I used 10 fold cross validation for a dataset with 2,23,585 samples. I tried random split of 80:20 also. There is no change in the result. Apart from SVM and LR no other model seem to work well on this dataset. Accuracy is 0.97. Recall is 0.99. can i assume that my model is working fine?

Sounds good. Well done.

Hi Jason

When we apply k-fold cross validation to regression model. Terms like accuracy do not make much sense, so is there any different function is used k-fold validation under regression problem?

You will estimate model error, such as MSE, RMSE, or MAE.

Hi Jason, great article (and site!)

My question is in regards to cross-validation. In my experience, we usually divide a dataset to training and test sets, and then further divide the training set into a training and validation set, where the validation set is used to tune the machine learning hyperparameters (e.g. lasso penalty coefficient, or min. sample leafs on a decision tree) .

My question is, during cross-validation, should we settle on a certain set of hyperparameters from one train_test_split, or re-tune the hyperparameters every time? In the latter case, we run the risk of the model hyperparameters being different from fold to fold… is that a problem, or acceptable albeit computationally expensive?

Thanks,

Great question, it is really up to you.

If you have the resources, it can be good to tune the model each time, record what was chosen and use an average of the parameters when you tune the final model.

Hi Jason,

Your site is the best for machine learning enthusiasts, I would like to get more explanation on mean and variance that we calculate when finding accuracy, what does they tell us of the results?

Sure:

https://machinelearningmastery.com/statistical-data-distributions/

in Kfold section you are finding accuracy, is this accuracy on test data or training data or the whole dataset? actually, I understood the concept of implementation of train-test split, but have a bit doubt how to fit data while using kfold??

results = model_selection.cross_val_score(model, X, Y, cv=kfold)

print(“Accuracy: %.3f%% (%.3f%%)” % (results.mean()*100.0, results.std()*100.0))

Neither, it uses k-fold cross validation. You can learn more here:

https://machinelearningmastery.com/k-fold-cross-validation/

Hi Jason Brownlee, I have dataset classify part manually and train algorithm for prediction for other parts depended on the train data what the method better for me to evaluate the performance of Machine Learning Algorithms,

You can learn more about how to evaluate algorithms here:

https://machinelearningmastery.com/faq/single-faq/how-do-i-evaluate-a-machine-learning-algorithm

hi.

it’s a good example for a beginner like me. However, how can i produce the confusion matrix obtained from the k-fold cross validation?

You cannot get a confusion matrix from multiple runs, such as over CV.

You can get a confusion matrix from a single run, such as on a train/test split.

This post will show you how:

https://machinelearningmastery.com/confusion-matrix-machine-learning/

hi mr. jason,

how if i want to use another metric likes recall, precision and f1-score for multiclass label?

No problem you can use the implementations from scikit-learn:

https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics

It’s my understanding that cross_val_score does not actually fit a model. If I use repeated k fold cross validation, how can I fit the model on these splits? And then use this model to predict? Repeated k-fold is nice because it can tune and train in the same step and I don’t have to fit a “final model”. Take the following example from sklearn webpage:

import numpy as np

from sklearn.model_selection import RepeatedKFold

X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])

y = np.array([0, 0, 1, 1])

rkf = RepeatedKFold(n_splits=2, n_repeats=2, random_state=2652124)

for train_index, test_index in rkf.split(X):

print(“TRAIN:”, train_index, “TEST:”, test_index)

X_train, X_test = X[train_index], X[test_index]

y_train, y_test = y[train_index], y[test_index]

I want to fit a model on these splits.

Does something like the following work?

logit = LogisticRegression()

rkf = RepeatedKFold(n_splits=2, n_repeats=2, random_state=2652124)

for train_index, test_index in rkf.split(X):

print(“TRAIN:”, train_index, “TEST:”, test_index)

X_train, X_test = X[train_index], X[test_index]

y_train, y_test = y[train_index], y[test_index]

model_fit = logit.fit(X_train, Y_train)

Cross validation is only used to evaluate the model.

Once you choose a model/config, you can fit a final model and use it for making predictions, more details here:

https://machinelearningmastery.com/train-final-machine-learning-model/