Making developers awesome at machine learning
Making developers awesome at machine learning
Machine learning models are chosen based on their mean performance, often calculated using k-fold cross-validation.
The algorithm with the best mean performance is expected to be better than those algorithms with worse mean performance. But what if the difference in the mean performance is caused by a statistical fluke?
The solution is to use astatistical hypothesis test to evaluate whether the difference in the mean performance between any two algorithms is real or not.
In this tutorial, you will discover how to use statistical hypothesis tests for comparing machine learning algorithms.
After completing this tutorial, you will know:
Kick-start your project with my new bookStatistics for Machine Learning, includingstep-by-step tutorials and thePython source code files for all examples.
Let’s get started.

Hypothesis Test for Comparing Machine Learning Algorithms
Photo byFrank Shepherd, some rights reserved.
This tutorial is divided into three parts; they are:
Model selection involves evaluating a suite of different machine learning algorithms or modeling pipelines and comparing them based on their performance.
The model or modeling pipeline that achieves the best performance according to your performance metric is then selected as your final model that you can then use to start making predictions on new data.
This applies to regression and classification predictive modeling tasks with classical machine learning algorithms and deep learning. It’s always the same process.
The problem is, how do you know the difference between two models is real and not just a statistical fluke?
This problem can be addressed using astatistical hypothesis test.
One approach is to evaluate each model on the samek-fold cross-validation split of the data (e.g. using the same random number seed to split the data in each case) and calculate a score for each split. This would give a sample of 10 scores for 10-fold cross-validation. The scores can then be compared using a paired statistical hypothesis test because the same treatment (rows of data) was used for each algorithm to come up with each score. ThePaired Student’s t-Test could be used.
A problem with using the Paired Student’s t-Test, in this case, is that each evaluation of the model is not independent. This is because the same rows of data are used to train the data multiple times — actually, each time, except for the time a row of data is used in the hold-out test fold. This lack of independence in the evaluation means that the Paired Student’s t-Test is optimistically biased.
This statistical test can be adjusted to take the lack of independence into account. Additionally, the number of folds and repeats of the procedure can be configured to achieve a good sampling of model performance that generalizes well to a wide range of problems and algorithms. Specifically two-fold cross-validation with five repeats, so-called 5×2-fold cross-validation.
This approach was proposed byThomas Dietterich in his 1998 paper titled “Approximate Statistical Tests for Comparing Supervised Classification Learning Algorithms.”
For more on this topic, see the tutorial:
Thankfully, we don’t need to implement this procedure ourselves.
TheMLxtend library bySebastian Raschka provides an implementation via thepaired_ttest_5x2cv() function.
First, you must install the mlxtend library, for example:
1 | sudo pip install mlxtend |
To use the evaluation, you must first load your dataset, then define the two models that you wish to compare.
1 2 3 4 5 6 | ... # load data X,y=.... # define models model1=... model2=... |
You can then call thepaired_ttest_5x2cv() function and pass in your data and models and it will report the t-statistic value and the p-value as to whether the difference in the performance of the two algorithms is significant or not.
1 2 3 | ... # compare algorithms t,p=paired_ttest_5x2cv(estimator1=model1,estimator2=model2,X=X,y=y) |
The p-value must be interpreted using an alpha value, which is the significance level that you are willing to accept.
If the p-value is less or equal to the chosen alpha, we reject the null hypothesis that the models have the same mean performance, which means the difference is probably real. If the p-value is greater than alpha, we fail to reject the null hypothesis that the models have the same mean performance and any observed difference in the mean accuracies is probability a statistical fluke.
The smaller the alpha value, the better, and a common value is 5 percent (0.05).
1 2 3 4 5 6 | ... # interpret the result ifp<=0.05: print('Difference between mean performance is probably real') else: print('Algorithms probably have the same performance') |
Now that we are familiar with the way to use a hypothesis test to compare algorithms, let’s look at some examples.
In this section, let’s compare the performance of two machine learning algorithms on a binary classification task, then check if the observed difference is statistically significant or not.
First, we can use themake_classification() function to create a synthetic dataset with 1,000 samples and 20 input variables.
The example below creates the dataset and summarizes its shape.
1 2 3 4 5 6 | # create classification dataset fromsklearn.datasetsimportmake_classification # define dataset X,y=make_classification(n_samples=1000,n_features=10,n_informative=10,n_redundant=0,random_state=1) # summarize the dataset print(X.shape,y.shape) |
Running the example creates the dataset and summarizes the number of rows and columns, confirming our expectations.
We can use this data as the basis for comparing two algorithms.
1 | (1000, 10) (1000,) |
We will compare the performance of two linear algorithms on this dataset. Specifically, alogistic regression algorithm and alinear discriminant analysis (LDA) algorithm.
The procedure I like is to use repeated stratified k-fold cross-validation with 10 folds and three repeats. We will use this procedure to evaluate each algorithm and return and report the mean classification accuracy.
The complete example is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # compare logistic regression and lda for binary classification fromnumpyimportmean fromnumpyimportstd fromsklearn.datasetsimportmake_classification fromsklearn.model_selectionimportcross_val_score fromsklearn.model_selectionimportRepeatedStratifiedKFold fromsklearn.linear_modelimportLogisticRegression fromsklearn.discriminant_analysisimportLinearDiscriminantAnalysis frommatplotlibimportpyplot # define dataset X,y=make_classification(n_samples=1000,n_features=10,n_informative=10,n_redundant=0,random_state=1) # evaluate model 1 model1=LogisticRegression() cv1=RepeatedStratifiedKFold(n_splits=10,n_repeats=3,random_state=1) scores1=cross_val_score(model1,X,y,scoring='accuracy',cv=cv1,n_jobs=-1) print('LogisticRegression Mean Accuracy: %.3f (%.3f)'%(mean(scores1),std(scores1))) # evaluate model 2 model2=LinearDiscriminantAnalysis() cv2=RepeatedStratifiedKFold(n_splits=10,n_repeats=3,random_state=1) scores2=cross_val_score(model2,X,y,scoring='accuracy',cv=cv2,n_jobs=-1) print('LinearDiscriminantAnalysis Mean Accuracy: %.3f (%.3f)'%(mean(scores2),std(scores2))) # plot the results pyplot.boxplot([scores1,scores2],labels=['LR','LDA'],showmeans=True) pyplot.show() |
Running the example first reports the mean classification accuracy for each algorithm.
Note: Yourresults may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.
In this case, the results suggest that LDA has better performance if we just look at the mean scores: 89.2 percent for logistic regression and 89.3 percent for LDA.
1 2 | LogisticRegression Mean Accuracy: 0.892 (0.036) LinearDiscriminantAnalysis Mean Accuracy: 0.893 (0.033) |
A box and whisker plot is also created summarizing the distribution of accuracy scores.
This plot would support my decision in choosing LDA over LR.

