当使用XGB想得到特征重要性时报错,代码及报错如下,

from xgboost.sklearn import XGBClassifier
import xgboost as xgb
def modelfit(alg, dtrain,y_train, dtest=None ,useTrainCV=True, cv_folds=5, early_stopping_rounds=50):if useTrainCV:xgb_param = alg.get_xgb_params()xgtrain = xgb.DMatrix(dtrain.as_matrix()[: ,1:], label=y_train)#xgtest = xgb.DMatrix(dtest.as_matrix()[: , :])cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds ,early_stopping_rounds=early_stopping_rounds)alg.set_params(n_estimators=cvresult.shape[0])#建模alg.fit(dtrain.as_matrix()[: ,1:], y_train ,eval_metric='auc')#对训练集预测dtrain_predictions = alg.predict(dtrain.as_matrix()[: ,1:])dtrain_predprob = alg.predict_proba(dtrain.as_matrix()[: ,1:])[:,1]#输出模型的一些结果#print(dtrain_predictions)#print(alg.predict_proba(dtrain.as_matrix()[: ,1:]))print(cvresult.shape[0])print ("\n关于现在这个模型")print ("准确率 : %.4g" % metrics.accuracy_score(y_train, dtrain_predictions))print ("AUC 得分 (训练集): %f" % metrics.roc_auc_score(y_train, dtrain_predprob))feat_imp = pd.Series(alg.get_booster().get_fscore()).sort_values(ascending=False)print(feat_imp.head(25))print(feat_imp.shape)feat_imp.plot(kind='bar', title='Feature Importances')plt.ylabel('Feature Importance Score')
xgb1 = XGBClassifier(learning_rate=0.1,n_estimators =50,max_depth=4,min_child_weight =1,gamma = 0,subsample=0.8,colsample_bytree = 0.8,objective ='binary:logistic' ,nthread=4,scale_pos_weight=0.8,seed = 27)
modelfit(xgb1 ,X_train,y_train)


遇到这个问题,开始我是佷懵的。

参考:https://stackoverflow.com/questions/38212649/feature-importance-with-xgbclassifier 上的内容后找到解决方法。

此时我们只需要将model.booster()改为model.get_booster()即可。
参考:https://blog.csdn.net/u012735708/article/details/84334218

关于XGB.booster()报错TypeError: 'str' object is not callable的解决方法相关推荐

  1. Python报错TypeError: 'str' object is not callable

    原文:http://blog.sina.com.cn/s/blog_71f3890901017rsh.html Python报错TypeError: 'str' object is not calla ...

  2. python报错:TypeError: 'str' object is not callable

    这个问题遇到好多次了,这次记录一下, 问题重现 代码仅保留最重要的: # summaries 是一个集合 # 求和并打印 result = 0 for str in summaries: result ...

  3. python nonetype object is not_python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法...

    发现问题 写python的时候出现了这个错,然后网上的教程的解决方案几乎都是--"重新定义下这个变量",看的我一脸懵逼 后来发现原来是我把return None的方法赋给了变量,之 ...

  4. Python----方法返回None值报错 TypeError NoneType object is not callable

    转载请声明,本文来自:https://blog.csdn.net/shijianduan1/article/details/106415395 相信很多小伙伴, 在遇到报错的时候,直接搜索报错内容, ...

  5. Matplotlib 使用 xlabel(“xxx”)时候报错,‘str‘ object is not callable

    Matplotlib 绘图使用plt.xlabel("xx")报错'str'object is not callable解决方法 这里并不是直接把字符串赋值给 xlabel 例如: ...

  6. WEEX 报错 TypeError: Converting circular structor to JSON 的解决方法

    背景 在进行千牛QAP插件开发的时候,有时会遇到 TypeError: Converting circular structor to JSON,并且看不到报错位置,对新手可能会造成疑惑. 原因 we ...

  7. TypeError: 'str' object is not callable

    Python 报错 TypeError: 'str' object is not callable 原因是我的一个变量名和python api的某个函数名一样,导致 检查你的代码中变量的名字,看有没有 ...

  8. Python报错TypeError: ‘User‘ object is not iterable

    Python报错TypeError: 'User' object is not iterable 原因 User对象是不可迭代的,我这里是因为User并不是list,所以是不可迭代的,所以不使用迭代即 ...

  9. 爬虫的自创建请求对象:报错AttributeError: 'str' object has no attribute 'items'

    在使用爬虫库创建自定义请求对象时 将值传入headers agent = random.choice(userAgent) REQ = request.Request(url,headers=agen ...

  10. Python中TypeError: ‘str‘ object is not callable

    str( )是python自带函数,是python保留的关键字,定义变量时应该避免使用str作为变量名.如果在使用str( )函数之前已经定义过str变量,则会出现TypeError: 'str' o ...

最新文章

  1. 周志华出任京东人工智能研究院学术委员会委员
  2. AB1601 PWM注意事项
  3. 字节 字符 utf-8 unicode 以及提交表单验证字符长度 .......
  4. 大话ion系列(三)
  5. OSI模型 TCP/IP模型 数据包结构
  6. mysql获取数据库名_mysql获取数据库名
  7. java 判断是不是微信打开_Java判断浏览器是微信还是支付宝
  8. BZOJ5251 八省联考2018劈配(网络流)
  9. python主要用来做什么-python主要用来做什么?Python开发简单吗?
  10. Mysql的跨表更新
  11. APT入门知识:抗击APT和针对性攻击
  12. php fork demo,php多进程demo
  13. Oracle:PL/SQL程序开发
  14. cai计算机辅助教程,拓展:计算机辅助教学(CAI)的基本模式
  15. 最短路径spfa算法
  16. 论文的参考文献怎样标注?
  17. markdown贴gif图片
  18. 苹果手机sim卡无效怎么办_苹果手机解锁密码忘了怎么办
  19. 如何撤销命令make install的安装结果,或者如何卸载Linux 库library ?
  20. 仿coco点餐系统的微信小程序

热门文章

  1. 微软一些工具的官方下载地址
  2. 何冲(为奥运冠军名字作诗)
  3. Codeforces Round #352 (Div. 1) B. Robin Hood
  4. HALCON-FUZZY检测用于开关引脚测量
  5. 在vs中使用正则表达式
  6. jQuery中浏览器版本判断的一个BUG,此BUG已影响到jqModal,thickbox等多个jQuery插件的应用...
  7. 无需插拔网线,解决一台电脑同时上内外网的难题
  8. 树——二叉树的深层特性
  9. ubuntu(linux)占领小米平板2(mipad2)
  10. python第九天课程:遇到了金角大王