svm_sklearn


sklearn SVM 支持向量机

2018-01-22


In [1]:
from sklearn import svm

分类svc


In [6]:
x = [[0,0], [1,1], [1,0]]
y = [0,1,1]

In [7]:
clf = svm.SVC()
clf.fit(x,y)


Out[7]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

In [12]:
z = [2,2]
result = clf.predict([[2,2]])
print(result)


[1]

In [14]:
print(clf.support_vectors_)
print(clf.support_)
print(clf.n_support_)


[[ 0.  0.]
 [ 1.  1.]
 [ 1.  0.]]
[0 1 2]
[1 2]

回归


In [15]:
x = [[0, 0], [1, 1]]  
y = [0.5, 1.5]   
clf = svm.SVR()   
clf.fit(x, y)


Out[15]:
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
  kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)

In [17]:
z = [[2,2]]
result = clf.predict([[2, 2]])   
print(result)


[ 1.22120072]

In [ ]: