【问题标题】:Finding The Most Relevant or Important Features for SVM using SGD (loss=hinge)使用 SGD (loss=hinge) 为 SVM 寻找最相关或最重要的特征
【发布时间】:2019-09-25 08:47:16
【问题描述】:

我正在研究一个文本分类问题,发现 SVM 在我的文本分类问题上表现最好。但是,我使用 sklearn 的 SGD 分类器 (loss=hinge) 进行了实验。

LIME 似乎提供了一种分析实例并显示给定实例的每个类的分析的方法。然而,LIME 的问题是:

exp = explainer.explain_instance(test_document, c.predict_proba)

在explainer.explain_instance 函数中,它需要一个概率分布作为第二个参数(c.predict_proba),并且没有可用于SGD 的predict_proba (loss=hinge)。我曾尝试使用 CalibratedClassifierCV,但我得到了不同的结果。

'=======SGD (loss=hinge)======='
Predicted:  [19, 7, 7, 13, 16, 9, 17, 6, 13, 17, 16, 17, 11, 1, 4, 14, 8, 10, 12, 10, 16, 1, 1]
True     :  [19, 3, 7, 13, 16, 9, 15, 6, 13, 10, 16, 17, 11, 1, 4, 14, 8, 5, 20, 18, 2, 12, 1]
0.6521739130434783
'====Calibrated Results======'
Predicted:  [19, 7, 7, 13, 16, 16, 12, 6, 13, 17, 16, 17, 11, 5, 4, 14, 8, 10, 15, 10, 16, 7, 1]
True     :  [19, 3, 7, 13, 16, 9, 15, 6, 13, 10, 16, 17, 11, 1, 4, 14, 8, 5, 20, 18, 2, 12, 1]
0.5652173913043478

这里的目标是在多类文本分类问题中为每个类找到最相关/最重要的特征。这样做的目的是分析结果并在研究论文中讨论它们的差异。 接受任何建议或替代方案。

另外,我可以使用 sklearn 的 SVC 代替 SGD,因为它具有 predict_proba 属性。但是,问题在于 SGD,我已经有一组要分析的参数,但我找不到将 SGD 的配置准确转换为 SVC 配置的方法。

【问题讨论】:

  • 出于兴趣,您在 CalibratedClassifierCV 中使用了多少次折叠?
  • 我将其保留为默认值 (cv=3)。我的数据集很小,因此不能超过 5

标签: python machine-learning scikit-learn text-classification sklearn-pandas


【解决方案1】:

所以我无法让 LIME 工作,但我找到了 LIME 的替代品,称为 ELI5 一个 python 的库。我将从教程网站重新创建一个示例,说明如何使用该模块来调试机器学习算法。

假设您已经有一个适合矢量化文本数据的 sklearn 的 clf 或估计器对象:

import eli5
eli5.show_weights(clf, top=10)

上面的代码有效,但更好的方法是提供vectorizer,让eli5自动计算细节:

#vec is the vectorizer object and 
#target_names is the names of the classes to be used
eli5.show_weights(clf, vec=vec, top=10,
                  target_names=twenty_test.target_names)

查看分类器如何分配特定类别 show_prediction 显示每个类别的分数以及对该分数做出贡献的顶级单词

#second param is the test instance whose prediction you'd like to see.
#vec and target_names are same from show_weights 
eli5.show_prediction(clf, twenty_test.data[0], vec=vec,
                     target_names=twenty_test.target_names)

以下只是通过show_prediction方法产生的整个输出的一部分。

这正是我要找的,我想知道为什么我以前没有找到它。希望它可以帮助其他人寻找类似的东西。

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 2019-03-09
    • 2016-06-29
    • 2015-06-24
    • 2017-07-19
    • 2017-12-02
    • 2019-11-26
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多