在 scikit-learn 中缩放 permutation_test_score

Scaling in scikit-learn permutation_test_score

我正在使用 scikit-learn "permutation_test_score" 方法来评估我的估算器性能的重要性。不幸的是,我无法从 scikit-learn 文档中了解到该方法是否对数据进行了任何缩放。我使用 StandardScaler 标准化我的数据,将训练集标准化应用于测试集。

函数本身不应用任何缩放。

这是文档中的示例:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import permutation_test_score
from sklearn import datasets


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')
cv = StratifiedKFold(2)

score, permutation_scores, pvalue = permutation_test_score(
    svm, X, y, scoring="accuracy", cv=cv, n_permutations=100, n_jobs=1)

但是,您可能想要在应用缩放的地方传入 permutation_test_score 一个 pipeline

示例:

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

pipe = Pipeline([('scaler', StandardScaler()), ('clf', SVC(kernel='linear'))])
score, permutation_scores, pvalue = permutation_test_score(
        pipe, X, y, scoring="accuracy", cv=cv, n_permutations=100, n_jobs=1)