0

因此,我将 SVM 一类分类器 ( https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html ) 用于异常检测项目。

该任务显示了一些有趣的准确性和 F1 分数,所以我想看看功能的重要性,但我对此有一些问题。我知道对于线性内核,我可以使用该.coef_属性,但我正在使用rbf内核,所以这不起作用。

我尝试通过以下方式使用sklearn.inspection.permutation_importancehttps://scikit-learn.org/stable/modules/permutation_importance.html ):

from sklearn.inspection import permutation_importance

perm_importance = permutation_importance(clf, test, train, scoring='f1')
sorted_idx = perm_importance.importances_mean.argsort()
features = train.columns.values
plt.barh(features[sorted_idx], perm_importance.importances_mean[sorted_idx])
plt.xlabel("Permutation Importance")

但我收到以下错误:

ValueError: Classification metrics can't handle a mix of continuous-multioutput and binary targets

我还查看了 SHAP(https://shap.readthedocs.io/en/latest/index.html),但我很难使用它。

如果还有其他方法可以获取功能重要性,请告诉我。任何意见将不胜感激。

4

1 回答 1

0

我最终能够使用 SHAP。下面是我的代码片段。我使用了错误的解释器。大部分代码来自文档中的示例:https ://shap-lrjball.readthedocs.io/en/latest/example_notebooks/kernel_explainer/Census%20income%20classification%20with%20scikit-learn.html

from sklearn.svm import OneClassSVM

clf = OneClassSVM(nu=0.25)

med = train.median().values.reshape((1,train.shape[1]))
f = lambda x: clf.fit_predict(x)

explainer = shap.KernelExplainer(clf.fit_predict, med)
shap_values = explainer.shap_values(test)
shap.summary_plot(shap_values, test)
于 2022-02-15T12:24:35.150 回答