Box and Whisker Plot of Classification Accuracy Scores for Two Algorithms
Now we can use a hypothesis test to see if the observed results are statistically significant.
First, we will use the 5×2 procedure to evaluate the algorithms and calculate a p-value and test statistic value.
1 2 3 4 5 | ... # check if difference between algorithms is real t,p=paired_ttest_5x2cv(estimator1=model1,estimator2=model2,X=X,y=y,scoring='accuracy',random_seed=1) # summarize print('P-value: %.3f, t-Statistic: %.3f'%(p,t)) |
We can then interpret the p-value using an alpha of 5 percent.
1 2 3 4 5 6 | ... # interpret the result ifp<=0.05: print('Difference between mean performance is probably real') else: print('Algorithms probably have the same performance') |
Tying this together, the complete example is listed below.
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 | # use 5x2 statistical hypothesis testing procedure to compare two machine learning algorithms fromnumpyimportmean fromnumpyimportstd fromsklearn.datasetsimportmake_classification fromsklearn.model_selectionimportcross_val_score fromsklearn.model_selectionimportRepeatedStratifiedKFold fromsklearn.linear_modelimportLogisticRegression fromsklearn.discriminant_analysisimportLinearDiscriminantAnalysis frommlxtend.evaluateimportpaired_ttest_5x2cv # define dataset X,y=make_classification(n_samples=1000,n_features=10,n_informative=10,n_redundant=0,random_state=1) # evaluate model 1 model1=LogisticRegression() cv1=RepeatedStratifiedKFold(n_splits=10,n_repeats=3,random_state=1) scores1=cross_val_score(model1,X,y,scoring='accuracy',cv=cv1,n_jobs=-1) print('LogisticRegression Mean Accuracy: %.3f (%.3f)'%(mean(scores1),std(scores1))) # evaluate model 2 model2=LinearDiscriminantAnalysis() cv2=RepeatedStratifiedKFold(n_splits=10,n_repeats=3,random_state=1) scores2=cross_val_score(model2,X,y,scoring='accuracy',cv=cv2,n_jobs=-1) print('LinearDiscriminantAnalysis Mean Accuracy: %.3f (%.3f)'%(mean(scores2),std(scores2))) # check if difference between algorithms is real t,p=paired_ttest_5x2cv(estimator1=model1,estimator2=model2,X=X,y=y,scoring='accuracy',random_seed=1) # summarize print('P-value: %.3f, t-Statistic: %.3f'%(p,t)) # interpret the result ifp<=0.05: print('Difference between mean performance is probably real') else: print('Algorithms probably have the same performance') |
Running the example, we first evaluate the algorithms before, then report on the result of the statistical hypothesis test.
Note: Yourresults may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.
In this case, we can see that the p-value is about 0.3, which is much larger than 0.05. This leads us to fail to reject the null hypothesis, suggesting that any observed difference between the algorithms is probably not real.
We could just as easily choose logistic regression or LDA and both would perform about the same on average.
This highlights that performing model selection based only on the mean performance may not be sufficient.
1 2 3 4 | LogisticRegression Mean Accuracy: 0.892 (0.036) LinearDiscriminantAnalysis Mean Accuracy: 0.893 (0.033) P-value: 0.328, t-Statistic: 1.085 Algorithms probably have the same performance |
Recall that we are reporting performance using a different procedure (3×10 CV) than the procedure used to estimate the performance in the statistical test (5×2 CV). Perhaps results would be different if we looked at scores using five repeats of two-fold cross-validation?
The example below is updated to report classification accuracy for each algorithm using 5×2 CV.
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 | # use 5x2 statistical hypothesis testing procedure to compare two machine learning algorithms fromnumpyimportmean fromnumpyimportstd fromsklearn.datasetsimportmake_classification fromsklearn.model_selectionimportcross_val_score fromsklearn.model_selectionimportRepeatedStratifiedKFold fromsklearn.linear_modelimportLogisticRegression fromsklearn.discriminant_analysisimportLinearDiscriminantAnalysis frommlxtend.evaluateimportpaired_ttest_5x2cv # define dataset X,y=make_classification(n_samples=1000,n_features=10,n_informative=10,n_redundant=0,random_state=1) # evaluate model 1 model1=LogisticRegression() cv1=RepeatedStratifiedKFold(n_splits=2,n_repeats=5,random_state=1) scores1=cross_val_score(model1,X,y,scoring='accuracy',cv=cv1,n_jobs=-1) print('LogisticRegression Mean Accuracy: %.3f (%.3f)'%(mean(scores1),std(scores1))) # evaluate model 2 model2=LinearDiscriminantAnalysis() cv2=RepeatedStratifiedKFold(n_splits=2,n_repeats=5,random_state=1) scores2=cross_val_score(model2,X,y,scoring='accuracy',cv=cv2,n_jobs=-1) print('LinearDiscriminantAnalysis Mean Accuracy: %.3f (%.3f)'%(mean(scores2),std(scores2))) # check if difference between algorithms is real t,p=paired_ttest_5x2cv(estimator1=model1,estimator2=model2,X=X,y=y,scoring='accuracy',random_seed=1) # summarize print('P-value: %.3f, t-Statistic: %.3f'%(p,t)) # interpret the result ifp<=0.05: print('Difference between mean performance is probably real') else: print('Algorithms probably have the same performance') |
Running the example reports the mean accuracy for both algorithms and the results of the statistical test.
Note: Yourresults may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.
In this case, we can see that the difference in the mean performance for the two algorithms is even larger, 89.4 percent vs. 89.0 percent in favor of logistic regression instead of LDA as we saw with 3×10 CV.
1 2 3 4 | LogisticRegression Mean Accuracy: 0.894 (0.012) LinearDiscriminantAnalysis Mean Accuracy: 0.890 (0.013) P-value: 0.328, t-Statistic: 1.085 Algorithms probably have the same performance |
This section provides more resources on the topic if you are looking to go deeper.
In this tutorial, you discovered how to use statistical hypothesis tests for comparing machine learning algorithms.
Specifically, you learned:
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

