【集成学习】sklearn中xgboost模块中plot_importance函数(绘图--特征重要性)

直接上代码,简单

  1 # -*- coding: utf-8 -*-
  2 """
  3 ###############################################################################
  4 # 作者:wanglei5205
  5 # 邮箱:wanglei5205@126.com
  6 # 代码:http://github.com/wanglei5205
  7 # 博客:http://cnblogs.com/wanglei5205
  8 # 目的:学习xgboost的plot_importance函数
  9 # 官方API文档:http://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.training
 10 ###############################################################################
 11 """
 12 ### load module
 13 import matplotlib.pyplot as plt
 14 from sklearn import datasets
 15 from sklearn.model_selection import train_test_split
 16 from sklearn.metrics import accuracy_score
 17 from xgboost import XGBClassifier
 18 from xgboost import plot_importance
 19 
 20 ### load datasets
 21 digits = datasets.load_digits()
 22 
 23 ### data analysis
 24 print(digits.data.shape)
 25 print(digits.target.shape)
 26 
 27 ### data split
 28 x_train,x_test,y_train,y_test = train_test_split(digits.data,
 29                                                  digits.target,
 30                                                  test_size = 0.3,
 31                                                  random_state = 33)
 32 
 33 model = XGBClassifier()
 34 model.fit(x_train,y_train)
 35 
 36 ### plot feature importance
 37 fig,ax = plt.subplots(figsize=(15,15))
 38 plot_importance(model,
 39                 height=0.5,
 40                 ax=ax,
 41                 max_num_features=64)
 42 plt.show()
 43 
 44 ### make prediction for test data
 45 y_pred = model.predict(x_test)
 46 
 47 ### model evaluate
 48 accuracy = accuracy_score(y_test,y_pred)
 49 print("accuarcy: %.2f%%" % (accuracy*100.0))
 50 """
 51 95.0%
 52 """
posted @ 2018-03-16 10:31  wanglei5205  阅读(13075)  评论(2编辑  收藏  举报
levels of contents