使用matplotlib绘制具有十大特征的特征重要性

时间:2020-06-23 14:19:38

标签: python matplotlib

我在数据集上运行了一个随机森林,该森林具有100多个变量。我很想创建我的RF的功能重要性图。但是,使用当前的python代码,我只能在绘图上显示所有变量。如果我只想显示前10个或前20个功能的重要性怎么办?我怎样才能做到这一点?谢谢!

%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

features = bvsa_train_feature.columns
importances = best_rf.feature_importances_
indices = np.argsort(importances)

plt.figure(figsize=(10,100))
plt.title('Feature Importances')
plt.barh(range(len(indices)), importances[indices], color='b', align='center')
plt.yticks(range(len(indices)), [features[i] for i in indices])
plt.xlabel('Relative Importance')
plt.show()

2 个答案:

答案 0 :(得分:2)

我将从排序数组中选择前10/20个值

>>> import requests

>>> url = "https://images.nasa.gov/search-results?q=Apollo"

>>> headers = {'User-Agent' : 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'}

>>> res = requests.get(url, headers=headers)
>>> res.status_code

>>> len(res.content)
>>> print(res.content)



200
2868
b'<!doctype html><!--[if IE 9]>\n\t<html class="no-js ie9" lang="en">\n<![endif]--><!--[if !IE 9]>--> <html class="no-js" lang="en" data-ng-app="availFeApp"> <!--<![endif]--> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1"> <meta name="pinterest" content="nopin"> <meta name="google-site-verification" content="ZKpcXLqaBX3jND8Fybkvr3MaaOpC_6MRjXBYm0XNkJQ"> <meta name="theme-color" content="#ffffff"> <base href="/"> <!--Title--> <meta property="og:title" content="{{ngMeta.title}}"> <title ng-bind="ngMeta.title"></title> <meta property="og:description" content="{{ngMeta.description}}"> <meta ng-if="ngMeta[\'url\']" property="og:url" content="{{ngMeta.url}}"> <meta ng-if="ngMeta[\'image\']" property="og:image" content="{{ngMeta.image}}"> <meta ng-if="ngMeta[\'imageWidth\']" property="og:image:width" content="{{ngMeta.imageWidth}}"> <meta ng-if="ngMeta[\'imageHeight\']" property="og:image:height" content="{{ngMeta.imageHeight}}"> <meta ng-if="ngMeta[\'fbAppId\']" ng-if="ngMeta[\'fbAppId\']" property="fb:app_id" content="{{ngMeta.fbAppId}}"> <meta ng-if="ngMeta[\'type\']" property="og:type" content="{{ngMeta.type}}"> <!--favicon support--> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png?v=eEa2MLRaNk"> <link rel="icon" type="image/png" href="/favicon-32x32.png?v=eEa2MLRaNk" sizes="32x32"> <link rel="icon" type="image/png" href="/favicon-16x16.png?v=eEa2MLRaNk" sizes="16x16"> <link rel="manifest" href="/manifest.json?v=eEa2MLRaNk"> <link rel="mask-icon" href="/safari-pinned-tab.svg?v=eEa2MLRaNk" color="#0d3d92"> <link rel="shortcut icon" href="/favicon.ico?v=eEa2MLRaNk"> <link rel="stylesheet" href="styles/vendor.100c511f.css"> <!--google fonts--> <link href="https://fonts.googleapis.com/css?family=Titillium+Web:300,400" rel="stylesheet"> <!--pretty checkbox library styles--> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pretty-checkbox@3.0/dist/pretty-checkbox.min.css"> <link rel="stylesheet" href="styles/main.8534951e.css"> <!-- We participate in the US government\'s analytics program. See the data at analytics.usa.gov. --> <script async type="text/javascript" src="https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js?agency=NASA&dclink=true" id="_fed_an_ua_tag"></script>  <body ng-strict-di> <!--[if lt IE 7]>\n\t<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>\n\t<![endif]--> <div id="page-wrapper" data-ng-view="" autoscroll="true"></div> <script src="scripts/vendor.d2e52c64.js"></script> <script src="scripts/scripts.bd45ad67.js"></script>  '

或切片顶部索引

important_features = np.sort(importances)[:10]

答案 1 :(得分:1)

您可以这样做:

features = bvsa_train_feature.columns
importances = best_rf.feature_importances_
indices = np.argsort(importances)

# customized number 
num_features = 10 

plt.figure(figsize=(10,100))
plt.title('Feature Importances')

# only plot the customized number of features
plt.barh(range(num_features), importances[indices[-num_features:]], color='b', align='center')
plt.yticks(range(num_features), [features[i] for i in indices[-num_features:])
plt.xlabel('Relative Importance')
plt.show()