机器学习实验(四)

实验四 SVM

一、 实验目的

  1. 加深对监督学习的理解和认识。

  2. 掌握 SVM 分类器的设计方法。

  3. 通过鸢尾花的花萼(sepal)和花瓣(petal)的长和宽,建立SVM分类器来判断样本属于山鸢尾(Iris Setosa)、变色鸢尾(Iris Versicolor)还是维吉尼亚鸢尾(Iris Virginica)。

二、实验原理

支持向量机旨在求一个分离超平面。这个超平面使得离它最近的点能够最远。

三、分类步骤

  1. 导入鸢尾花数据集;

  2. 数据归一化;

  3. 训练集和测试数据集划分;

  4. 评价分类结果TP、FN、FP、TN以及精确率和召回率;

    • FN:被判定为负样本,但事实上是正样本;
    • FP:被判定为正样本,但事实上是负样本;
    • TN:被判定为负样本,事实上也是负样本;
    • TP:被判定为正样本,事实上也是证样本;
    • 精确率 Precesion:针对模型判断出的所有正例(即 T P + F P TP+FP TP+FP )而言,其中真正例 TP 占的比例。 P r e c i s i o n = T P ( T P + F P ) Precision = \frac{TP}{(TP+FP)} Precision=(TP+FP)TP
    • 召回率 Recall:针对数据集中的所有正例(即 T P + F P TP+FP TP+FP )而言,模型正确判断出的正例 TP 占数据集中所有正例的比例,FN 表示被模型误认为是负例但实际是正例的数据。 R e c a l l = T P T P + F N Recall = \frac{TP}{TP+FN} Recall=TP+FNTP
  5. 加入松弛因子后,与未加松弛因子之前效果做对比。

    假设样本数为 n n n 原先对样本的分类是 y i [ ( w x i ) + b ] ≥ 1      ( i = 1 , 2 , … … , n ) y_i[(wx_i)+b] \geq1\ \ \ \ (i = 1,2,……,n) yi[(wxi)+b]1    (i=1,2,,n),

    则引入松弛因子 ζ ≥ 0 \zeta \geq 0 ζ0 后对样本分类的要求变为 y i [ ( w x i ) + b ] ≥ 1 − ζ i      ( i = 1 , 2 , … … , n ) y_i[(wx_i)+b] \geq 1 - \zeta_i\ \ \ \ (i = 1,2,……,n) yi[(wxi)+b]1ζi    (i=1,2,,n)

    松弛因子的意义是引入一定的容错性

四、代码和执行结果展示

import numpy as np
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import label_binarize

iris = datasets.load_iris()
X = iris.data
y = iris.target

random_state = np.random.RandomState(0)
n_samples, n_features = X.shape

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=66)
# 设定参数
classifier = OneVsRestClassifier(svm.SVC(C=100000, kernel='linear', probability=True, random_state=random_state))
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)

def func(precited, expected):
    res = (precited ^ expected)  
    r = np.bincount(res)
    tp_list = (precited & expected)
    fp_list = (precited & (~expected))
    tp_list = tp_list.tolist()
    fp_list = fp_list.tolist()
    TP = tp_list.count(1)
    FP = fp_list.count(1)
    TN = r[0] - TP
    FN = r[1] - FP
    Recall = TP / (TP + FN)
    Precesion = TP / (TP + FP)
    return TP, FP, TN, FN, Recall, Precesion

y_test1 = label_binarize(y_test, classes=[0, 1, 2])
y_pred1 = label_binarize(y_pred, classes=[0, 1, 2])

print(y_pred1[..., 0], y_test1[..., 0])
tp, fp, tn, fn, recall, pre = func(y_pred1[..., 0], y_test1[..., 0])
recall = format(recall, '0.2f')
pre = format(pre, '0.2f')
print(f"type1:recall = {recall} precision ={pre}")
tp, fp, tn, fn, recall, pre = func(y_pred1[..., 1], y_test1[..., 1])
recall = format(recall, '0.2f')
pre = format(pre, '0.2f')
print(f"type2:recall = {recall} precision = {pre}")
tp, fp, tn, fn, recall, pre = func(y_pred1[..., 2], y_test1[..., 2])
recall = format(recall, '0.2f')
pre = format(pre, '0.2f')
print(f"type3:recall = {recall} precision = {pre}")

修改松弛因子

即修改如下语句中的参数 C

classifier = OneVsRestClassifier(svm.SVC(C=100, kernel='linear', probability=True, random_state=random_state))

结果展示

分类计算,得到各类的精确率 Precesion 和召回率 Recall 如下:

修改松弛因子

可见添加松弛因之后,各项指标均有明显提升

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值