How to use the lightgbm.plot_importance function in lightgbm

To help you get started, we’ve selected a few lightgbm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / LightGBM / tests / python_package_test / test_plotting.py View on Github external
self.assertEqual(ax0.get_ylabel(), 'Features')
        self.assertLessEqual(len(ax0.patches), 30)

        gbm1 = lgb.LGBMClassifier(n_estimators=10, num_leaves=3, silent=True)
        gbm1.fit(self.X_train, self.y_train)

        ax1 = lgb.plot_importance(gbm1, color='r', title='t', xlabel='x', ylabel='y')
        self.assertIsInstance(ax1, matplotlib.axes.Axes)
        self.assertEqual(ax1.get_title(), 't')
        self.assertEqual(ax1.get_xlabel(), 'x')
        self.assertEqual(ax1.get_ylabel(), 'y')
        self.assertLessEqual(len(ax1.patches), 30)
        for patch in ax1.patches:
            self.assertTupleEqual(patch.get_facecolor(), (1., 0, 0, 1.))  # red

        ax2 = lgb.plot_importance(gbm0, color=['r', 'y', 'g', 'b'],
                                  title=None, xlabel=None, ylabel=None)
        self.assertIsInstance(ax2, matplotlib.axes.Axes)
        self.assertEqual(ax2.get_title(), '')
        self.assertEqual(ax2.get_xlabel(), '')
        self.assertEqual(ax2.get_ylabel(), '')
        self.assertLessEqual(len(ax2.patches), 30)
        self.assertTupleEqual(ax2.patches[0].get_facecolor(), (1., 0, 0, 1.))  # r
        self.assertTupleEqual(ax2.patches[1].get_facecolor(), (.75, .75, 0, 1.))  # y
        self.assertTupleEqual(ax2.patches[2].get_facecolor(), (0, .5, 0, 1.))  # g
        self.assertTupleEqual(ax2.patches[3].get_facecolor(), (0, 0, 1., 1.))  # b
github microsoft / LightGBM / tests / python_package_test / test_plotting.py View on Github external
def test_plot_importance(self):
        gbm0 = lgb.train(self.params, self.train_data, num_boost_round=10)
        ax0 = lgb.plot_importance(gbm0)
        self.assertIsInstance(ax0, matplotlib.axes.Axes)
        self.assertEqual(ax0.get_title(), 'Feature importance')
        self.assertEqual(ax0.get_xlabel(), 'Feature importance')
        self.assertEqual(ax0.get_ylabel(), 'Features')
        self.assertLessEqual(len(ax0.patches), 30)

        gbm1 = lgb.LGBMClassifier(n_estimators=10, num_leaves=3, silent=True)
        gbm1.fit(self.X_train, self.y_train)

        ax1 = lgb.plot_importance(gbm1, color='r', title='t', xlabel='x', ylabel='y')
        self.assertIsInstance(ax1, matplotlib.axes.Axes)
        self.assertEqual(ax1.get_title(), 't')
        self.assertEqual(ax1.get_xlabel(), 'x')
        self.assertEqual(ax1.get_ylabel(), 'y')
        self.assertLessEqual(len(ax1.patches), 30)
        for patch in ax1.patches:
github closest-git / LiteMORT / python-package / case_poct.py View on Github external
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
            y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]

        if isMORT:
            model = LiteMORT(params).fit(X_train, y_train, eval_set=[(X_valid, y_valid)])
            pred_val = model.predict(X_valid)
            pred_raw = model.predict_raw(X_valid)
            y_pred[valid_index] = pred_raw
            fold_score = roc_auc_score(y_valid, pred_raw)
        else:
            lgtrain = lgb.Dataset(X_train, y_train)
            lgval = lgb.Dataset(X_valid, y_valid)
            model = lgb.train(params, lgtrain, num_rounds, valid_sets=lgval,
                              early_stopping_rounds=early_stopping_rounds, verbose_eval=100)
            plt.figure(figsize=(12, 6))
            lgb.plot_importance(model, max_num_features=30)
            plt.title("Featurertances")
            plt.show()

            fold_importance = pd.DataFrame()
            fold_importance["feature"] = X.columns
            fold_importance["importance"] = model.feature_importance()
            fold_importance["fold"] = fold_n + 1
            feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
            model.save_model(f'model_lgb_poct_{fold_n}_.txt')
            pred_val = model.predict(X_valid)
            y_pred[valid_index] = pred_val
            fold_score = roc_auc_score(y_valid, pred_val)

        print("fold n°{} time={:.3g} score={:.4g}".format(fold_n, time.time() - t0, fold_score))
        if test is not None:
            pred_test = model.predict(test, num_iteration=model.best_iteration)
github produvia / kryptos / ml / ml / utils / feature_exploration.py View on Github external
explainer = shap.TreeExplainer(model)
        shap_values = explainer.shap_values(X)
        shap.summary_plot(shap_values, X, plot_type="bar", show=False)
        save_fig(namespace, name, idx, importance_type='shap')

        if name == 'XGBOOST':
            for i in ['weight', 'cover', 'gain']:
                if i == 'gain':
                    xgb.plot_importance(model.get_score(fmap='', importance_type=i), importance_type=i, max_num_features=20)
                else:
                    xgb.plot_importance(model, importance_type=i, max_num_features=20)
                save_fig(namespace, name, idx, importance_type=i)

        elif name == 'LIGHTGBM':
            for i in ['split', 'gain']:
                lgb.plot_importance(model, importance_type=i, max_num_features=20)
                save_fig(namespace, name, idx, importance_type=i)

        else:
            pass
github ybabakhin / kaggle-skeleton / models_zoo.py View on Github external
def feature_importance(self):
        lgb.plot_importance(self.model, max_num_features=10)
        plt.show()
        return self.model.feature_importance()
github microsoft / LightGBM / examples / python-guide / plot_example.py View on Github external
# train
gbm = lgb.train(params,
                lgb_train,
                num_boost_round=100,
                valid_sets=[lgb_train, lgb_test],
                feature_name=['f' + str(i + 1) for i in range(X_train.shape[-1])],
                categorical_feature=[21],
                evals_result=evals_result,
                verbose_eval=10)

print('Plotting metrics recorded during training...')
ax = lgb.plot_metric(evals_result, metric='l1')
plt.show()

print('Plotting feature importances...')
ax = lgb.plot_importance(gbm, max_num_features=10)
plt.show()

print('Plotting split value histogram...')
ax = lgb.plot_split_value_histogram(gbm, feature='f26', bins='auto')
plt.show()

print('Plotting 54th tree...')  # one tree use categorical feature to split
ax = lgb.plot_tree(gbm, tree_index=53, figsize=(15, 15), show_info=['split_gain'])
plt.show()

print('Plotting 54th tree with graphviz...')
graph = lgb.create_tree_digraph(gbm, tree_index=53, name='Tree54')
graph.render(view=True)