Permutation Importance:排列重要性

Permutation Importance 特点

Permutation Importance 提供了一个和模型无关的计算特征重要性的方法,他是在模型训练之后,对特征重要性进行计算

Permutation Importance 排列重要性 计算思路:

  1. 选择一个特征
  2. 在数据集上对该特征的所有值进行随机排列
  3. 利用模型计算新的预测结果
  4. 比较新旧结果,如果新旧结果的差异不那么大,说明该特征对模型的影响较小,即该特征的重要性较低;如果新旧结果的差异性显著,说明该特征对模型的影响较大,即该特征的重要性较大

例子 :数据集 :breast_cancer 数据集: 一共有30个特征,569条数据集

from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForesrClassifier
from sklearn.model_selection import train_test_split
from sklearn.inspection import permutation_importance
import matplotlib.pyplot as plt

cancer = load_breast_cancer() #导入数据
X_train,X_test,Y_train,Y_test = train_test_split(cancer.data,cancer.target,random_state =100)
rf = RandomForestClassfier(n_estimators=100,random_state=1)
rf.fit(X_train,Y_train)
baseline = rf.score(X_test,Y_test)
result = permutation_importance(rf,X_test,Y_test,n_repeats=10,random_state=1,scoring='accuracy')
importances = result.importances_mean
# 画图
plt.bar(range(len(importances)), importances)
plt.xlabel('Feature Index')
plt.ylabel('Permutation Importance')
plt.show()
``

## 运行结果
![重要性下降的越多,说明该特征越重要](https://img-blog.csdnimg.cn/a485ed8c1fc44dfba204959176b21d24.jpeg#pic_center)


Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