【问题标题】:Determining the most contributing features for SVM classifier in sklearn在 sklearn 中确定 SVM 分类器的最有贡献的特征
【发布时间】:2017-05-26 08:22:59
【问题描述】:

我有一个数据集,我想根据该数据训练我的模型。训练后,我需要知道对 SVM 分类器的分类有主要贡献的特征。

森林算法有一个叫做特征重要性的东西,有什么类似的吗?

【问题讨论】:

标签: python machine-learning scikit-learn svm


【解决方案1】:

如果您正在使用 rbf(径向基函数)内核,您可以使用sklearn.inspection.permutation_importance 来获取特征重要性,如下所示。 [doc]

from sklearn.inspection import permutation_importance
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

svc =  SVC(kernel='rbf', C=2)
svc.fit(X_train, y_train)

perm_importance = permutation_importance(svc, X_test, y_test)

feature_names = ['feature1', 'feature2', 'feature3', ...... ]
features = np.array(feature_names)

sorted_idx = perm_importance.importances_mean.argsort()
plt.barh(features[sorted_idx], perm_importance.importances_mean[sorted_idx])
plt.xlabel("Permutation Importance")

【讨论】:

    【解决方案2】:

    我创建了一个同样适用于 Python 3 的解决方案,它基于 Jakub Macina 的代码 sn-p。

    from matplotlib import pyplot as plt
    from sklearn import svm
    
    def f_importances(coef, names, top=-1):
        imp = coef
        imp, names = zip(*sorted(list(zip(imp, names))))
    
        # Show all features
        if top == -1:
            top = len(names)
    
        plt.barh(range(top), imp[::-1][0:top], align='center')
        plt.yticks(range(top), names[::-1][0:top])
        plt.show()
    
    # whatever your features are called
    features_names = ['input1', 'input2', ...] 
    svm = svm.SVC(kernel='linear')
    svm.fit(X_train, y_train)
    
    # Specify your top n features you want to visualize.
    # You can also discard the abs() function 
    # if you are interested in negative contribution of features
    f_importances(abs(clf.coef_[0]), feature_names, top=10)
    

    【讨论】:

    • 你的回答是好的,但它是相反的。您正在绘制前 10 个最差的特征。
    【解决方案3】:

    只有一行代码:

    拟合一个 SVM 模型:

    from sklearn import svm
    svm = svm.SVC(gamma=0.001, C=100., kernel = 'linear')
    

    并按如下方式实现情节:

    pd.Series(abs(svm.coef_[0]), index=features.columns).nlargest(10).plot(kind='barh')
    

    结果是:

    the most contributing features of the SVM model in absolute values

    【讨论】:

      【解决方案4】:

      是的,SVM 分类器有 coef_ 属性,但它只适用于具有 线性内核 的 SVM。对于其他内核是不可能的,因为数据是通过内核方法转换到另一个空间的,与输入空间无关,请查看explanation

      from matplotlib import pyplot as plt
      from sklearn import svm
      
      def f_importances(coef, names):
          imp = coef
          imp,names = zip(*sorted(zip(imp,names)))
          plt.barh(range(len(names)), imp, align='center')
          plt.yticks(range(len(names)), names)
          plt.show()
      
      features_names = ['input1', 'input2']
      svm = svm.SVC(kernel='linear')
      svm.fit(X, Y)
      f_importances(svm.coef_, features_names)
      

      函数的输出如下所示:

      【讨论】:

      • 如何找到除线性之外的内核的特征重要性,如果你能发布相同的答案会很棒
      • 我更新了答案,非线性内核是不可能的。
      • 负面影响很大的权重呢?
      • 对于更多通用案例并查看效果(在相同情况下为负面效果),您可以查看此 [问题](stackoverflow.com/a/49937090/7127519)
      • 我收到错误The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 知道如何解决这个问题吗?
      猜你喜欢
      • 1970-01-01
      • 2020-03-14
      • 2017-06-24
      • 2020-05-19
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 2011-08-15
      • 2019-01-04
      相关资源
      最近更新 更多