I fit the basic LGBM model in Python.
# Create an instance
LGBM = LGBMRegressor(random_state = 123, importance_type = 'gain') # `split` can be also selected here
# Fit the model (subset of data)
LGBM.fit(X_train_subset, y_train_subset)
# Predict y_pred
y_pred = LGBM.predict(X_test)
I am looking at documentation:
importance_type (string, optional (default="split")) – How the importance is calculated. If “split”, result contains numbers of times the feature is used in a model. If “gain”, result contains total gains of splits which use the feature.
I used gain and it prints me the total gains.
# Print features by importantce
pd.DataFrame([X_train.columns, LGBM.feature_importances_]).T.sort_values([1], ascending = [True])
0 1
59 SLG_avg_p 0
4 PA_avg 2995.8
0 home 5198.55
26 next_home 11824.2
67 first_time_pitcher 15042.1
etc
I tried:
# get importance
importance = LGBM.feature_importances_
# summarize feature importance
for i, v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
plt.bar([x for x in range(len(importance))], importance)
plt.show()
And receive values and plot:
Feature: 0, Score: 5198.55005
Feature: 1, Score: 20688.87198
Feature: 2, Score: 49147.90228
Feature: 3, Score: 71734.03088
etc
I also tried:
# feature importance
print(LGBM.feature_importances_)
# plot
plt.bar(range(len(LGBM.feature_importances_)), LGBM.feature_importances_)
plt.show()
How to print the percentage in this model? For some reason I was sure they calculate it automatically.

