• 使用Xgboost时出现错误 TypeError: 'str' object is not callable


    在Xgboost进行调参代码时,出现错误,TypeError: 'str' object is not callable

    def modelfit(alg,dtrain,predictors,useTrainCV=True,cv_folds=5,early_stopping_rounds=50):
        if useTrainCV:
            xgb_param = alg.get_xgb_params()
            xgbtrain = xgb.DMatrix(dtrain[predictors].values,label=dtrain[target].values)
            cvresult = xgb.cv(xgb_param,xgbtrain,num_boost_round=alg.get_params()['n_estimators'],
                              nfold=cv_folds,metrics='auc',early_stopping_rounds=early_stopping_rounds,
                              show_stdv=False)
            alg.set_params(n_estimators=cvresult.shape[0])
        # Fit the algorithm on the data
        alg.fit(dtrain[predictors],dtrain['FLAG'],eval_metric='auc')
        
        #Predict training set:
        dtrain_predictions = alg.predict(dtrain[predictors])
        dtrain_predprob = alg.predict_proba(dtrain[predictors])[:,1]
    
        #Print model report:
        print("
    Model Report") 
        print("Accuracy : %.4g" % metrics.accuracy_score(dtrain['FLAG'].values, dtrain_predictions)) 
        print("AUC Score (Train): %f" % metrics.roc_auc_score(dtrain['FLAG'], dtrain_predprob)) 
        
        print('sucessful')
    
        feat_imp = pd.Series(alg.booster().get_fscore()).sort_values(ascending=False)
        feat_imp.plot(kind='bar', title='Feature Importances')
        plt.ylabel('Feature Importance Score')
    

    解决方法:

    将代码中的 alg.booster() 改成alg.get_booster() 即可。

     
  • 相关阅读:
    基因组注释
    GapCloser
    Endnote参考文献格式修改
    多行变单行
    AD的基因组【转载】
    ROC曲线
    自我觉察-4:觉察“不浪费食物”和“胃过饱食”的信念
    自我觉察6-我的价值感?
    表观遗传、开放染色质测序、ATAC-seq、ChIP-seq简介
    ADNI(Alzheimer`s disease neuroimaging initiative)介绍
  • 原文地址:https://www.cnblogs.com/xiaodongsuibi/p/9303959.html
Copyright © 2020-2023  润新知