支持向量机(SVM)代码实现
支持向量机是一个重要的分类算法,在上一篇文章中简单地介绍了它的一些理论知识,这篇文章是简单讲一下如何用python训练一个svm模型
导入所需的数据库
import numpy as np
from sklearn import datasets
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from sklearn.datasets import make_moons
from sklearn.svm import SVC
简单地定义一个线性svm的函数
# 线性SVM
def linearsvm():
"""LinearSVC会对偏置项进行正则化,所以需要先减去平均值,使训练集集中"""
iris = datasets.load_iris()
X = iris["data"][:, (2,3)]
Y = (iris["target"] == 2).astype(np.float64)
# iris["target"] == 2输出布尔值,astype()函数输出的是0,1的数值
svm_clf = Pipeline((("scaler", StandardScaler()),
("linear_svc",LinearSVC(C = 1, loss = "hinge")),))
# C是SVM类中的超参数,C值越小,间隔越宽,但是间隔违例也会越多
svm = svm_clf.fit(X, Y)
y_pred = svm.predict([[5.5, 1.7]])
print(y_pred)
定义一个多项式svm
def polysvm():
data = make_moons()
x = data[0]
y = data[1]
poly_kernel_svm_clf = Pipeline((("scaler", StandardScaler()), ("svm_clf", SVC(kernel = "poly", degree = 3, coef0 = 1, C = 5)),))
# coef0控制模型受高阶多项式或者低阶多