一、原理部分:
依然是图片~
二、sklearn实现:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
from sklearn.datasets import load_digits
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler #载入数据
digits = load_digits()
x_data = digits.data
y_data = digits.target
x_train,x_test,y_train,y_test = train_test_split(x_data,y_data)
st = StandardScaler()
x_train_st = st.fit_transform(x_train)
x_test_st = st.fit_transform(x_test) #训练模型
svc = SVC(kernel='rbf')
model_svc = GridSearchCV(svc,param_grid=({'C':[7,8,9],'gamma':[0.01,0.02]}))
model_svc.fit(x_train_st,y_train)
print(model_svc.best_params_)
print(model_svc.best_score_)
y_hat = model_svc.predict(x_test_st)
print(accuracy_score(y_hat,y_test))
{'gamma': 0.01, 'C': 7}
0.978470675575
0.982222222222
感觉SVM调参要很注意,如果过低说明你很可能过拟合了!可以先给一个比较大范围的,然后逐步缩小