Skip to content

Commit 6d4492d

Browse files
author
minjk-bl
committed
Add top count option for Plot_Feature_importances
1 parent 7f771e0 commit 6d4492d

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

js/m_ml/ModelInfo.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,12 @@ define([
425425
'plot_feature_importances': {
426426
name: 'plot_feature_importances',
427427
label: 'Plot feature importances',
428-
code: "vp_plot_feature_importances(${model}, ${fi_featureData}${sort})",
428+
code: "vp_plot_feature_importances(${model}, ${fi_featureData}${sort}${top_count})",
429429
description: 'Draw feature_importances_',
430430
options: [
431431
{ name: 'fi_featureData', label: 'Feature Data', component: ['data_select'], var_type: ['DataFrame', 'Series', 'ndarray', 'list', 'dict'], value: 'X_train' },
432-
{ name: 'sort', label: 'Sort data', component: ['bool_checkbox'], value: true, usePair: true }
432+
{ name: 'sort', label: 'Sort data', component: ['bool_checkbox'], value: true, usePair: true },
433+
{ name: 'top_count', label: 'Top count', component: ['input_number'], min: 0, max: 5, usePair: true },
433434
]
434435
}
435436
}

python/userCommand.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ def vp_create_feature_importances(model, X_train=None, sort=False):
8181
######
8282
# Visual Python: Machine Learning > Model Info
8383
######
84-
def vp_plot_feature_importances(model, X_train=None, sort=False):
84+
def vp_plot_feature_importances(model, X_train=None, sort=False, top_count=0):
8585
df_i = vp_create_feature_importances(model, X_train, sort)
8686

87-
if sort: df_i['Percentage'].sort_values().plot(kind='barh')
88-
else: df_i['Percentage'].plot(kind='barh')
87+
if sort:
88+
if top_count > 0:
89+
df_i['Percentage'].sort_values().tail(top_count).plot(kind='barh')
90+
else:
91+
df_i['Percentage'].sort_values().plot(kind='barh')
92+
else:
93+
df_i['Percentage'].plot(kind='barh')
8994
_vp_plt.xlabel('Feature importance Percentage')
9095
_vp_plt.ylabel('Features')
9196

0 commit comments

Comments
 (0)