python--svm

# -*- coding: utf-8 -*-
from sklearn import svm
from sklearn import datasets
import numpy as np

iris = datasets.load_iris() #载入鸢尾数据
x = iris.data[0:150]
y = iris.target[0:150]

clf = svm.SVC() #定义
print clf.fit(x,y) #训练
print clf.support_vectors_ #支持向量
print clf.support_ #支持向量的下标
print clf.n_support_ #每类的支持向量个数
print clf.predict([[5,4,1,0.5], [6,3,5,1], [8,4,6,2]]) #预测

线性核:

svc = svm.SVC(kernel='linear')

多项式核:

svc = svm.SVC(kernel='poly', degree=3)

径向基核:

svc = svm.SVC(kernel='rbf')

默认为径向基核, 也可以自定义核:

def my_kernel(x,y):
    return np.dot(x, y.T)
clf = svm.SVC(kernel=my_kernel)

还有precomputed核,见http://scikit-learn.org/stable/modules/svm.html的1.2.6.1.2

显示核:

print clf.kernel

参数:

print clf.intercept_
print clf.dual_coef_

SVC为分类, LinearSVC也为分类. 区别在于多分类时采用one-vs-one还是one-vs-rest.

SVR为回归

原文地址:https://www.cnblogs.com/saieuler/p/3366746.html