对鸢尾花lris数据集进行SVM线性分类

一、创建虚拟环境

  • 命令行创建
    在这里插入图片描述
  • 输入命令
conda create -n name python=x.x

在这里插入图片描述

  • 激活虚拟环境
conda active luy
  • 安装需要的包
pip install 包名  //这里安装的包包括numpy,pandas,sklearn,matplotlib

二、对鸢尾花lris数据集进行SVM线性分类

(一)LinearSVC方式实现分类

代码如下

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()

结果如下
在这里插入图片描述

  • 数据归一化处理,C=1e9
standardScaler=StandardScaler()
standardScaler.fit(X)
X_standard=standardScaler.transform(X)
svc=LinearSVC(C=1e9)
svc.fit(X_standard,Y1)
  • 画出决策边界

代码

def plot_decision_boundary(model,axis):
    x0,x1=np.meshgrid(
        np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*100)).reshape(-1,1),
        np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*100)).reshape(-1,1),
    )
    X_new=np.c_[x0.ravel(),x1.ravel()]
    y_predict=model.predict(X_new)
    zz=y_predict.reshape(x0.shape)
    from matplotlib.colors import ListedColormap
    custom_cmap=ListedColormap(['#EF9A9A','#FFF59D','#90CAF9'])
    plt.contourf(x0,x1,zz,linewidth=5,cmap=custom_cmap)

plot_decision_boundary(svc,axis=[-3,3,-3,3])
plt.scatter(X_standard[0:y2,0],X_standard[0:y2,1],color='red')
plt.scatter(X_standard[y2:y1,0],X_standard[y2:y1,1],color='blue')
plt.show()

在这里插入图片描述

(二)分类后的内容添加上下边界

代码

def plot_svc_decision_boundary(model,axis):
    x0,x1=np.meshgrid(
        np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*100)).reshape(-1,1),
        np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*100)).reshape(-1,1),
    )
    X_new=np.c_[x0.ravel(),x1.ravel()]
    
    y_predict=model.predict(X_new)
    zz=y_predict.reshape(x0.shape)
    
    from matplotlib.colors import ListedColormap
    custom_cmap=ListedColormap(['#EF9A9A','#FFF59D','#90CAF9'])
    
    plt.contourf(x0,x1,zz,linewidth=5,cmap=custom_cmap)
    
    w=model.coef_[0]
    b=model.intercept_[0]
    
    index_x=np.linspace(axis[0],axis[1],100)
    
    y_up=(1-w[0]*index_x-b)/w[1]
    y_down=(1-w[0]*index_x-b)/w[1]
    
    x_index_up=index_x[(y_up<=axis[3])&(y_up>=axis[2])]
    x_index_down=index_x[(y_down<=axis[3])&(y_down>=axis[2])]
    
    y_up=y_up[(y_up<=axis[3])&(y_up>=axis[2])]
    y_down=y_down[(y_down<=axis[3])&(y_down>=axis[2])]
    
    plt.plot(x_index_up,y_up,color="black")
    plt.plot(x_index_down,y_down,color="black")

svc=LinearSVC(C=1e9)
svc.fit(X_standard,Y1)
plot_svc_decision_boundary(svc,axis=[-3,3,-3,3])
plt.scatter(X_standard[0:y2,0],X_standard[0:y2,1],color='red')
plt.scatter(X_standard[y2:y1,0],X_standard[y2:y1,1],color='blue')
plt.show()

在这里插入图片描述
修改C的值

svc=LinearSVC(C=0.01)
svc.fit(X_standard,Y1)
plot_svc_decision_boundary(svc,axis=[-3,3,-3,3])
plt.scatter(X_standard[0:y2,0],X_standard[0:y2,1],color='red')
plt.scatter(X_standard[y2:y1,0],X_standard[y2:y1,1],color='blue')
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值