可以从哪些模型中获得特征重要性,并给出代码
时间: 2024-10-14 11:05:51 浏览: 22
许多机器学习模型支持内置的特征重要性评估,例如决策树、随机森林、梯度提升机等。下面分别举例说明:
### 1. 决策树(Decision Tree)
```python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=42)
# 使用决策树模型
dtree = DecisionTreeClassifier(random_state=42)
dtree.fit(X_train, y_train)
# 获取特征重要性
importances = dtree.feature_importances_
indices = np.argsort(importances)[::-1]
# 输出特征重要性
print("Feature Importances (in descending order):")
for i in range(X_train.shape[1]):
print(f"Feature {i+1}: {importances[indices[i]]:.2f}")
```
### 2. 随机森林(Random Forest)
```python
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
# 获取特征重要性
feature_importances_rf = rf.feature_importances_
# 打印特征重要性
print("Feature Importance from Random Forest (in descending order):")
for i, v in enumerate(sorted(zip(X_train.columns, feature_importances_rf), key=lambda x: x[1], reverse=True)):
print(f"{v[0]}: {v[1]:.2f}")
```
### 3. 梯度提升机(Gradient Boosting)
```python
from lightgbm import LGBMClassifier
lgbm = LGBMClassifier(random_state=42)
lgbm.fit(X_train, y_train)
# 对于LightGBM,`feature_importances_`属性返回的是增益(gain),不是重要性
importances_lgbm = lgbm.feature_importances_
# 打印特征重要性
print("Feature Importance from LightGBM (in descending order based on gain):")
for i, importance in enumerate(sorted(zip(X_train.columns, importances_lgbm), key=lambda x: x[1], reverse=True)):
print(f"{importance[0]}: {importance[1]:.2f}")
```
阅读全文