如何在Python的joblib中实现“如果继续”

时间:2019-08-27 00:09:08

标签: python parallel-processing joblib

假设我在joblib中实现了以下循环:

from sklearn.model_selection import KFold
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix, f1_score
from sklearn import svm
from sklearn import datasets
from sklearn.model_selection import StratifiedKFold
from sklearn.svm import LinearSVC
from joblib import Parallel, delayed

iris = datasets.load_iris()
X, Y = iris.data, iris.target

skf = StratifiedKFold(n_splits=5)
clf = svm.SVC(kernel='rbf')

def train(train_index, test_index):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = Y[train_index], Y[test_index]

    clf.fit(X_train, y_train)
    Y_predict = clf.predict(X_test)

    f1 = f1_score(y_test, Y_predict, average='weighted')
    conf_mat = confusion_matrix(y_test, Y_predict)

    return dict(f1=f1, conf_mat=conf_mat)


out = Parallel(n_jobs=2, verbose=100, pre_dispatch='1.5*n_jobs')(
    delayed(train)(train_index, test_index) for train_index, test_index in skf.split(X, Y))

f1_scores = [d['f1'] for d in out]
conf_mats = [d['conf_mat'] for d in out]

print('f1_scores:', f1_scores)
print('confusion matrices:', conf_mats)

我想检查循环中的条件:

  

对于train_index,skf.split(X,Y)中的test_index

如果满足条件,则在下一个迭代中继续。例如,如果我想跳过火车和测试数组的索引(如果想精确的话,则为#5)。

如本代码所示,如​​何在joblib上下文中实现Python continue

0 个答案:

没有答案