首先引用相关库
import sklearn
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from sklearn import svm
加载数据集
breast = load_breast_cancer(return_X_y=True)
x,y = breast
x.shape
是一个维度为(569,30)的数据
在不进行数据归一化的前提下
#分割数据集
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)
kernel = svm.SVC()
#进行训练
kernel.fit(x_train,y_train)
y_predict = kernel.predict(x_test)
print(y_predict)
print(y_test)
结果如下 [1 1 1 1 0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 0 0 0 1 0 0 1 0 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0] [1 1 1 1 0 0 0 0 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 1 0 1 0 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 0 1 0 1 1 0]
#计算准确率
j = 0
for i in range(len(y_test)):
if(y_predict[i] == y_test[i]):
j = j + 1
rate = j / len(y_test)
print(rate)
0.9122807017543859
进行数据归一化
#数据标准化
ss = StandardScaler()
ss_x_train = x_train
ss_x_test = x_test
ss_x_train = ss.fit_transform(ss_x_train)
ss_x_test = ss.fit_transform(ss_x_test)
#进行训练
kernel.fit(ss_x_train,y_train)
y_predict = kernel.predict(ss_x_test)
print(y_predict)
print(y_test)
结果如下文章来源:https://uudwc.com/A/Y6gqP
1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0 1 1 1] [0 1 0 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 0 0 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0 1 1 1]
#计算准确率
j = 0
for i in range(len(y_test)):
if(y_predict[i] == y_test[i]):
j = j + 1
rate = j / len(y_test)
print(rate)
0.9824561403508771
可以发现,当进行了数据归一化之后,准确率高了不少,文章来源地址https://uudwc.com/A/Y6gqP