简体   繁体   中英

how can i adapt this deprecated StratifiedKFold code

I have a dataset where the response values are not balanced, I have a lot more of my qualified rejected vs non-rejected value so I'm looking to balance my dataset.

To that end there was a code that worked with the now deprecated cross_validation.StratifiedKFold but now I need to adapt it and I don't understand it perfectly so I'm looking for help.

The original code is:

def stratified_cv(X, y, clf_class, shuffle=True, n_folds=10, **kwargs):
    stratified_k_fold = cross_validation.StratifiedKFold(y, n_folds=n_folds, shuffle=shuffle)
    y_pred = y.copy()
    # ii -> train
    # jj -> test indices
    for ii, jj in stratified_k_fold: 
        X_train, X_test = X[ii], X[jj]
        y_train = y[ii]
        clf = clf_class(**kwargs)
        clf.fit(X_train,y_train)
        y_pred[jj] = clf.predict(X_test)
    return y_pred

Where X is the dataset fit_transformed, converted to a numpy float array and scaled and the y is the "rejected" vs. "not-rejected" classification converted to an array of int (0 or 1 of course). Finally the clf_class(**kwargs) can be classifiers like ensemble.GradientBoostingClassifier , svm.SVC and ensemble.RandomForestClassifier

X = np.array([[-0.6786493 ,  0.67648946, -0.52360328, -0.32758048,  1.6170861 ,
        1.23488274,  1.56676695,  0.47664315,  1.56703625, -0.07060962,
       -0.05594035, -0.07042665,  0.86674322, -0.46549436,  0.86602851,
       -0.08500823, -0.60119509, -0.0856905 , -0.42793202],[0.6031696 ,  0.14906505, -0.52360328, -0.32758048,  1.6170861 ,
        1.30794844, -0.33373776,  1.12450284, -0.33401297, -0.10808036,
        0.14486653, -0.10754944,  1.05857074,  0.14782467,  1.05938994,
        1.24048169, -0.60119509,  1.2411686 , -0.42793202],[ 0.33331299,  0.9025285 , -0.52360328, -0.32758048, -0.61839626,
       -0.59175986,  1.16830364,  0.67598459,  1.168464  , -1.57338336,
        0.49627857, -1.57389963, -0.75686906,  0.19893459, -0.75557074,
        0.70312091,  0.21153386,  0.69715637, -1.1882185 ],[ 0.6031696 , -0.42859027, -0.68883427,  3.05268496, -0.61839626,
       -0.59175986,  2.19659605, -1.46693591,  2.19675881, -2.74286476,
       -0.60815927, -2.7432675 , -0.07855114, -0.5677142 , -0.07880574,
       -1.30302599,  1.02426282, -1.30640087,  0.33235445],[ 0.67063375, -0.6546293 , -0.52360328,  3.05268496, -0.61839626,
       -0.59175986, -0.24008971,  0.62614923, -0.24004065, -1.03893233,
        1.0986992 , -1.03793936, -0.27631146,  1.06780322, -0.27656174,
       -0.04918418, -0.60119509, -0.04588472,  1.09264093],[-0.74611345, -0.90578379, -0.52360328, -0.32758048, -0.61839626,
       -0.59175986, -0.93051461,  1.82219789, -0.93025113,  0.54272717,
       -0.85916786,  0.54209937,  0.15678365,  0.55670403,  0.15850147,
        0.88224117,  0.61789834,  0.88291665,  1.8529274 ],[ 0.53570545,  1.50529926, -0.52360328, -0.32758048, -0.61839626,
       -0.59175986,  2.81173526, -1.66627735,  2.81135938,  2.30385178,
       -0.15634379,  2.3031117 , -0.79642112,  1.42557266, -0.79512194,
       -1.73291462,  1.83699177, -1.73099578,  1.8529274 ]])

y = np.array([0,0,0,0,0,1,1])

StratifiedKFold has moved into model_selection . So you should do:

from sklearn.model_selection import StratifiedKFold
def stratified_cv(X, y, clf_class, shuffle=True, n_folds=10, **kwargs):
    stratified_k_fold = StratifiedKFold(n_splits=n_folds, shuffle=shuffle)
    y_pred = y.copy()
    # ii -> train
    # jj -> test indices
    for ii, jj in stratified_k_fold.split(X,y): 
        X_train, X_test = X[ii], X[jj]
        y_train = y[ii]
        clf = clf_class(**kwargs)
        clf.fit(X_train,y_train)
        y_pred[jj] = clf.predict(X_test)
    return y_pred

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM