作为 How to compose sklearn estimators using another estimator? 的后续行动, 我正在尝试交叉验证一堆模型。
手册
首先,我手动执行所有步骤以确保一切正常:
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import roc_auc_score
X, y = make_classification(n_samples=10000, n_features=40,
n_clusters_per_class=10,
n_informative=25,
random_state=12, shuffle=False)
logit = LogisticRegression(solver="saga",random_state=12).fit(X,y)
logit_yhat = logit.predict_proba(X)[:,1]
print("logit",roc_auc_score(y, logit_yhat))
randf = RandomForestClassifier(n_estimators=10,max_depth=5,min_samples_split=10, random_state=12).fit(X,y)
randf_yhat = randf.predict_proba(X)[:,1]
print("randf",roc_auc_score(y, randf_yhat))
gaunb = GaussianNB().fit(X,y)
gaunb_yhat = gaunb.predict_proba(X)[:,1]
print("gaunb",roc_auc_score(y, gaunb_yhat))
gbcdt = GradientBoostingClassifier(random_state=12).fit(X,y)
gbcdt_yhat = gbcdt.predict_proba(X)[:,1]
print("gbcdt",roc_auc_score(y, gbcdt_yhat))
scores = np.transpose(np.array((logit_yhat, randf_yhat, gaunb_yhat, gbcdt_yhat)))
aggregator = LogisticRegression(solver="saga",random_state=12).fit(scores, y)
aggregator_yhat = aggregator.predict_proba(scores)[:,1]
print("aggregator",aggregator.coef_,roc_auc_score(y, aggregator_yhat))
这打印:
logit 0.6913163859713081
randf 0.7871255096874669
gaunb 0.7032834038916749
gbcdt 0.8527915275109933
aggregator [[-3.95961856 5.70858186 -2.45036885 13.3983472 ]] 0.8799606190093959
到目前为止一切顺利。
使用管道
现在我创建一个管道并检查它是否执行相同的操作:
from sklearn.base import BaseEstimator, TransformerMixin, clone
class PredictProbaTransformer(BaseEstimator, TransformerMixin):
def __init__(self, clf):
self.clf = clf
def transform(self, X):
"Return predict_proba(X)."
print("transform")
return self.clf.predict_proba(X)[:,[1]]
def fit_transform(self, X, y=None, **fit_params):
print("fit_transform")
return self.clf.fit(X, y, **fit_params).predict_proba(X)[:,[1]]
from sklearn.pipeline import Pipeline, FeatureUnion
pipe = Pipeline([("stack",FeatureUnion([
("logit",PredictProbaTransformer(clone(logit))),
("randf",PredictProbaTransformer(clone(randf))),
("gaunb",PredictProbaTransformer(clone(gaunb))),
("gbcdt",PredictProbaTransformer(clone(gbcdt))),
])), ("aggregator",LogisticRegression(solver="saga",random_state=12))]).fit(X,y)
pipe_yhat = pipe.predict_proba(X)[:,1]
print("pipe",pipe.named_steps["aggregator"].coef_,roc_auc_score(y, pipe_yhat))
这打印:
pipe [[-3.95961856 5.70858186 -2.45036885 13.3983472 ]] 0.8799606190093959
与“手册”部分中的 aggregator 行相同 - 很好!
交叉验证
当我尝试对 pipe 进行交叉验证时,我感到有些奇怪:
from sklearn.model_selection import cross_validate
pipe_scores = pd.DataFrame(cross_validate(
pipe, X=X, y=y, return_train_score=True, cv=10, scoring="roc_auc"))
打印 10 次(因为 cv=10)这 12 行:
fit_transform --- 4 times
transform --- 8 times
因为它在每个训练阶段为 stack 中的 4 个分类器调用了 4 次 fit_transform),然后为每个训练阶段调用了 4 次 transform test 数据中有相同的 4 个分类器,然后是 again在训练数据上再进行 4 次(即使它已经在训练阶段完成了)。
最重要:pipe_scores.describe()是
fit_time score_time test_score train_score
count 10.000000 10.000000 10.000000 10.000000
mean 3.329516 0.006074 0.482034 0.895046
std 0.068609 0.000594 0.081499 0.006657
min 3.212703 0.005362 0.299673 0.886333
25% 3.276795 0.005602 0.451310 0.891166
50% 3.350762 0.006122 0.504630 0.892991
75% 3.370433 0.006331 0.519399 0.898570
max 3.425937 0.007302 0.586310 0.906820
奇怪的是,所有 train_score 都高于我的 88% 手动运行。
但是,为什么 test_score 看起来是完全随机的?! (平均值和中位数约为 50%,对应于“抛硬币”分类器)。
避免这种怪异现象的解决方法是通过随机排列
- 要么将
shuffle=True(而不是False)传递给make_classification - 或传递
cv=StratifiedKFold(n_splits=10,shuffle=True,random_state=12)(而不是10)到cross_validate.
那么分数是
fit_time score_time test_score train_score
count 10.000000 10.000000 10.000000 10.000000
mean 3.400829 0.005355 0.774932 0.887762
std 0.125579 0.000444 0.011324 0.003578
min 3.211147 0.004896 0.763047 0.883219
25% 3.333121 0.005074 0.767166 0.884810
50% 3.376660 0.005153 0.772864 0.886907
75% 3.484209 0.005516 0.781219 0.890338
max 3.602676 0.006194 0.799907 0.893941
附言。 随机播放 make_classification在 StratifiedKFold 中影响 列 和 行它只影响行,不影响列。 只有 row 洗牌很重要:如果我按
X = X[:, np.random.permutation(X.shape[1])]
在make_classification(... shuffle=False)之后,我得到相同的GaussianNB和 GradientBoostingClassifier和潜移默化的不同LogisticRegression和 RandomForestClassifier , 和 cross_validate返回随机 test_scores。
请您参考如下方法:
至于为什么所有 train_scores 都在 88% 以上,这是因为在交叉验证时,您在 0.9 的训练数据上进行训练。因此,您的模型可以(过度)更好地适应这些数据。至于为什么 test_score 在你不打乱特征时这么小,我相信会发生这种情况,因为在没有打乱的情况下进行交叉验证时,并非所有(10 个)集群都出现在训练数据集中(占所有集群的 0.9 data) 和数据集中一样,它们也没有被打乱。
