資源簡介
支持向量機SVM python源代碼 親測可在pycharm用,可以用于統計學習方法的課后練習使用
代碼片段和文件信息
import?numpy?as?np
import?pylab?as?pl
from?sklearn?import?svm
#?we?create?40?separable?points
X?=?np.r_[np.random.randn(20?2)?-?[2?2]?np.random.randn(20?2)?+?[2?2]]
Y?=?[0]*20?+[1]*20
#fit?the?model
clf?=?svm.SVC(kernel=‘linear‘)
clf.fit(X?Y)
#?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])
print?“w:?“?w
print?“a:?“?a
#?print?“xx:?“
評論
共有 條評論