KFold with sklearn.model_selection shuffle Error

Viewed 484

I try to use the KFold function but give me this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-175-38368ff78797> in <module>()
      1 cs = np.logspace(-4,5,10)
      2 
----> 3 aucs_l2=kfoldlr(train, 5, 3)

2 frames
/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_split.py in __init__(self, n_splits, shuffle, random_state)
    286         if not isinstance(shuffle, bool):
    287             raise TypeError("shuffle must be True or False;"
--> 288                             " got {0}".format(shuffle))
    289 
    290         if not shuffle and random_state is not None:  # None is the default

TypeError: shuffle must be True or False; got 5

Here my Code:

from sklearn.model_selection import KFold
from sklearn import metrics
from sklearn import preprocessing



def kfoldlr(dataset , k , cs, l1_penalty = False):
    '''Performs kfold cross validation for the dataset and also searches for the optimal regularixation parameter '''
    aucs = {}
    kf = KFold(len(dataset),k) #Provides indices to split data in train/test sets
    for train_idx, vali_idx in kf: #For loop to extract and append AUC values to array from training data
        cv_train,cv_validate=dataset.iloc[train_idx,:], dataset.iloc[vali_idx,:]
        for c in cs:
            auc, f_imp, tpr, fpr, threshold, accuracy = LRmodel(cv_train,cv_validate,c, l_1 = l1_penalty)
            if c in aucs:
                aucs[c].append(auc)
            else:
                aucs[c]=[]
                aucs[c].append(auc)
    return aucs

aucs_l2=kfoldlr(train, 5, 3)

I know that comes from the old version from sklearn. But I don't know where I can set the shuffle in the function.

Thank you for your help !

1 Answers

The syntax is kf = KFold(n_splits=k, shuffle=False, random_state=0 and then for train_idx, vali_idx in kf.split(X).


Replace:

for train_idx, vali_idx in kf

with

for train_idx, vali_idx in kf.split(X)

E.g.

from sklearn.model_selection import KFold
from sklearn import metrics
from sklearn import preprocessing
from sklearn.datasets import load_iris

dataset = load_iris().data
y = load_iris().target

def kfoldlr(dataset , k , cs, l1_penalty = False):
    '''Performs kfold cross validation for the dataset and also searches for the optimal regularixation parameter '''
    kf = KFold(n_splits=k, shuffle=False, random_state=0) #Provides indices to split data in train/test sets
    for train_idx, vali_idx in kf.split(dataset): #For loop to extract and append AUC values to array from training data
        print(train_idx, vali_idx)
    return 

aucs_l2=kfoldlr(dataset, 3, 3)

will return

[ 50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67
  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103
 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
 140 141 142 143 144 145 146 147 148 149] [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49]
[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35
  36  37  38  39  40  41  42  43  44  45  46  47  48  49 100 101 102 103
 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
 140 141 142 143 144 145 146 147 148 149] [50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
 98 99]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99] [100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
 136 137 138 139 140 141 142 143 144 145 146 147 148 149]
Related