Browse Source

feature_importance

罗俊辉 1 year ago
parent
commit
03f8b0782d
1 changed files with 16 additions and 1 deletions
  1. 16 1
      main.py

+ 16 - 1
main.py

@@ -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()