1 导包
import numpy as np
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
2 加载数据
X,y=datasets.load_wine(return_X_y=True)
display(X.shape)
输出:
(178, 13)
x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.2)
display(x_train.shape,y_train.shape)
输出:
(142, 13)
(142,)
3 建模
3.1 线性:linear
svc=SVC(kernel='linear')
svc.fit(x_train,y_train)
yred=svc.predict(x_test)
score=accuracy_score(yred,y_test)
display('使用linear核函数得分:',score)
输出:
'使用linear核函数得分:'
0.9444444444444444
svc.coef_
输出:
array([[ 0.68238177, 0.59090752, 1.17127315, -0.21150042, 0.0081547 ,
-0.01397723, 0.2235816 , 0.22634936, -0.41167559, 0.47889006,
-0.16003878, 0.50632237, 0.00469413],
[ 0.08796641, 0.13272108, 0.04146849, -0.07352927, 0.00631995,
0.28416506, 0.43334105, 0.00655127, 0.18678886, -0.26506095,
0.03456889, 0.29852352, 0.00403859],
[-0.6507222 , -0.33430121, -0.36136551, -0.02392738, -0.02279356,
0.37705139, 1.19788242, 0.28111838, 0.54743024, -0.61747287,
0.43714574, 0.94564