zoukankan      html  css  js  c++  java
  • 特征重要性之排列重要性Permutaion Importance

    基于模型刷选特征方法有:排列重要性、shap value、null importance

    这里简单介绍一下排列重要性:

    一、排列重要性原理

    首先建立一个模型,计算某列特征重要性时,打乱该列顺序,其余列不变,然后再使用打乱后的数据来预测,最后计算正确率;如果某列对模型预测很重要,那么打乱该列顺序之后,模型预测正确率就会很差,如果对预测结果没有影响,则说明该变量对模型没有那么重要;为了消减随机对结果的影响,我们会多次乱打,再求均值和方差。

    二、排列重要性的好处

    1. 计算速度快
    2. 应用广泛、易于理解
    3. 与我们期望一个特征重要性度量所具有的性质一致

    三、排列重要性如何使用Python实现

    1.可以直接eli5库计算和展示排列重要性

    # -*- coding: utf-8 -*-
    """
    Created on Sun Sep 26 15:51:26 2021
    
    @author: chenguimei
    """
    
    # 引入数据
    from sklearn import datasets
    import pandas as pd
    import numpy as np
    
    iris = datasets.load_iris()
    X = pd.DataFrame(iris.data)
    X.columns = iris.feature_names
    y = iris.target
    print("Class labels:",np.unique(y))  #打印分类类别的种类
    
    
    # 切分训练数据和测试数据
    from sklearn.model_selection import train_test_split
    ## 30%测试数据,70%训练数据,stratify=y表示训练数据和测试数据具有相同的类别比例
    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=1,stratify=y)
    
    
    #决策树模型  
    from sklearn.tree import DecisionTreeClassifier
    tree = DecisionTreeClassifier(criterion='gini',max_depth=4,random_state=1)
    tree.fit(X_train,y_train)
    print(X.columns,tree.feature_importances_)
    
    from sklearn.metrics import roc_curve, auc
    resu = tree.predict(X_test)
    print(resu)
    print(y_test)
    
    
    import eli5
    from eli5.sklearn import PermutationImportance
    
    perm = PermutationImportance(lr, random_state=1).fit(X_test, y_test)
    eli5.show_weights(perm, feature_names = X_test.columns.tolist())

     2.sklearn.inspection._permutation_importance 的permutation_importance

    from sklearn.inspection._permutation_importance import permutation_importance
    from sklearn.datasets import load_iris
    from sklearn.metrics import get_scorer
    from sklearn.linear_model import LogisticRegression
    
    
    permutation_importance(tree, X_test, y_test, get_scorer('accuracy'))

    文章强调2点:(1)打乱顺序;(2)使用准确率指标衡量

  • 相关阅读:
    SDUST OJ 时间类的加、减法赋值运算
    POJ 2823 (滑动窗口)
    POJ 2229 计数DP
    POJ 1995 (快速幂)
    poj 3009 (深搜求最短路)
    C++ 学习笔记之 STL 队列
    C++ 学习笔记之 引用
    Anaconda3使用
    Ubuntu 18.04安装Conda、Jupyter Notebook、Anaconda
    Ubuntu 18.04安装 pyenv、pyenv-virtualenv、virtualenv、Numpy、SciPy、Pillow、Matplotlib
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/15338891.html
Copyright © 2011-2022 走看看