分类的排列检验分数

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在机器学习中,我们经常使用一个分数来评估分类模型的性能。然而,我们还需要检验这个分数的显著性,以确保模型性能不只是偶然因素导致的。这就是排列检验分数(permutation test score)发挥作用的地方。它通过计算分类器在数据集的 1000 种不同排列上的准确率来生成一个零分布。然后,将获得的分数大于使用原始数据获得的分数的排列所占的百分比计算为经验 p 值。在这个实验中,我们将使用 sklearn.model_selection 中的 permutation_test_score 函数,通过排列来评估交叉验证分数的显著性。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向 Labby 提问。课程结束后提供反馈,我们会及时为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/UtilitiesandDatasetsGroup(["Utilities and Datasets"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/svm("Support Vector Machines") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/model_selection("Model Selection") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/datasets("Datasets") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/svm -.-> lab-49246{{"分类的排列检验分数"}} sklearn/model_selection -.-> lab-49246{{"分类的排列检验分数"}} sklearn/datasets -.-> lab-49246{{"分类的排列检验分数"}} ml/sklearn -.-> lab-49246{{"分类的排列检验分数"}} end

加载数据集并生成随机特征

我们将使用鸢尾花数据集,该数据集包含从 3 种鸢尾花中获取的测量数据,并生成一些与鸢尾花数据集中的类别标签不相关的随机特征数据(即 20 个特征)。

from sklearn.datasets import load_iris
import numpy as np

iris = load_iris()
X = iris.data
y = iris.target

n_uncorrelated_features = 20
rng = np.random.RandomState(seed=0)
X_rand = rng.normal(size=(X.shape[0], n_uncorrelated_features))

对原始数据进行排列检验分数计算

接下来,我们使用原始的鸢尾花数据集和带有“accuracy”分数的“SVC”分类器来计算“permutation_test_score”,以便在每一轮评估模型。

from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import permutation_test_score

clf = SVC(kernel="linear", random_state=7)
cv = StratifiedKFold(2, shuffle=True, random_state=0)

score_iris, perm_scores_iris, pvalue_iris = permutation_test_score(
    clf, X, y, scoring="accuracy", cv=cv, n_permutations=1000
)

对随机数据进行排列检验分数计算

接下来,我们使用随机生成的特征和鸢尾花标签来计算“permutation_test_score”,这些特征和标签之间应该不存在依赖关系。

score_rand, perm_scores_rand, pvalue_rand = permutation_test_score(
    clf, X_rand, y, scoring="accuracy", cv=cv, n_permutations=1000
)

绘制结果

我们绘制了原始鸢尾花数据集和随机化数据的排列分数(零分布)的直方图。我们还用红线表示分类器在原始数据上获得的分数。每个图上都显示了 p 值。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

## 原始数据
ax.hist(perm_scores_iris, bins=20, density=True)
ax.axvline(score_iris, ls="--", color="r")
score_label = f"Score on original\ndata: {score_iris:.2f}\n(p-value: {pvalue_iris:.3f})"
ax.text(0.7, 10, score_label, fontsize=12)
ax.set_xlabel("Accuracy score")
_ = ax.set_ylabel("Probability density")

plt.show()

fig, ax = plt.subplots()

## 随机数据
ax.hist(perm_scores_rand, bins=20, density=True)
ax.set_xlim(0.13)
ax.axvline(score_rand, ls="--", color="r")
score_label = f"Score on original\ndata: {score_rand:.2f}\n(p-value: {pvalue_rand:.3f})"
ax.text(0.14, 7.5, score_label, fontsize=12)
ax.set_xlabel("Accuracy score")
ax.set_ylabel("Probability density")

plt.show()

总结

在本实验中,我们学习了如何使用sklearn.model_selection中的permutation_test_score函数,通过排列来评估交叉验证分数的显著性。我们通过计算分类器在数据集的 1000 种不同排列上的准确率生成了一个零分布,并将获得的分数大于使用原始数据获得的分数的排列百分比计算为经验 p 值。我们还绘制了结果,以可视化零分布和在原始数据上获得的分数。