如何将cross_val_score与random_state一起使用 [英] How to use cross_val_score with random_state

查看:533
本文介绍了如何将cross_val_score与random_state一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于不同的运行,我得到了不同的值...我在这里做错了什么

I'm getting different values for different runs ... what am I doing wrong here:

X=np.random.random((100,5))
y=np.random.randint(0,2,(100,))
clf=RandomForestClassifier()
cv = StratifiedKFold(y, random_state=1)
s = cross_val_score(clf, X,y,scoring='roc_auc', cv=cv)
print(s)
# [ 0.42321429  0.44360902  0.34398496]
s = cross_val_score(clf, X,y,scoring='roc_auc', cv=cv)
print(s)
# [ 0.42678571  0.46804511  0.36090226]

推荐答案

您所犯的错误是调用np.random生成的种子以产生随机输出.

The mistake you are making is calling the RandomForestClassifier whose default arg, random_state is None. So, it picks up the seed generated by np.random to produce the random output.

两个中的random_state StratifiedKFold RandomForestClassifier必须相同,才能产生相等数量的交叉验证分数数组.

The random_state in both StratifiedKFold and RandomForestClassifier need to be the same inorder to produce equal arrays of scores of cross validation.

插图:

X=np.random.random((100,5))
y=np.random.randint(0,2,(100,))

clf = RandomForestClassifier(random_state=1)
cv = StratifiedKFold(y, random_state=1)        # Setting random_state is not necessary here
s = cross_val_score(clf, X,y,scoring='roc_auc', cv=cv)
print(s)
##[ 0.57612457  0.29044118  0.30514706]
print(s)
##[ 0.57612457  0.29044118  0.30514706]

另一种解决方法是不同时为RFC和SKF提供random_state args.但是,只需提供np.random.seed(value)即可在开始处创建随机整数.这些还会在输出处创建相等的数组.

Another way of countering it would be to not provide random_state args for both RFC and SKF. But, simply providing the np.random.seed(value) to create the random integers at the beginning. These would also create equal arrays at the output.

这篇关于如何将cross_val_score与random_state一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