仅用一行代码:
适合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')
结果将是:
0
仅用一行代码:
适合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')
结果将是:
0
是的,SVM分类器具有属性coef_
,但仅适用于具有线性内核的 SVM。对于其他内核,这是不可能的,因为数据是通过内核方法转换到与输入空间无关的另一个空间的,请查看说明 。
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)
函数的输出如下所示:
0
我基于Jakub Macina的代码段创建了一个也适用于Python 3的解决方案。
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)
0
我有一个数据集,我想在该数据上训练我的模型。训练后,我需要了解SVM分类器分类的主要贡献者。
森林算法有一个叫做功能重要性的东西,有没有类似的东西?