私はLinearSVCとsklearnで訓練したモデルの超平面をプロットしようとしています。私は自然言語で作業しています。モデルをフィッティングする前に、CountVectorizerとTfidfTransformerでフィーチャーを抽出しました。Plot hyperplane Linear SVM python
ここで分類器:
from sklearn.svm import LinearSVC
from sklearn import svm
clf = LinearSVC(C=0.2).fit(X_train_tf, y_train)
その後、私はon the Scikit-learn website提案としてプロットしてみました:
# get the separating hyperplane
w = clf.coef_[0]
a = -w[0]/w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf.intercept_[0])/w[1]
# plot the parallels to the separating hyperplane that pass through the
# support vectors
b = clf.support_vectors_[0]
yy_down = a * xx + (b[1] - a * b[0])
b = clf.support_vectors_[-1]
yy_up = a * xx + (b[1] - a * b[0])
# plot the line, the points, and the nearest vectors to the plane
plt.plot(xx, yy, 'k-')
plt.plot(xx, yy_down, 'k--')
plt.plot(xx, yy_up, 'k--')
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
s=80, facecolors='none')
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.axis('tight')
plt.show()
この例では、svm.SVC(カーネル= 'リニア')を使用して、私の分類器はLinearSVCありながら、 。したがって、私はこのエラーを取得する:
AttributeError Traceback (most recent call last)
<ipython-input-39-6e231c530d87> in <module>()
7 # plot the parallels to the separating hyperplane that pass through the
8 # support vectors
----> 9 b = clf.support_vectors_[0]
1 yy_down = a * xx + (b[1] - a * b[0])
11 b = clf.support_vectors_[-1]
AttributeError: 'LinearSVC' object has no attribute 'support_vectors_'
どのように私は正常に私のLinearSVC分類器のhyperplanをプロットすることができますか?
ここにある:私はその一例では、彼らは、線形SVMのための別のカーネルを使用することを知ってhttp://scikit-learn.org/0.18/auto_examples/svm/plot_separating_hyperplane.html – Alex
が、私はクラシファイアをプロットしたい – Alex
エラーの理由を探しているような質問が表示されますが、ドキュメンテーションに明示的に記載されているように:* "LinearSVCはキーワードカーネルを受け入れないことに注意してください。また、SVCやNuSVCのメンバーの中には、「support_」のようなものもありません」* LinearSVCの使い方についてはあまりよく分かりませんが、同様の出力が必要なことを明確にするために質問を言い換えることができます'SVC'を使用しますが、代わりに' 'support_vectors_''属性を持たない' 'LinearSVC''を使用してください。このように人々は明らかにするために深く掘る必要はありません。 – ImportanceOfBeingErnest