SVM系列(四)SVM python应用实例(一)随机数据+人脸识别

目前先不给出SVM的算法的python代码,先用python自带的SVM进行分类、感谢该视频的相关内容https://www.youtube.com/watch?v=EySkC36W9hE&t=1631s

线性可分数据

from sklearn.svm import SVC
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import datasets
import numpy as np
from sklearn.model_selection import train_test_split
## 利用自带的点数据,进行分类
X,y = datasets.make_blobs(n_samples=300,n_features= 2,centers= 2)
X_train,X_test, y_train,y_test = train_test_split(X,y,test_size = 0.2)
## 采用的是线性分类器,tol 就是 我们说的loss,这里非常小,相当于hard-margin
svc = SVC(kernel= 'linear')
svc.fit(X_train,y_train)
y_ = svc.predict(X_test)
## 数据可视化
w1 , w2= svc.coef_[0]
b = svc.intercept_
x1 = np.linspace(-4,6,100)
x2 = (-b - w1*x1)/w2
x2_1 = (1-b - w1*x1)/w2
x21 = (-1-b - w1*x1)/w2
support_vectors = svc.support_vectors_   ## 得到支持向量
plt.scatter(X_train[:,0],X_train[:,1],c = y_train)
plt.scatter(X_test[:,0],X_test[:,1],c = y_)
plt.plot(x1,x2)
plt.plot(x1,x2_1,'--')
plt.plot(x1,x21,'--')
plt.scatter(support_vectors[:,0],support_vectors[:,1],marker= 'o',s = 300,alpha=  0.3 , c='red')

输出结果,可见准确率100%,因为我们将预测数据以预测标签颜色输出,如果分错了就会再各自的位置出现对方颜色的标签。红色标记的就是向量机

 线性不可分数据

接下来讨论线性不可分数据

1. 生成数据,随机产生符合正态分布的数据,400个训练,400个测试,可视化训练数据的情况,可见目标按照主对角线分为2类

from sklearn.svm import SVC
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import datasets
import numpy as np
from sklearn.model_selection import train_test_split

X = np.random.randn(800,2)
y = X [:,0] * X [:,1] >0
X_train,X_test, y_train,y_test = train_test_sp
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lee_Yu_Rui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值