Skip to main content

Evaluate feature importance for LightGBM models using various methods.

Project description

LightGBM Feature Importance Evaluator

LightGBM Feature Importance Evaluator provides advanced tools to analyze and evaluate feature importance in LightGBM models using various methods, including traditional gains, SHAP values, and more. The package also includes enhanced preprocessing and feature filtering utilities.


Key Features in v0.1.1

  • New Importance Method: abs_shap - Computes mean absolute SHAP values for improved insights.
  • Enhanced Preprocessing: Streamlined data filtering by date and handling of missing values via preprocess_data.
  • Feature Selection Utility: Quickly filter features using filter_features based on importance thresholds.
  • Improved Performance: Optimized functions for better runtime efficiency and usability.
  • Additional Parameters: Support for verbosity and early stopping in LightGBM model training.

Key Features

  • LightGBM-Specific Importance Methods

    • Gain-based importance
    • Split-based importance
  • Model-Agnostic Methods

    • Permutation importance
    • Drop-column importance
  • SHAP-Based Interpretability

    • Local and global explanations using SHAP values
  • Cross-Validation Support

    • Robust feature importance evaluation using stratified k-fold cross-validation
  • Feature Filtering

    • Select important features based on a user-defined threshold

Installation

Install the package directly from PyPI:

pip install lightgbm_feature_importance_evaluator_zhoumath

Usage

Here's how to use lightgbm_feature_importance_evaluator_zhoumath step by step:

1. Import the Package

from lightgbm_feature_importance_evaluator_zhoumath.evaluator import FeatureImportanceEvaluator
from lightgbm import LGBMClassifier
import pandas as pd

2. Prepare Your Dataset

# Sample dataset
data = pd.DataFrame({
    "feature1": [1, 2, 3, 4, 5],
    "feature2": [5, 4, 3, 2, 1],
    "feature3": [2, 3, 4, 5, 6],
    "target": [0, 1, 0, 1, 0],
    "timestamp": ["2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-01-05"]
})

3. Initialize the Evaluator

evaluator = FeatureImportanceEvaluator(
    data=data,
    target_column="target",
    timestamp_column="timestamp",
    feature_columns=feature_columns,
    model=model,
    importance_method="abs_shap",  # Use the new `abs_shap` method
    start_date="2023-01-01",       # Optional: Filter by start date
    end_date="2023-12-31",         # Optional: Filter by end date
    missing_values=["NA", None],   # Handle missing values
    verbose=True,                  # Print logs during processing
    num_boost_round=1000,          # Number of boosting rounds
    early_stopping_rounds=50,      # Early stopping patience
)

4. Evaluate Feature Importance

importance_df = evaluator.evaluate_importance()
print("Feature Importance:")
print(importance_df)

5. Filter Features

selected_features, sorted_importance = evaluator.get_filtered_features(importance_df)
print("Selected Features:")
print(selected_features)

Available Importance Methods

  • gain: Importance based on the gain (performance improvement) when a feature is used for splitting.
  • split: Importance based on the frequency a feature is used for splitting.
  • permutation: Model-agnostic importance based on the drop in performance when a feature is randomly shuffled.
  • shap: Uses SHAP values to explain the contribution of each feature to predictions.
  • drop_column: Measures the change in model performance when a feature is entirely removed from the dataset.

Customization Options

Cross-Validation

  • Adjust the number of splits using n_splits.
  • Repeat cross-validation multiple times using repeats.

Importance Threshold

  • Filter features with importance above a specific threshold using importance_threshold.

Date Filtering

  • Use start_date and end_date to filter data based on a time range.

Dependencies

The package requires the following Python libraries:

  • numpy
  • pandas
  • scikit-learn
  • shap
  • lightgbm

Install them with:

pip install -r requirements.txt

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request.

Acknowledgments

This package was developed to streamline the evaluation of feature importance in LightGBM models and simplify workflows for data scientists and machine learning practitioners.

Author

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file lightgbm_feature_importance_evaluator_zhoumath-0.1.1.tar.gz.

File metadata

File hashes

Hashes for lightgbm_feature_importance_evaluator_zhoumath-0.1.1.tar.gz
Algorithm Hash digest
SHA256 72201b2ff7de2fc5e56a18261e5b48e43e7b8687dedb211fa74aaf3eb22a2012
MD5 95c9bd075160f050af9f88081ad06f01
BLAKE2b-256 d8ba125c6930c19af6fa5af5d5e2a0bc90a3149bd0fa7f0c1b056b983761e5e7

See more details on using hashes here.

File details

Details for the file lightgbm_feature_importance_evaluator_zhoumath-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lightgbm_feature_importance_evaluator_zhoumath-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b2375602623213baf7794a75ec6099954f5e0ec55b00a8edeb000960e97d005a
MD5 c3eaf007070f02567a74bbe237872cb2
BLAKE2b-256 084c2ccb6ae5a2799a960c09e08cefcab860c00fa8192a419e581eb581ce10c4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page