【问题标题】:TypeError: 'KFold' object is not iterableTypeError:“KFold”对象不可迭代
【发布时间】:2018-07-16 09:56:27
【问题描述】:

我正在关注Kaggle 上的一个内核,主要是关注A kernel for Credit Card Fraud Detection

我已经到了需要执行 KFold 以找到 Logistic 回归的最佳参数的步骤。

以下代码显示在内核本身中,但出于某种原因(可能是旧版本的 scikit-learn,给我一些错误)。

def printing_Kfold_scores(x_train_data,y_train_data):
    fold = KFold(len(y_train_data),5,shuffle=False) 

    # Different C parameters
    c_param_range = [0.01,0.1,1,10,100]

    results_table = pd.DataFrame(index = range(len(c_param_range),2), columns = ['C_parameter','Mean recall score'])
    results_table['C_parameter'] = c_param_range

    # the k-fold will give 2 lists: train_indices = indices[0], test_indices = indices[1]
    j = 0
    for c_param in c_param_range:
        print('-------------------------------------------')
        print('C parameter: ', c_param)
        print('-------------------------------------------')
        print('')

        recall_accs = []
        for iteration, indices in enumerate(fold,start=1):

            # Call the logistic regression model with a certain C parameter
            lr = LogisticRegression(C = c_param, penalty = 'l1')

            # Use the training data to fit the model. In this case, we use the portion of the fold to train the model
            # with indices[0]. We then predict on the portion assigned as the 'test cross validation' with indices[1]
            lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel())

            # Predict values using the test indices in the training data
            y_pred_undersample = lr.predict(x_train_data.iloc[indices[1],:].values)

            # Calculate the recall score and append it to a list for recall scores representing the current c_parameter
            recall_acc = recall_score(y_train_data.iloc[indices[1],:].values,y_pred_undersample)
            recall_accs.append(recall_acc)
            print('Iteration ', iteration,': recall score = ', recall_acc)

            # The mean value of those recall scores is the metric we want to save and get hold of.
        results_table.ix[j,'Mean recall score'] = np.mean(recall_accs)
        j += 1
        print('')
        print('Mean recall score ', np.mean(recall_accs))
        print('')

    best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter']

    # Finally, we can check which C parameter is the best amongst the chosen.
    print('*********************************************************************************')
    print('Best model to choose from cross validation is with C parameter = ', best_c)
    print('*********************************************************************************')

    return best_c

我得到的错误如下: 对于这一行:fold = KFold(len(y_train_data),5,shuffle=False) 错误:

TypeError: init() 为参数 'shuffle' 获得了多个值

如果我从这一行中删除 shuffle=False,我会收到以下错误:

TypeError: shuffle must be True or False;得到 5 个

如果我删除 5 并保留 shuffle=False,我会收到以下错误;

TypeError: 'KFold' 对象不可迭代 来自这一行:for iteration, indices in enumerate(fold,start=1):

如果有人可以帮助我解决这个问题并建议如何使用最新版本的 scikit-learn 来完成,我们将不胜感激。

谢谢。

【问题讨论】:

    标签: python machine-learning scikit-learn cross-validation


    【解决方案1】:

    这取决于您如何导入 KFold。

    如果你这样做了:

    from sklearn.cross_validation import KFold
    

    那么你的代码应该可以工作了。因为它需要 3 个参数:- 数组长度、分割数和随机播放

    但如果你这样做:

    from sklearn.model_selection import KFold
    

    那么这将不起作用,您只需要传递拆分和随机播放的数量。在enumerate() 中进行更改时无需传递数组的长度。

    顺便说一下,model_selection 是新模块,推荐使用。尝试像这样使用它:

    fold = KFold(5,shuffle=False)
    
    for train_index, test_index in fold.split(X):
    
        # Call the logistic regression model with a certain C parameter
        lr = LogisticRegression(C = c_param, penalty = 'l1')
        # Use the training data to fit the model. In this case, we use the portion of the fold to train the model
        lr.fit(x_train_data.iloc[train_index,:], y_train_data.iloc[train_index,:].values.ravel())
    
        # Predict values using the test indices in the training data
        y_pred_undersample = lr.predict(x_train_data.iloc[test_index,:].values)
    
        # Calculate the recall score and append it to a list for recall scores representing the current c_parameter
        recall_acc = recall_score(y_train_data.iloc[test_index,:].values,y_pred_undersample)
        recall_accs.append(recall_acc)
    

    【讨论】:

    • 感谢Vivek提供的额外信息,我确实在使用model_selection,因此不明白导致此错误的原因,现在我知道了,在您为我澄清后,谢谢。
    【解决方案2】:

    KFold 是一个拆分器,所以你必须给一些东西来拆分。

    示例代码:

    X = np.array([1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4]])
    y = np.array([1, 2, 3, 4])
    # Now you create your Kfolds by the way you just have to pass number of splits and if you want to shuffle.
    fold = KFold(2,shuffle=False)
    # For iterate over the folds just use split
    for train_index, test_index in fold.split(X):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]
        # Follow fitting the classifier
    

    如果你想获取训练/测试循环的索引,只需添加枚举

    for i, train_index, test_index in enumerate(fold.split(X)):
        print('Iteration:', i)
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]
    

    我希望这行得通

    【讨论】:

    • 嘿 Tzomas,谢谢你的回答,它确实解决了错误问题,但我真的不明白为什么要拆分 X?在内核本身中,它对每个参数 C 迭代 5 次,但在我的情况下,它迭代 X 的长度数,远远高于 5,这里有什么问题?
    • 对不起,我想我的错字有一些错误,它确实解决了我的问题,谢谢!
    猜你喜欢
    • 2018-04-17
    • 2013-09-01
    • 2017-05-15
    • 2017-08-27
    • 2018-10-10
    • 2021-12-13
    • 2019-02-20
    • 2020-03-27
    • 2018-12-12
    相关资源
    最近更新 更多