2011-01-12 11:40:16 +08:00
|
|
|
"""
|
|
|
|
|
=================================================================
|
|
|
|
|
Test with permutations the significance of a classification score
|
|
|
|
|
=================================================================
|
|
|
|
|
|
|
|
|
|
In order to test if a classification score is significative a technique
|
|
|
|
|
in repeating the classification procedure after randomizing, permuting,
|
|
|
|
|
the labels. The p-value is then given by the percentage of runs for
|
|
|
|
|
which the score obtained is greater than the classification score
|
|
|
|
|
obtained in the first place.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2011-01-12 11:40:16 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2011-01-12 11:40:16 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-01-12 11:40:16 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.svm import SVC
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import StratifiedKFold
|
|
|
|
|
from sklearn.model_selection import permutation_test_score
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import datasets
|
2011-01-12 11:40:16 +08:00
|
|
|
|
|
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-01-12 11:40:16 +08:00
|
|
|
# Loading a dataset
|
|
|
|
|
iris = datasets.load_iris()
|
|
|
|
|
X = iris.data
|
|
|
|
|
y = iris.target
|
|
|
|
|
n_classes = np.unique(y).size
|
|
|
|
|
|
|
|
|
|
# Some noisy data not correlated
|
|
|
|
|
random = np.random.RandomState(seed=0)
|
|
|
|
|
E = random.normal(size=(len(X), 2200))
|
|
|
|
|
|
|
|
|
|
# Add noisy data to the informative features for make the task harder
|
|
|
|
|
X = np.c_[X, E]
|
|
|
|
|
|
|
|
|
|
svm = SVC(kernel='linear')
|
2015-09-11 02:26:39 +08:00
|
|
|
cv = StratifiedKFold(2)
|
2011-01-12 11:40:16 +08:00
|
|
|
|
2012-12-25 20:16:05 +08:00
|
|
|
score, permutation_scores, pvalue = permutation_test_score(
|
2012-12-20 04:44:34 +08:00
|
|
|
svm, X, y, scoring="accuracy", cv=cv, n_permutations=100, n_jobs=1)
|
2011-01-12 11:40:16 +08:00
|
|
|
|
2013-02-01 22:04:03 +08:00
|
|
|
print("Classification score %s (pvalue : %s)" % (score, pvalue))
|
2011-01-12 11:40:16 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-01-12 11:40:16 +08:00
|
|
|
# View histogram of permutation scores
|
2017-06-07 19:23:12 +08:00
|
|
|
plt.hist(permutation_scores, 20, label='Permutation scores',
|
|
|
|
|
edgecolor='black')
|
2014-05-15 04:31:03 +08:00
|
|
|
ylim = plt.ylim()
|
2011-12-20 18:03:41 +08:00
|
|
|
# BUG: vlines(..., linestyle='--') fails on older versions of matplotlib
|
2017-06-07 19:23:12 +08:00
|
|
|
# plt.vlines(score, ylim[0], ylim[1], linestyle='--',
|
2011-12-20 18:03:41 +08:00
|
|
|
# color='g', linewidth=3, label='Classification Score'
|
|
|
|
|
# ' (pvalue %s)' % pvalue)
|
2017-06-07 19:23:12 +08:00
|
|
|
# plt.vlines(1.0 / n_classes, ylim[0], ylim[1], linestyle='--',
|
2011-12-20 18:03:41 +08:00
|
|
|
# color='k', linewidth=3, label='Luck')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot(2 * [score], ylim, '--g', linewidth=3,
|
2014-05-15 10:35:13 +08:00
|
|
|
label='Classification Score'
|
|
|
|
|
' (pvalue %s)' % pvalue)
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot(2 * [1. / n_classes], ylim, '--k', linewidth=3, label='Luck')
|
2011-12-20 18:03:41 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.ylim(ylim)
|
|
|
|
|
plt.legend()
|
|
|
|
|
plt.xlabel('Score')
|
|
|
|
|
plt.show()
|