与GridSearch结合使用的自定义交叉验证功能

时间:2020-09-12 03:14:51

标签: python cross-validation gridsearchcv

我目前正在自学Python和机器学习,并且正在从事有关分类的项目。我有可执行代码,我想自己重写并学习。 现在,我已经无法独自前进了。我将AdaBoost,CatBoost,XGBoost等不同的分类器合在一起使用。

第一个功能是自定义的交叉值功能,这是可以理解的。 第二个功能是GridSearch的扩展功能,我不完全了解它,现在想重写为“普通GridSearch功能”。关于第二个功能的任何提示和帮助,我将不胜感激

可以在这里找到称为ParamSearch的原始“定制” GridSearch: https://effectiveml.com/files/paramsearch.py

def crossvaltest_cat(params, X, y, n_splits=5):
    skf = StratifiedKFold(n_splits=5)
    accuracy, score, f1 = [], [], []
    for train_index, test_index in skf.split(X, y):
        X_train, X_test = X.iloc[train_index, :], X.iloc[test_index, :]
        y_train, y_test = y.iloc[train_index], y.iloc[test_index]
        
        clf = CatBoostClassifier(**params)
        clf.fit(X_train, y_train)
        
        y_pred = np.array(clf.predict(X_test))
        tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
        
        accuracy.append(accuracy_score(y_test, y_pred))
        score.append(score_function(tp,fp,fn,tn))
        f1.append(f1_score(y_test, y_pred))
        
    return np.mean(score)

def cat_param_tune(params, X, y ,n_splits=5):
    ps = paramsearch(params)
    for prms in chain(ps.grid_search(['border_count']),
                      ps.grid_search(['l2_leaf_reg']),
                      ps.grid_search(['iterations','learning_rate']),
                      ps.grid_search(['depth'])):
        res = crossvaltest_cat(prms,X, y,n_splits)
        ps.register_result(res,prms)
        print(res,prms,'best:',ps.bestscore(),ps.bestparam())
        print()
    return ps.bestparam(), ps.bestscore()

0 个答案:

没有答案