Sklearn --- 支持向量机

一、软间隔支持向量机

软间隔支持向量机的优化目标:

\min \limits_{w,b} \frac{1}{2}\left | \left | w \right | \right |^2+C \sum_{i=1}^{m}l_{0/1}(y_i(w^Tx_i+b)-1)

其中l_{0/1}可以用下列损失函数替代

hinge损失:l_{hinge}(z)=max(0,1-z)

指数损失:l_{exp}(z)=exp(-z)

对率损失:l_{log}(z)=log(1+exp(-z))

import numpy as np
from sklearn import datasets
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC

# 读取数据集
iris = datasets.load_iris()
x = iris["data"][:, (2,3)]
y = (iris["target"] == 2).astype(np.float64)

# SVM参数列表
svm_clf = Pipeline((
    # 参数标准化
    ("scaler", StandardScaler()),
    # 线性SVC,惩罚参数C=1,使用hinge损失函数
    ("linear_svc", LinearSVC(C = 1, loss = 'hinge')),
    ))

# 拟合向量机
svm_clf.fit(x, y)

# 使用训练好的向量机进行预测
svm_clf.predict([[5.5,1.7]])

 

二、多项式特征构造

举个例子就是,假设有a、b两个特征,当degree=2时,其二次多项式为:(1,a,b,a^{2},ab,b^{2}})

from sklearn.datasets import make_moons
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures

polynomial_svm_clf = Pipeline((
    # 构造多项式特征,degree表示多项式次数
    ("poly_features", PolynomialFeatures(degree = 3)),
    ("scaler", StandardScaler()),
    ("svm_clf", LinearSVC(C = 10, loss = "hinge"))
))

polynomial_svm_clf.fit(x, y)

 

三、使用多项式核

from sklearn.svm import SVC


poly_kernel_svm_clf = Pipeline((
    ("scaler", StandardScaler()),
    # 使用多项式核,degree表示深度,coef表系数,惩罚系数C设置为5
    ("svm_clf", SVC(kernel = "poly", degree = 3, coef0 = 1, C = 5))))

poly_kernel_svm_clf.fit(x,y)

 

四、高斯核函数

rbf_kernel_svm_clf = Pipeline((
    ("scaler", StandardScaler()),
    # 使用高斯核函数,这里的gamma是高斯核公式中的带宽
    ("svm_clf", SVC(kernel = "rbf", gamma = 5, C = 0.001))
))

 

五、支持向量机回归

支持向量机优化目标:

\min \limits_{w,b} \frac{1}{2}\left | \left | w \right | \right |^2+C \sum_{i=1}^{m}l_{\epsilon }(f(x_i)-y_i)

from sklearn.svm import SVR
# 使用多项式核,其中epsilon在向量机理论中表示对训练样本的容忍度
svm_poly_reg = SVR(kernel = 'poly', degree = 2, C = 100, epsilon = 0.1)
svm_poly_reg.fit(x, y)

 

参考文献:

《Hands-ON Machine Learning with Scikit-Learn & TensorFlow》

周志华的《机器学习》

 

你可能感兴趣的:(Sklearn --- 支持向量机)