Skip to main content
 首页 » 编程设计

python之Sklearn RandomizedSearchCV 操作系统错误 : [Errno 5] Input/output error

2024年12月31日6zhwl

我正在尝试使用 RandomizedSearch 来确定 SKLearn 的 MLP 和 XGBoost 的最佳超参数。运行优化时,大约运行 50 次后出现 OSError。

我使用 XGBoost 进行随机搜索的代码:

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 代码并且一切正常。

请您参考如下方法:

我遇到了同样的问题,我发现将 xgboost 文件夹中现有的 xgboost.dll 替换为来自 http://www.picnet.com.au/blogs/guido/2016/09/22/xgboost-windows-x64-binaries-for-download/ 的二进制文件之一。解决了我的问题。