Sklearn cross_val_score给出的数字与model.score明显不同?

问题描述 投票:1回答:1

我有一个二进制分类问题

首先,我训练测试将数据拆分为:

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

我检查了y_train,它基本上将两个类(1,0)的比例分为50/50,这就是它的数据集的方式

当我尝试诸如以下的基本模型时:

model = RandomForestClassifier()
model.fit(X_train, y_train)
model.score(X_train, y_train)

输出为0.98或1%的差异,具体取决于列车测试拆分的随机状态。

但是,当我尝试使用cross_val_score时,例如:

cross_val_score(model, X_train, y_train, cv=StratifiedKFold(shuffle=True), scoring='accuracy')

输出为

array([0.65      , 0.78333333, 0.78333333, 0.66666667, 0.76666667])

数组中的分数都没有接近0.98吗?

[当我尝试得分='r2'时得到

>>>cross_val_score(model, X_train, y_train, cv=StratifiedKFold(shuffle=True), scoring='r2')
array([-0.20133482, -0.00111235, -0.2       , -0.2       , -0.13333333])

有人知道为什么会这样吗?我尝试过Shuffle = TrueFalse,但没有帮助。

提前感谢

python machine-learning scikit-learn classification cross-validation
1个回答
2
投票

在基本模型中,您将根据训练语料库计算分数。尽管这是确保模型实际上已从您提供的数据中学到的正确方法,但不能保证模型在新的和看不见的数据上的最终准确性。

© www.soinside.com 2019 - 2024. All rights reserved.