You can download this code by clicking the button below.
This code is now available for download.
The code uses the LightGBM library to extract feature importance from a trained model. It also uses NumPy for numerical operations.
Technology Stack : The code uses the LightGBM library for machine learning, and NumPy for numerical computations.
Code Type : The type of code
Code Difficulty :
def random_feature_importance(lgbm_model):
"""
This function extracts and returns the feature importance from a trained LightGBM model.
"""
import lightgbm as lgbm
import numpy as np
def get_feature_importance(model):
# Extract feature importance from the model
importance = model.feature_importances_
# Normalize the feature importance
importance = (importance - np.min(importance)) / (np.max(importance) - np.min(importance))
# Sort the feature importance in descending order
sorted_idx = np.argsort(importance)[::-1]
return sorted_idx, importance[sorted_idx]
# Check if the input is a trained LightGBM model
if not isinstance(lgbm_model, lgbm.Booster):
raise ValueError("The input must be a trained LightGBM model.")
# Get the feature importance
sorted_idx, importance = get_feature_importance(lgbm_model)
return sorted_idx, importance