我正在尝试使用RandomizedSearch来确定SKLearn的MLP和XGBoost的最佳hypter参数。在运行优化时,大约运行50次后,发生OSError。
我用于XGBoost的randomizedSearch的代码:
from scipy.stats import randint as sp_randint
import xgboost as xgb
from sklearn.model_selection import RandomizedSearchCV
from joblib import dump, load
from sklearn.model_selection import StratifiedShuffleSplit
import numpy as np
import pickle
# Reference f1 eval --> https://stackoverflow.com/questions/51587535/custom-evaluation-function-based-on-f1-for-use-in-xgboost-python-api
from sklearn.metrics import f1_score
import numpy as np
def f1_eval(y_pred, dtrain):
y_true = dtrain.get_label()
err = 1-f1_score(y_true, np.round(y_pred))
print("Score: ", str(1 - err))
return 'f1_err', err
neg_samples = len(y[y['canceled_in_6_mon'] == 0])
pos_samples = len(y[y['canceled_in_6_mon'] == 1])
xgb_model = xgb.XGBClassifier(objective= 'reg:logistic', nthread=1,
scale_pos_weight=neg_samples / pos_samples)
parameters = {
'learning_rate': [0.005, 0.01, 0.05, 0.1, 0.15, 0.25, 0.35], #so called `eta` value
'max_depth': sp_randint(10, 100),
'min_child_weight': sp_randint(1, 8),
'silent': [1],
'gamma': [0, 0.2, 0.5, 0.7, 1],
'subsample': [0.5, 0.7, 1],
'colsample_bytree': [0.5, 0.7, 1],
"n_estimators": sp_randint(20, 100),
"max_features": sp_randint(10, 400),
"min_samples_split": sp_randint(2, 20),
"seed": [42],
"min_samples_leaf": sp_randint(1, 5)
}
ss = StratifiedShuffleSplit(n_splits=3, test_size=0.24, random_state=42)
clf = RandomizedSearchCV(estimator=xgb_model, param_distributions = parameters,cv=ss, verbose=10, n_jobs=4, scoring='f1', n_iter=50)
clf.fit(X=X_train, y=np.ravel(y_train), eval_metric=f1_eval)
这是我得到的完整输出,包括错误: Pastebin Link
为什么会发生这种情况?该错误表明Joblib存在问题,但我使用RandomForest分类器执行了相同的randomizedSearch代码,并且一切正常。