文章目录
一、对鸢尾花数据集进行SVM线性分类
1、导入需要使用的包并获取数据
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
iris=datasets.load_iris()
X=iris.data
Y=iris.target
对数据进行处理
X=X[:,:2]
Y1=Y[Y<2]
y1=len(Y1)
Y2=Y[Y<1]
y2=len(Y2)
X=X[:y1,:2]
plt.scatter(X[0:y2,0],X[0:y2,1],color='red')
plt.scatter(X[y2+1:y1,0],X[y2+1:y1,1],color='blue')
plt.show()
2、数据归一化处理
standardScaler=StandardScaler()
standardScaler.fit(X)
X_standard=standardScaler.transform(X)
svc=LinearSVC(C=1e9)
svc.fit(X_standard,Y1)
3、画出决策边
def plot_decision_boundary(model, axis):
x0, x1 = np.meshgrid