...by writing lines of code in python
Discover how in my new Ebook:
Statistical Methods for Machine Learning
It providesself-study tutorials on topics like:
Hypothesis Tests, Correlation, Nonparametric Stats, Resampling, and much more...
Skip the Academics. Just Results.






Thanks for the post!!
Another possible option would be the bayesian approach through BEST
Really it was good….for those who have not proper knowledge can easily understand. I m Associate professor in Statistics in one of the reputed Science college.
Dear Dr Jason,
I have extended the above accuracy with the models used athttps://machinelearningmastery.com/calculate-the-bias-variance-trade-off/#comment-550512. That is I have made pairwise combinations of the models from that site and this site and produced the following results.
This contained statistically significant and not significant comparisons
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | comparing LogisticRegression() with KNeighborsClassifier() LogisticRegression()MeanAccuracy:0.892(0.034) KNeighborsClassifier()MeanAccuracy:0.942(0.022) P-value:0.131,t-Statistic:-1.802 notstatisticallysignificant ------------------------------ comparing LogisticRegression() with DecisionTreeClassifier() LogisticRegression()MeanAccuracy:0.892(0.034) DecisionTreeClassifier()MeanAccuracy:0.831(0.035) P-value:0.004,t-Statistic:5.098 statisticallysignificant ------------------------------ comparing LogisticRegression() with SVC() LogisticRegression()MeanAccuracy:0.892(0.034) SVC()MeanAccuracy:0.952(0.021) P-value:0.003,t-Statistic:-5.318 statisticallysignificant ------------------------------ comparing LogisticRegression() with GaussianNB() LogisticRegression()MeanAccuracy:0.892(0.034) GaussianNB()MeanAccuracy:0.866(0.039) P-value:0.368,t-Statistic:0.988 notstatisticallysignificant ------------------------------ comparing LogisticRegression() with LinearDiscriminantAnalysis() LogisticRegression()MeanAccuracy:0.892(0.034) LinearDiscriminantAnalysis()MeanAccuracy:0.894(0.031) P-value:0.328,t-Statistic:1.085 notstatisticallysignificant ------------------------------ comparing KNeighborsClassifier() with DecisionTreeClassifier() KNeighborsClassifier()MeanAccuracy:0.942(0.022) DecisionTreeClassifier()MeanAccuracy:0.832(0.033) P-value:0.007,t-Statistic:4.420 statisticallysignificant ------------------------------ comparing KNeighborsClassifier() with SVC() KNeighborsClassifier()MeanAccuracy:0.942(0.022) SVC()MeanAccuracy:0.952(0.021) P-value:0.028,t-Statistic:-3.062 statisticallysignificant ------------------------------ comparing KNeighborsClassifier() with GaussianNB() KNeighborsClassifier()MeanAccuracy:0.942(0.022) GaussianNB()MeanAccuracy:0.866(0.039) P-value:0.211,t-Statistic:1.434 notstatisticallysignificant ------------------------------ comparing KNeighborsClassifier() with LinearDiscriminantAnalysis() KNeighborsClassifier()MeanAccuracy:0.942(0.022) LinearDiscriminantAnalysis()MeanAccuracy:0.894(0.031) P-value:0.136,t-Statistic:1.777 notstatisticallysignificant ------------------------------ comparing DecisionTreeClassifier() with SVC() DecisionTreeClassifier()MeanAccuracy:0.829(0.040) SVC()MeanAccuracy:0.952(0.021) P-value:0.002,t-Statistic:-5.782 statisticallysignificant ------------------------------ comparing DecisionTreeClassifier() with GaussianNB() DecisionTreeClassifier()MeanAccuracy:0.831(0.036) GaussianNB()MeanAccuracy:0.866(0.039) P-value:0.041,t-Statistic:-2.732 statisticallysignificant ------------------------------ comparing DecisionTreeClassifier() with LinearDiscriminantAnalysis() DecisionTreeClassifier()MeanAccuracy:0.831(0.034) LinearDiscriminantAnalysis()MeanAccuracy:0.894(0.031) P-value:0.002,t-Statistic:-6.124 statisticallysignificant ------------------------------ comparing SVC() with GaussianNB() SVC()MeanAccuracy:0.952(0.021) GaussianNB()MeanAccuracy:0.866(0.039) P-value:0.018,t-Statistic:3.467 statisticallysignificant ------------------------------ comparing SVC() with LinearDiscriminantAnalysis() SVC()MeanAccuracy:0.952(0.021) LinearDiscriminantAnalysis()MeanAccuracy:0.894(0.031) P-value:0.003,t-Statistic:5.191 statisticallysignificant ------------------------------ comparing GaussianNB() with LinearDiscriminantAnalysis() GaussianNB()MeanAccuracy:0.866(0.039)<p LinearDiscriminantAnalysis()MeanAccuracy:0.894(0.031) P-value:0.450,t-Statistic:-0.819 notstatisticallysignificant ------------------------------ |
The statistically significant models were:
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 61 | comparing LogisticRegression() with DecisionTreeClassifier() LogisticRegression()MeanAccuracy:0.892(0.034) DecisionTreeClassifier()MeanAccuracy:0.831(0.035) P-value:0.004,t-Statistic:5.098 statisticallysignificant ------------------------------ comparing LogisticRegression() with SVC() LogisticRegression()MeanAccuracy:0.892(0.034) SVC()MeanAccuracy:0.952(0.021) P-value:0.003,t-Statistic:-5.318 statisticallysignificant ------------------------------ comparing KNeighborsClassifier() with DecisionTreeClassifier() KNeighborsClassifier()MeanAccuracy:0.942(0.022) DecisionTreeClassifier()MeanAccuracy:0.832(0.033) P-value:0.007,t-Statistic:4.420 statisticallysignificant ------------------------------ comparing KNeighborsClassifier() with SVC() KNeighborsClassifier()MeanAccuracy:0.942(0.022) SVC()MeanAccuracy:0.952(0.021) P-value:0.028,t-Statistic:-3.062 statisticallysignificant ------------------------------ comparing DecisionTreeClassifier() with SVC() DecisionTreeClassifier()MeanAccuracy:0.829(0.040) SVC()MeanAccuracy:0.952(0.021) P-value:0.002,t-Statistic:-5.782 statisticallysignificant ------------------------------ comparing DecisionTreeClassifier() with GaussianNB() DecisionTreeClassifier()MeanAccuracy:0.831(0.036) GaussianNB()MeanAccuracy:0.866(0.039) P-value:0.041,t-Statistic:-2.732 statisticallysignificant ------------------------------ comparing DecisionTreeClassifier() with LinearDiscriminantAnalysis() DecisionTreeClassifier()MeanAccuracy:0.831(0.034) LinearDiscriminantAnalysis()MeanAccuracy:0.894(0.031) P-value:0.002,t-Statistic:-6.124 statisticallysignificant ------------------------------ comparing SVC() with GaussianNB() SVC()MeanAccuracy:0.952(0.021) GaussianNB()MeanAccuracy:0.866(0.039) P-value:0.018,t-Statistic:3.467 statisticallysignificant ------------------------------ comparing SVC() with LinearDiscriminantAnalysis() SVC()MeanAccuracy:0.952(0.021) LinearDiscriminantAnalysis()MeanAccuracy:0.894(0.031) P-value:0.003,t-Statistic:5.191 statisticallysignificant |
Conclusion:
Out of the statistically significant models SVC had the highest accuracy of 0.952 compared to LDA of 0.894. The p-value is 0.003
Thank you,
Anthony of Sydney
Dear Dr Jason,
apologies, I forgot to consider the comparison of SVC and KNeighborsClassifier with average values of 0.952 and 0.942 respectively and significant with p-value of 0.028.
Further conclusion:
Though there was little difference in accuracy between SVC and KNeighborsClassifier, it appears for the particular dataset consisting of X and y, SVC is likely to be the most suitable method for accuracy.
So if one is to made predictions for a given dataset X, y , SVC is likely to be model of choice.
Thank you,
Anthony of Sydney
Nice work!
A good way to present pair-wise hypothesis tests is in a matrix with algorithms along both axis and significant true/false in each cell of the matrix.
Dear Dr Jason,
Thank you for your reply.
When you say that a “…good way to present pair wise hypothesis testing in a matrix…” could you elaborate please. Do you mean pair-wise boxplots, with a scatter matrix pairs?
Is there a scatter_matrix that enables one to switch from a scatter plot to a pairwise comparison of boxplots?
Thank you,
Anthony of Sydney
No, not a plot, a matrix or table with true/false values indicating whether there is a significant difference between each pair of algorithms or not.
One can then review the actual mean values for each algorithm that has significant results and ignore the rest.
A list of pairs can also be used.
Dear Dr Jason,
Thank you for that.
Do you mean a table such as this:
1 2 3 4 5 | ModelCombinationPair. Model1 score1std1 Model2score2std1 sig/nodiff LDA() SVC() LDA 0.894 0.03 SVC 0.952 0.021sig ........................... ........................... DTC()LDA() DTC 0.831 0.03 LDA 0.8940.031sig |
Please advise.
Thank you,
Anthony of Sydney
I don’t think so. It was something I did way back in my phd days.
Dear Dr Jason,
Please widen the above ‘table’ it shows
1 | Model,CombinationPair,model1,score1,std1,model2,score2,std2,sig/notsig |
Thank you,
Anthony of Sydney
Dear Dr Jason,
A modification to the program, produced this list:
1 2 3 4 5 6 7 8 9 10 | modelpairs,model1,mean,std,model2,mean,std,sig/notsig lr&cart, lr,0.89,0.03, cart,0.83,0.03, sig lr&svm, lr,0.89,0.03, svm,0.95,0.02, sig knn&cart, knn,0.94,0.02, cart,0.83,0.03, sig knn&svm, knn,0.94,0.02, svm,0.95,0.02, sig cart&svm, cart,0.83,0.03, svm,0.95,0.02, sig cart&bayes, cart,0.83,0.04, bayes,0.87,0.04, sig cart&lda, cart,0.83,0.04, lda,0.89,0.03, sig svm&bayes, svm,0.95,0.02, bayes,0.87,0.04, sig svm&lda, svm,0.95,0.02, lda,0.89,0.03, sig |
Do you mean something like the above?
If so is there a way to display text in a nice way = the text aligns nicely.
Thank you,
Anthony of Sydney
Dear Dr Jason,
This is a text-graphic of the output using the package ‘prettyable’ fromhttps://pypi.org/project/PrettyTable/
1 | pipinstallprettytable--upgrade |
Some code to demonstrate implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | fromprettytableimportPrettyTable ................ ................ #values of the *_values determined elsewhere................ x=PrettyTable() column_names=["model pairs","model1","mean1","std","model2","mean","std","sig/not sig"]x.add_column(column_names[0],models_values) x.add_column(column_names[1],model_values) x.add_column(column_names[2],mean1_values) x.add_column(column_names[3],std1_values) x.add_column(column_names[4],model_values) x.add_column(column_names[5],mean2_values) x.add_column(column_names[6],std2_values) x.add_column(column_names[7],sig_values) print(x) |
Output – expand the width of the page by hovering your mouse over the top of this output to view complete viewing.
1 2 3 4 5 6 7 8 9 10 11 12 13 | +--------------+--------+-------+-------+--------+-------+-------+-------------+ |modelpairs |model1|mean1| std |model2| mean| std |sig/notsig| +--------------+--------+-------+-------+--------+-------+-------+-------------+ | lr&cart | lr |0.892|0.034| cart |0.828|0.037| sig | | lr&svm | lr |0.892|0.034| svm |0.952|0.021| sig | | knn&cart | knn |0.942|0.022| cart |0.832|0.039| sig | | knn&svm | knn |0.942|0.022| svm |0.952|0.021| sig | | cart&svm | cart |0.830|0.033| svm |0.952|0.021| sig | |cart&bayes| cart |0.832|0.038|bayes |0.866|0.039| sig | | cart&lda | cart |0.830|0.039| lda |0.894|0.031| sig | |svm&bayes | svm |0.952|0.021|bayes |0.866|0.039| sig | | svm&lda | svm |0.952|0.021| lda |0.894|0.031| sig | +--------------+--------+-------+-------+--------+-------+-------+-------------+ |
Thank you,
Anthony of Sydney
Very cool.
Weka does this too I think and adds a * to the mean that is larger – to make the table easier to scan.
Dear Dr Jason,
The above was an ascii text table. The following two are graphical implementations using plotly and matplotlib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #This is the graphical implementation of the table using plotly and matplotlib #column names and labels calculated earlier. column_names=["model pairs","model1","mean1","std","model2","mean2","std","sig/not sig"] columns=[models,model1,mean1,std1,model2,mean2,std2,sig] #Graphical implementation using plotly importplotly.graph_objectsasgo fig=go.Figure(data=[go.Table(header=dict(values=column_names),cells=dict(values=columns))]) fig.show() #Graphical implementation using matplotlib frommatplotlibimportpyplot fig=pyplot.figure() ax=fig.add_subplot(111) ax.axis('off') the_table=ax.table(cellText=array(columns).T,colLabels=column_names,loc='center') pyplot.show() |
Thank you,
Anthony of Sydney
Dear Dr Jason,
You mentioned “….Weka does this too I think and adds a * to the mean that is larger – to make the table easier to scan.>
I spent an extra two minutes to modify the code in python.
1 2 3 4 5 6 7 8 | #mean1 and mean2 are numeric that is converted to a string. #asterisk is added to string whether mean1 > mean2 and vice versa temp_mean1="%.3f"%mean1 temp_mean2="%.3f"%mean2 ifmean1>mean2: temp_mean1=temp_mean1+"*" else: temp_mean2=temp_mean2+"*" |
Here is the result:
1 2 3 4 5 6 7 8 9 10 11 12 | +--------------+--------+--------+-------+--------+--------+-------+-------------+ |modelpairs |model1|mean1 | std |model2|mean2 | std |sig/notsig| +--------------+--------+--------+-------+--------+--------+-------+-------------+ | lr&cart | lr |0.892*|0.034| cart |0.831 |0.039| sig | | lr&svm | lr |0.892 |0.034| svm |0.952*|0.021| sig | | knn&cart | knn |0.942*|0.022| cart |0.831 |0.039| sig | | knn&svm | knn |0.942 |0.022| svm |0.952*|0.021| sig | | cart&svm | cart |0.832 |0.036| svm |0.952*|0.021| sig | | cart&lda | cart |0.830 |0.036| lda |0.894*|0.031| sig | |svm&bayes | svm |0.952*|0.021|bayes |0.866 |0.039| sig | | svm&lda | svm |0.952*|0.021| lda |0.894 |0.031| sig | +--------------+--------+--------+-------+--------+--------+-------+-------------+ |
Thank you,
Anthony of Sydney
Dear Dr Jason,
The table above used the package prettytable.
Unfortunately you cannot add a header using prettytable package.
If you want to add a header like the table below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | +--------------------------------------------------------------------------------+ | Pairwisecomparisonofscoresformodels | +--------------+--------+--------+-------+--------+--------+-------+-------------+ |modelpairs |model1|mean1 | std |model2|mean2 | std |sig/notsig| +--------------+--------+--------+-------+--------+--------+-------+-------------+ | lr&cart | lr |0.892*|0.034| cart |0.834 |0.037| sig | | lr&svm | lr |0.892 |0.034| svm |0.952*|0.021| sig | | knn&cart | knn |0.942*|0.022| cart |0.832 |0.034| sig | | knn&svm | knn |0.942 |0.022| svm |0.952*|0.021| sig | | cart&svm | cart |0.830 |0.035| svm |0.952*|0.021| sig | | cart&lda | cart |0.831 |0.032| lda |0.894*|0.031| sig | |svm&bayes | svm |0.952*|0.021|bayes |0.866 |0.039| sig | | svm&lda | svm |0.952*|0.021| lda |0.894 |0.031| sig | +--------------+--------+--------+-------+--------+--------+-------+-------------+ |
Use the pytable package. First you uninstall prettytable then install pytable.
1 2 3 4 5 | reminyourdoswindow pipuninstallprettytable pipuninstallptable--upgrade |
In your python program you import the ptable package as importing prettytable.
In this example, you add another line
1 | x.title="Pairwise comparison of scores for models" |
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 | fromprettytableimportPrettyTable x=PrettyTable() column_names=["model pairs","model1","mean1","std","model2","mean","std","sig/not sig"]x.add_column(column_names[0],models_values) x.title="Pairwise comparison of scores for models " x.add_column(column_names[1],model_values) x.add_column(column_names[2],mean1_values) x.add_column(column_names[3],std1_values) x.add_column(column_names[4],model_values) x.add_column(column_names[5],mean2_values) x.add_column(column_names[6],std2_values) x.add_column(column_names[7],sig_values) print(x) |
Thank you,
Anthony of Sydney
Dear Dr Jason,
From my enhancements to your tutorial on comparing the scores of models, I have shown how one can make a table of the significant relationships between one model versus another model.
This tutorial showed the boxplots of the scores when comparing models.
Without showing the code in its entirety, I go to the essentials of plotting data of boxplots using matplotlib, matplotlib and seaborn which uses matplotlib. Note I did not accidentally write matplotlib twice. There are two methods.
I will relate this to the tutorial.
It is assumed that the packages have been declared at the top of the program.
This is presented as a ‘conceptual’ method but without the accoutrements
First matplotlib where subplots is instantiated with number of rows and number of columns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #This is matplotlib using rows and cols method fig,ax=pyplot.subplots(vert,horiz) fig.suptitle("Pairwise comparison of scores for models",fontsize=14,fontweight='bold') fig.tight_layout() index_limit=len(datafromanarraycontainingmodel1andmodel2) counter=0;#Use this to access an array containing model1 and model2 data. and other arrays such as whether the relationship between the two models is sig or not sig foriinrange(vert): forjinrange(horiz): ............ ............ model1_scores=datafromanarraycontainingmodel1andmodel2[counter];#conceptual model2_scores=datafromanarraycontainingmodel1andmodel2[counter];#conceptual ax[i,j].boxplot([model1_scores,model2_scores],showmeans=True) ......... .......... counter+=1;# for use in accessing other arrays su ifcounter ==index_limit: ax[i,j+1].set_axis_off();#ensure that you don't have an empty graph break |
This uses matplotlib : compare the difference between instantiation of subplots in this and the previous example
1 2 3 4 5 6 7 8 9 10 11 12 13 | fig=pyplot.figure() fig.subplots_adjust(hspace=0.7,wspace=0.3) fig.tight_layout() fig.suptitle("Pairwise comparison of scores for models",fontsize=14,fontweight='bold') counter=0;#Use this to access an array containing model1 and model2 data. and other arrays such as whether the relationship between the two models is sig or not sig foritemindatafromanarraycontainingmodel1andmodel2: ax=fig.add_subplot(int(vert),int(horiz),int(item[0]+1)) model1_scores=datafromanarraycontainingmodel1andmodel2[counter];#conceptual model2_scores=datafromanarraycontainingmodel1andmodel2[counter];#conceptual ax.boxplot([model1_scores,model2_scores],showmeans=True) counter+=1;#the counter in this instance may be used to get other arrays associated with model1 and model2 |
This example uses seaborn and matplotlib.
A boxplot in seaborn requires (i) a DataFrame, and (ii) restructing the two variables model1 and model2 into one array The boxplot in seaborn does IS NOT
1 2 3 4 5 | importseabornassns #This shows difference between matplotlib's boxplot using variables model1 and model2 pyplot.boxplot([model1,model2],showmeans=True) #This won't display properly - you'll get len(model1) boxplots! sns.boxplot([model1,model2],showmeans=True) |
What is required in seaborn’s boxplot is to have two variables, the categorical variable identifying model1 and model2, and another array consisting of the stacking of model1 and models’ values.
The generation of the separate categorical and values arrays are performed automatically using pandas’ melt and DataFrame function.
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 | importseabornassns frompandasimportDataFrame frompandasimportmelt #This is matplotlib using rows and cols method fig,ax=pyplot.subplots(vert,horiz) fig.suptitle("Pairwise comparison of scores for models",fontsize=14,fontweight='bold') fig.tight_layout() index_limit=len(datafromanarraycontainingmodel1andmodel2) counter=0;#Use this to access an array containing model1 and model2 data. foriinrange(vert): forjinrange(horiz): ............ ............ model1_scores=datafromanarraycontainingmodel1andmodel2[counter];#conceptual model2_scores=datafromanarraycontainingmodel1andmodel2[counter];#conceptual labels=(str(type(list_of_models[0]).__name__),str(type(list_of_models[1]).__name__)) model1_scores=datafromanarraycontainingmodel1andmodel2 model1_scores=datafromanarraycontainingmodel1andmodel2 #sns requires all data to be in a DataFrame temp_array=array([x1,x2]).T # The column names will show the paritcular model pair's names, eg LDA and SVC tempdf=DataFrame(temp_array,columns=array(labels)) # This next procedure generates two columns # one column containing the hidden array of labels and the other model1 and model2 tempdf=melt(tempdf) # By DEFAULT tempdf has two columns of data, named 'variable' and 'value' # Let's rename the labels, 'models' for the x axis and 'scores' for the y axis templabel=['models','scores'] tempdf.columns=templabel ax[i,j].set_ylabel("scores",fontsize=8) # I choose not to display xlabel as the tick labels are self-explanatory ax[i,j].set_xlabel("") sns.boxplot(x=tempdf.columns[0],y=tempdf.columns[1],data=tempdf,width=0.2,showmeans=True,color='white',ax=ax[i,j]) ......... .......... counter+=1 ifcounter ==index_limit: ax[i,j+1].set_axis_off();#ensure that you don't have an empty graph break |
A bonus.
You can use a DataFrame in conjunction with the melt method to generate an array of categorical variable associated with the other ‘array’ of data.
The categorical variable’s is derived when intializing the DataFrame.
1 2 3 4 5 6 | df=DataFrame([model1_scores,model2_scores],columns=labels,.....) #df has the labels for the columns df=melt(df);# df generates one column of categories based on the value of labels (of the columns) df.columns=['category','scores'] category=df.values[:,0] values=df.values[:,1] |
Thank you,
Anthony of Sydney
Hi Jason,
This is a very interesting post which will help me with my PhD work a lot so thank you.
I am just wondering what your advice would be if you wanted to test multiple machine learning algorithms on multiple data sets.
I was thinking I could test each data set using cross-validation and then get a table of results for each machine learning algorithm to conduct a hypothesis test. An example below shows a table presenting the standardised RMSE for various machine learning algorithms:
Dataset | GP1 | GP2 | ANN | Linear Regression
Ishigami | 0.21 | 0.16 | 0.19 | 0.32
Sobol | blah | blah | blah | blah
….
….
and so on.
So in this case. Would there be a recommended hypothesis test to compare the regression techniques? Any literature you would recommend to investigate this further? And what are your thoughts on this sort of analysis?
My issue is that most of the literature I am finding is comparing two machine learning techniques as the best for just one dataset. Whereas I am wanting to find an overall better technique for numerous datasets.
Thanks,
Aaron
Hi Aaron, I hope you’ll find your answer till now, however I would share a really great Book that essentially is in the depth of this field, that is : Evaluating Learning Algorithms A Classification Perspective By “NATHALIE JAPKOWICZ, University of Ottawa” and “MOHAK SHAH, McGill University”.
I hope this reference help you and others out with these problems.
Perhaps the references in this tutorial will help:
https://machinelearningmastery.com/statistical-power-and-power-analysis-in-python/
Hi Jason,
Thanks for the article! It is indeed very helpful!
May I ask what method would you suggest when using large datasets? Cross-validation would be very time-consuming, so maybe there is something else out there that I could consider?
Thanks,
Laura
For very large datasets, perhaps a train/test split and a mcnemars test:
https://machinelearningmastery.com/mcnemars-test-for-machine-learning/
when I make this test, should I do preprocessing the data before it, or not necessary
if i compare ML and DL classifiers, cross val score in DL how is wittren?
Perhaps you can use a standard test harness like cross-validation:
https://machinelearningmastery.com/repeated-k-fold-cross-validation-with-python/
Hi, I came across this paper and it seems to be very close to your article. I don’t know if it was plagiarised but wanted to bring it to your notice.
https://www.spu.edu.iq/kjar/index.php/kjar/article/view/630/333
Hello Jason. Just 1 quick question. Why do you define cv and cv2? Doesn’t this mean that the 2 models will be trained and evaluated in different splits of data? Would it be wrong if you just used 1 cv?
Hello Jason, Thank you for your great post. I have a question for comparing two deep learning models such as u-net and attention u-net. Is it possible to fix the dataset (add 10% of data for test and the rest for training), and train the two models using a set of the same hyperparameters on the fix dataset. Then, using hypothesis testing on the obtained results?
Hi Carol…You are very welcome! Certainly your understanding is correct! You may also want to try the various models on new data and compare the root mean square error of each.
https://machinelearningmastery.com/regression-metrics-for-machine-learning/
Other ideas are presented here:
https://machinelearningmastery.com/evaluate-performance-deep-learning-models-keras/
Welcome!
I'mJason Brownlee PhD
and Ihelp developers get results withmachine learning.
Read more
TheStatistics for Machine Learning EBook is
where you'll find theReally Good stuff.