|
3 | 3 | Test with permutations the significance of a classification score
|
4 | 4 | =================================================================
|
5 | 5 |
|
6 |
| -In order to test if a classification score is significative a technique |
7 |
| -in repeating the classification procedure after randomizing, permuting, |
8 |
| -the labels. The p-value is then given by the percentage of runs for |
9 |
| -which the score obtained is greater than the classification score |
10 |
| -obtained in the first place. |
11 |
| -
|
| 6 | +This example demonstrates the use of |
| 7 | +:func:`~sklearn.model_selection.permutation_test_score` to evaluate the |
| 8 | +significance of a cross-valdiated score using permutations. |
12 | 9 | """
|
13 | 10 |
|
14 |
| -# Author: Alexandre Gramfort <[email protected]> |
| 11 | +# Authors: Alexandre Gramfort <[email protected]> |
| 12 | +# Lucy Liu |
15 | 13 | # License: BSD 3 clause
|
| 14 | +# |
| 15 | +# Dataset |
| 16 | +# ------- |
| 17 | +# |
| 18 | +# We will use the :ref:`iris_dataset`, which consists of measurements taken |
| 19 | +# from 3 types of irises. |
| 20 | + |
| 21 | +from sklearn.datasets import load_iris |
| 22 | + |
| 23 | +iris = load_iris() |
| 24 | +X = iris.data |
| 25 | +y = iris.target |
16 | 26 |
|
17 |
| -print(__doc__) |
| 27 | +# %% |
| 28 | +# We will also generate some random feature data (i.e., 2200 features), |
| 29 | +# uncorrelated with the class labels in the iris dataset. |
18 | 30 |
|
19 | 31 | import numpy as np
|
20 |
| -import matplotlib.pyplot as plt |
| 32 | + |
| 33 | +n_uncorrelated_features = 2200 |
| 34 | +rng = np.random.RandomState(seed=0) |
| 35 | +# Use same number of samples as in iris and 2200 features |
| 36 | +X_rand = rng.normal(size=(X.shape[0], n_uncorrelated_features)) |
| 37 | + |
| 38 | +# %% |
| 39 | +# Permutation test score |
| 40 | +# ---------------------- |
| 41 | +# |
| 42 | +# Next, we calculate the |
| 43 | +# :func:`~sklearn.model_selection.permutation_test_score` using the original |
| 44 | +# iris dataset, which strongly predict the labels and |
| 45 | +# the randomly generated features and iris labels, which should have |
| 46 | +# no dependency between features and labels. We use the |
| 47 | +# :class:`~sklearn.svm.SVC` classifier and :ref:`accuracy_score` to evaluate |
| 48 | +# the model at each round. |
| 49 | +# |
| 50 | +# :func:`~sklearn.model_selection.permutation_test_score` generates a null |
| 51 | +# distribution by calculating the accuracy of the classifier |
| 52 | +# on 1000 different permutations of the dataset, where features |
| 53 | +# remain the same but labels undergo different permutations. This is the |
| 54 | +# distribution for the null hypothesis which states there is no dependency |
| 55 | +# between the features and labels. An empirical p-value is then calculated as |
| 56 | +# the percentage of permutations for which the score obtained is greater |
| 57 | +# that the score obtained using the original data. |
21 | 58 |
|
22 | 59 | from sklearn.svm import SVC
|
23 | 60 | from sklearn.model_selection import StratifiedKFold
|
24 | 61 | from sklearn.model_selection import permutation_test_score
|
25 |
| -from sklearn import datasets |
26 | 62 |
|
| 63 | +clf = SVC(kernel='linear', random_state=7) |
| 64 | +cv = StratifiedKFold(2, shuffle=True, random_state=0) |
27 | 65 |
|
28 |
| -# ############################################################################# |
29 |
| -# Loading a dataset |
30 |
| -iris = datasets.load_iris() |
31 |
| -X = iris.data |
32 |
| -y = iris.target |
33 |
| -n_classes = np.unique(y).size |
34 |
| - |
35 |
| -# Some noisy data not correlated |
36 |
| -random = np.random.RandomState(seed=0) |
37 |
| -E = random.normal(size=(len(X), 2200)) |
38 |
| - |
39 |
| -# Add noisy data to the informative features for make the task harder |
40 |
| -X = np.c_[X, E] |
41 |
| - |
42 |
| -svm = SVC(kernel='linear') |
43 |
| -cv = StratifiedKFold(2) |
44 |
| - |
45 |
| -score, permutation_scores, pvalue = permutation_test_score( |
46 |
| - svm, X, y, scoring="accuracy", cv=cv, n_permutations=100, n_jobs=1) |
47 |
| - |
48 |
| -print("Classification score %s (pvalue : %s)" % (score, pvalue)) |
49 |
| - |
50 |
| -# ############################################################################# |
51 |
| -# View histogram of permutation scores |
52 |
| -plt.hist(permutation_scores, 20, label='Permutation scores', |
53 |
| - edgecolor='black') |
54 |
| -ylim = plt.ylim() |
55 |
| -# BUG: vlines(..., linestyle='--') fails on older versions of matplotlib |
56 |
| -# plt.vlines(score, ylim[0], ylim[1], linestyle='--', |
57 |
| -# color='g', linewidth=3, label='Classification Score' |
58 |
| -# ' (pvalue %s)' % pvalue) |
59 |
| -# plt.vlines(1.0 / n_classes, ylim[0], ylim[1], linestyle='--', |
60 |
| -# color='k', linewidth=3, label='Luck') |
61 |
| -plt.plot(2 * [score], ylim, '--g', linewidth=3, |
62 |
| - label='Classification Score' |
63 |
| - ' (pvalue %s)' % pvalue) |
64 |
| -plt.plot(2 * [1. / n_classes], ylim, '--k', linewidth=3, label='Luck') |
65 |
| - |
66 |
| -plt.ylim(ylim) |
67 |
| -plt.legend() |
68 |
| -plt.xlabel('Score') |
| 66 | +score_iris, perm_scores_iris, pvalue_iris = permutation_test_score( |
| 67 | + clf, X, y, scoring="accuracy", cv=cv, n_permutations=1000) |
| 68 | + |
| 69 | +score_rand, perm_scores_rand, pvalue_rand = permutation_test_score( |
| 70 | + clf, X_rand, y, scoring="accuracy", cv=cv, n_permutations=1000) |
| 71 | + |
| 72 | +# %% |
| 73 | +# Original data |
| 74 | +# ^^^^^^^^^^^^^ |
| 75 | +# |
| 76 | +# Below we plot a histogram of the permutation scores (the null |
| 77 | +# distribution). The red line indicates the score obtained by the classifier |
| 78 | +# on the original data. The score is much better than those obtained by |
| 79 | +# using permuted data and the p-value is thus very low. This indicates that |
| 80 | +# there is a low likelihood that this good score would be obtained by chance |
| 81 | +# alone. It provides evidence that the iris dataset contains real dependency |
| 82 | +# between features and labels and the classifier was able to utilize this |
| 83 | +# to obtain good results. |
| 84 | + |
| 85 | +import matplotlib.pyplot as plt |
| 86 | + |
| 87 | +fig, ax = plt.subplots() |
| 88 | + |
| 89 | +ax.hist(perm_scores_iris, bins=20, density=True) |
| 90 | +ax.axvline(score_iris, ls='--', color='r') |
| 91 | +score_label = (f"Score on original\ndata: {score_iris:.2f}\n" |
| 92 | + f"(p-value: {pvalue_iris:.3f})") |
| 93 | +ax.text(0.7, 260, score_label, fontsize=12) |
| 94 | +ax.set_xlabel("Accuracy score") |
| 95 | +_ = ax.set_ylabel("Probability") |
| 96 | + |
| 97 | +# %% |
| 98 | +# Random data |
| 99 | +# ^^^^^^^^^^^ |
| 100 | +# |
| 101 | +# Below we plot the null distribution for the randomized data. The permutation |
| 102 | +# scores are similar to those obtained using the original iris dataset |
| 103 | +# because the permutation always destroys any feature label dependency present. |
| 104 | +# The score obtained on the original randomized data in this case though, is |
| 105 | +# very poor. This results in a large p-value, confirming that there was no |
| 106 | +# feature label dependency in the original data. |
| 107 | + |
| 108 | +fig, ax = plt.subplots() |
| 109 | + |
| 110 | +ax.hist(perm_scores_rand, bins=20, density=True) |
| 111 | +ax.set_xlim(0.13) |
| 112 | +ax.axvline(score_rand, ls='--', color='r') |
| 113 | +score_label = (f"Score on original\ndata: {score_rand:.2f}\n" |
| 114 | + f"(p-value: {pvalue_rand:.3f})") |
| 115 | +ax.text(0.14, 125, score_label, fontsize=12) |
| 116 | +ax.set_xlabel("Accuracy score") |
| 117 | +ax.set_ylabel("Probability") |
69 | 118 | plt.show()
|
| 119 | + |
| 120 | +# %% |
| 121 | +# Another possible reason for obtaining a high p-value is that the classifier |
| 122 | +# was not able to use the structure in the data. In this case, the p-value |
| 123 | +# would only be low for classifiers that are able to utilize the dependency |
| 124 | +# present. In our case above, where the data is random, all classifiers would |
| 125 | +# have a high p-value as there is no structure present in the data. |
| 126 | +# |
| 127 | +# Finally, note that this test has been shown to produce low p-values even |
| 128 | +# if there is only weak structure in the data [1]_. |
| 129 | +# |
| 130 | +# .. topic:: References: |
| 131 | +# |
| 132 | +# .. [1] Ojala and Garriga. `Permutation Tests for Studying Classifier |
| 133 | +# Performance |
| 134 | +# <http://www.jmlr.org/papers/volume11/ojala10a/ojala10a.pdf>`_. The |
| 135 | +# Journal of Machine Learning Research (2010) vol. 11 |
| 136 | +# |
0 commit comments