sklearn cross_val_score如何使用kfold? [英] How does sklearn cross_val_score use kfold?
问题描述
我是机器学习的新手,并试图理解 cross_val_score
使用Kfold将数据拆分为k折。
I am new to machine learning and am trying to understand cross_val_score
uses Kfold to split the data to k folds.
kf = KFold(n_splits=2)
cv_results =cross_val_score(model, X_train, Y_train, cv=kf)
我知道 kfold
会拆分数据,但我尝试将其打印出来
I know kfold
splits the data but I tried printing it out
dataset = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6],[7,7,7],[8,8,8]]
kf = KFold(n_splits=2)
print kf
>>> KFold(n_splits=2, random_state=None, shuffle=False)
它不显示k折叠,但是 cross_val_score
如何得到所有折叠?
It doesn't show the k folds but then how does cross_val_score
get all the folds?
推荐答案
您需要调用 Kf.split(dataset)
来实际拆分数据。 点击此处查看KFold的工作方式
You need to call Kf.split(dataset)
to actually split the data. Click here to see how KFold works
为了清楚起见, KFold
是类而不是函数。
Just to make it clear, KFold
is a class and not a function.
kf = KFold(n_splits = 2)
创建一个KFold对象。
和 print kf
只会打印出类对象。
kf = KFold(n_splits=2)
creates an object of KFold.
and print kf
will just print out the class object.
当您调用 cross_val_score(model,X_train,Y_train,cv = kf)
您将对象 kf
传递给cross_val_score函数,其中 kf.split(X_train)
会被调用以将 X_train
分成两部分。 Y_train
也将被类似地拆分。
and when you callcross_val_score(model, X_train, Y_train, cv=kf)
you are passing the object kf
to cross_val_score function where kf.split(X_train)
would be called to split X_train
into 2 folds. Y_train
would also be splitted similarly.
这篇关于sklearn cross_val_score如何使用kfold?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!