sklearn学习6----交叉验证

1、kfold:自己分样本来交叉验证迭代

  • 导入模块:from sklearn.model_selection import KFold

  • 参数:

KFold(n_splits=3, shuffle=False, random_state=None)
'''
n_splits : int, default=3
Number of folds. Must be at least 2.
shuffle : boolean, optional
Whether to shuffle the data before splitting into batches.
random_state : int, RandomState instance or None, optional, default=None
If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Used when shuffle == True.
'''

n_splits:就是将样本分成多少份。进行k折验证

shuffle:是否在分割成批次之前将数据洗牌。

random_state:如果INT,随机状态是随机数生成器所使用的种子;如果是随机状态实例,随机数是随机数生成器;如果没有,随机数生成器是NP-随机使用的随机状态实例。当洗牌= =真时使用。

  • 代码示例

from sklearn.model_selection import KFold
kf = KFold(n_splits=5,shuffle=False) 
c_range= [0.01,0.1,1,10,100]
for C in c_range:
    for train,test in kf.split(X):
        lr = LogisticRegression(C = C, penalty = 'l1')
        lr.fit(X.iloc[train,:],Y.iloc[train,:].values.ravel())
        y_pred = lr.predict(X.iloc[test,:].values)

 2、【交叉验证度量】直接交叉验证cross_val_score

  

原文地址:https://www.cnblogs.com/Lee-yl/p/9140066.html