|
@@ -164,8 +164,23 @@ class LightGBM(object):
|
|
|
print(f"Accuracy: {accuracy}")
|
|
|
fw.close()
|
|
|
|
|
|
+ def feature_importance(self):
|
|
|
+ """
|
|
|
+ Get the importance of each feature
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ lgb_model = lgb.Booster(model_file=self.model)
|
|
|
+ importance = lgb_model.feature_importance(importance_type='split')
|
|
|
+ feature_name = lgb_model.feature_name()
|
|
|
+ feature_importance = sorted(zip(feature_name, importance), key=lambda x: x[1], reverse=True)
|
|
|
+
|
|
|
+ # 打印特征重要性
|
|
|
+ for name, imp in feature_importance:
|
|
|
+ print(name, imp)
|
|
|
+
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
L = LightGBM()
|
|
|
# L.train_model()
|
|
|
- L.evaluate_model()
|
|
|
+ # L.evaluate_model()
|
|
|
+ L.feature_importance()
|