微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何检查通过的每个最大深度的 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 举报,一经查实,本站将立刻删除。