如何检查通过的每个最大深度的 XGBoost 模型性能

问题描述

我有一个函数可以计算传递的每个参数值的模型精度,具体取决于我希望操作是集成还是只使用分类器模型 i已经过去了。

在我的情况下,我想检查我的 XGBoost 的模型性能,在每个最大深度 值达到指定范围的准确性方面

目前,当我运行下面的代码时,它采用 max_depth 的认 xgboost 值,而不是从 i 中选择 max_depth=for i in values

classifier = xgb.XGBClassifier(max_depth=i,n_estimators=300,learning_rate=0.05)

result = performance_check(X_train,y_train,X_test,y_test,classifier,args=21,xlabel="Max Depth",baggingClassifier=False)

我收到一条错误消息

XGBoostError:max_depth 的无效参数格式期望 int 但 value='[1139 376]'

def performance_check(X_train,args,xlabel,max_samples=0.6,baggingClassifier=True):
    # define lists to collect scores
    train_scores,test_scores = list(),list()
    best_value = []
    # define the tree depths to evaluate
    values = [i for i in range(1,args)]
    # evaluate classifier for the range of passed in argument
    for i in values:
        if baggingClassifier:
            model = BaggingClassifier(classifier,n_estimators=i,max_samples=max_samples,max_features=1.0,bootstrap=True,bootstrap_features=False).fit(X_train,y_train)          
        elif not baggingClassifier:
            model = classifier
            model.fit(X_train,y_train)
            
        # evaluate on the train dataset
        train_yhat = model.predict(X_train)
        train_acc = accuracy_score(y_train,train_yhat)
        train_scores.append(train_acc)
        # evaluate on the test dataset
        test_yhat = model.predict(X_test)
        test_acc = accuracy_score(y_test,test_yhat)
        test_scores.append(test_acc)
        
        # find best value based on best test score
        if test_acc == max(test_scores):
            best_value = i
        
    print('Best Number of Estimator is: ',best_value)
        
        # summarize progress
        # print(f'{i}. train: {train_acc:.3f},test: {test_acc:.3f}')
        
    # plot of train and test scores vs n_estimators
    plot = accuracy_performance_plot(values,train_scores,test_scores,xlabel=xlabel)
        
    return plot
def accuracy_performance_plot(values,xlabel):
    plt.plot(values,'-o',label='Train')
    plt.plot(values,label='Test')
    plt.title('Model Accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel(f'{xlabel}')
    plt.legend()
    
    return plt.show();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)