基于SVM的鸢尾花数据集回归分析

1. 作者介绍

张佳伦,男,西安工程大学电子信息学院,2022级研究生
研究方向:机器视觉与人工智能
电子邮件:[email protected]

陈梦丹,女,西安工程大学电子信息学院,2022级硕士研究生,张宏伟人工智能课题组
研究方向:机器视觉与人工智能
电子邮件:[email protected]

2. SVM支持向量机算法

支持向量机(Support Vector Machine, SVM),其决策边界是对学习样本求解的最大边距超平面。

图(1)为决策边界与支持向量。
在这里插入图片描述

图(2)为软间隔与硬间隔
在这里插入图片描述
SVM可以通过核技巧(kernel Trick)进行非线性分类,是常见的核学习(kernel learning)方法之一。
在这里插入图片描述
常见的核函数:

Rbf高斯核函数:
在这里插入图片描述
Sigmoid核函数:
在这里插入图片描述

2.1 鸢尾花数据集

在这里插入图片描述
利用.info()查看数据集的整体信息,可以查看到数据集的标签种类以及对应的标签名称与标签个数。

# 使用.info()查看数据的整体信息
iris_features.info()

在这里插入图片描述
利用.value_counts()查看数据集样本的种类,可以查看到数据集对应样本种类的编号和对应样本个数。
在这里插入图片描述

2.2 鸢尾花数据集可视化

2.2.1 散点图

合并标签和特征信息,将数据集的标签特征与样本种类结合起来进行二维的可视化分布。如鸢尾花数据集有四个特征标签以及三种样本种类,将三种样本种类分布,分别映射到对应的二维特征坐标中。且当坐标对应为同一纬度时,及可视各个样本在本纬度对应的分布数量。可以在各个纬度的二维组合下观察到每个样本的特征分布。

# 合并标签和特征信息
iris_all = iris_features.copy() ##进行浅拷贝,防止对于原始数据的修改
iris_all['target'] = iris_target
sns.pairplot(data=iris_all,diag_kind='hist', hue= 'target')  # 特征与标签组合的散点可视化
plt.show()

在这里插入图片描述

2.2.2 箱型图

箱线图是针对单个特征纬度的样本种类分布,通过箱线图即可方便的观察到数据集在某个单一特征下,样本种类的一维特征分布以及各个样本种类之间的可分离度。

for col in iris_features.columns:
    sns.boxplot(x='target', y=col, saturation=0.5,palette='pastel', data=iris_all)
    plt.title(col)
plt.show()

在这里插入图片描述

2.2.3 三维散点图(3D)

选取其三个特征绘制三维散点图。根据选中的三个纬度的特征即可对数据集中的样本种类进行三维建模。通过改变选中的三个特征,和根据箱线图的单一纬度特征的样本可分离度,可以在不同的特征纬度下对数据集进行3D建模,并得到分离度最高的3D建模结果。

# 选取其前三个特征绘制三维散点图
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection='3d')
iris_all_class0 = iris_all[iris_all['target']==0].values
iris_all_class1 = iris_all[iris_all['target']==1].values
iris_all_class2 = iris_all[iris_all['target']==2].values
# 'setosa'(0), 'versicolor'(1), 'virginica'(2)
ax.scatter(iris_all_class0[:,0], iris_all_class0[:,1], iris_all_class0[:,2],label='setosa')
ax.scatter(iris_all_class1[:,0], iris_all_class1[:,1], iris_all_class1[:,2],label='versicolor')
ax.scatter(iris_all_class2[:,0], iris_all_class2[:,1], iris_all_class2[:,2],label='virginica')
plt.legend()
plt.show()

在这里插入图片描述

3. SVM算法实现

3.1 完整代码

import numpy as np
import pandas as pd
import pylab
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.axes._axes import _log as matplotlib_axes_logger
matplotlib_axes_logger.setLevel('ERROR')
from sklearn.datasets import load_iris

data = load_iris() # 得到数据特征
iris_target = data.target # 得到数据对应的标签
iris_features = pd.DataFrame(data=data.data, columns=data.feature_names) # 利用Pandas转化为DataFrame格式

iris_features.info()

pd.Series(iris_target).value_counts()

#### 数据可视化 ####
# 散点图
# 合并标签和特征信息
iris_all = iris_features.copy() ##进行浅拷贝,防止对于原始数据的修改
iris_all['target'] = iris_target
sns.pairplot(data=iris_all,diag_kind='hist', hue= 'target')  # 特征与标签组合的散点可视化
plt.show()

# 箱型图
for col in iris_features.columns:
    sns.boxplot(x='target', y=col, saturation=0.5,palette='pastel', data=iris_all)
    plt.title(col)
plt.show()

# 选取其前三个特征绘制三维散点图
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection='3d')
iris_all_class0 = iris_all[iris_all['target']==0].values
iris_all_class1 = iris_all[iris_all['target']==1].values
iris_all_class2 = iris_all[iris_all['target']==2].values
# 'setosa'(0), 'versicolor'(1), 'virginica'(2)
ax.scatter(iris_all_class0[:,0], iris_all_class0[:,1], iris_all_class0[:,2],label='setosa')
ax.scatter(iris_all_class1[:,0], iris_all_class1[:,1], iris_all_class1[:,2],label='versicolor')
ax.scatter(iris_all_class2[:,0], iris_all_class2[:,1], iris_all_class2[:,2],label='virginica')
plt.legend()
plt.show()

iris=datasets.load_iris()
a,b=0,2
X_reduced = iris.data[:,:4]
X = X_reduced[:,[a,b]]        #二维可视化,即只取两个属性
y = iris.target     #由上述程序结果可知取值为0,1,2
x_min,x_max = X[:,0].min()-.5, X[:,0].max()+.5   #x值的最小值和最大值分别是第一列最小值和最大值-5和+5
y_min,y_max = X[:,1].min()-.5, X[:,1].max()+.5   #y值的最小值和最大值分别是第二列最小值和最大值-5和+5
plt.figure(2,figsize=(8,6))
plt.clf
plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.Set1,edgecolor='w')  #绘制散点图,c即color,cmap是将y不同的值画出不同颜色,edgecolor为白色
plt.xlabel(iris.feature_names[a])
plt.ylabel(iris.feature_names[b])
plt.xlim(x_min,x_max)    #x轴的作图范围
plt.ylim(y_min,y_max)    #x轴的作图范围
plt.xticks(())     #x轴的刻度内容的范围
plt.yticks(())    #y轴的刻度内容的范围

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=0)
sc = StandardScaler()
sc.fit(X_train)  #调用训练集训练
X_train_std = sc.transform(X_train)
X_test_std: object = sc.transform(X_test)

def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim = (xx2.min(), xx2.max())
    X_test, y_test = X[test_idx, :], y[test_idx]
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl)
    if test_idx:
        X_test, y_test = X[test_idx, :], y[test_idx]
        plt.scatter(X_test[:, 0], X_test[:, 1],c='black', alpha=0.8, linewidths=1, marker='o', s=10, label='test set')
        
# 调整2*2图像大小比例
plt.figure(2,figsize=(10,8))

pylab.subplot(2, 2, 1)   # 子图像为2*2的第一个
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
svm = SVC(kernel='linear', random_state=0, C=1.0)  # 调用SVM核函数,’linear’核函数,以及两个超参数
svm.fit(X_train_std, y_train)   # 训练
plot_decision_regions(X_combined_std, y_combined, classifier=svm, test_idx=range(105,150))
plt.ylabel(iris.feature_names[b])
plt.title('Linear')

pylab.subplot(2, 2, 2)
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
svm = SVC(kernel='poly',random_state=0, degree=2, gamma=0.3, C=100)
# 调用SVM核函数,’poly’以及四个参数,多项式核函数专属的超参数d
svm.fit(X_train_std, y_train)  #训练
plot_decision_regions(X_combined_std, y_combined, classifier=svm, test_idx=range(105,150))
plt.title('poly')

pylab.subplot(2, 2, 3)
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
svm = SVC(kernel='rbf', random_state=0, gamma=0.9, C=1.5)
svm.fit(X_train_std, y_train)
plot_decision_regions(X_combined_std, y_combined, classifier=svm, test_idx=range(105,150))
plt.ylabel(iris.feature_names[b])
plt.xlabel(iris.feature_names[a])
plt.title('rbf')

pylab.subplot(2, 2, 4)
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
svm = SVC(kernel='sigmoid', random_state=0, gamma=0.3, C=50)
svm.fit(X_train_std, y_train)
plot_decision_regions(X_combined_std, y_combined, classifier=svm, test_idx=range(105,150))
plt.xlabel(iris.feature_names[a])
plt.title('sigmoid')

plt.show()

3.2 运行结果

在SVM分类中,将特征标签设置为变量,通过改变标签变量可以得到不同纬度的样本种类的二维特征分布,再通过四种不同的SVM核函数,对其进行分类比较,最后调整超参数得到最好的分类核函数和分类结果。

特征0:2二维特征分布图在这里插入图片描述相关参数设置:
Linear : c=1
Poly : degree=2,gamma=0.3,c=100
Rbf : gamma=0.9, c=1.5
Sigmoid : gamma=0.3, c=50.0

特征0:2 经四种不同的SVM核函数处理结果图
在这里插入图片描述
特征0:3 二维特征分布图
在这里插入图片描述
相关参数设置:
Linear : c=1
Poly : degree=3,gamma=0.7,c=10
Rbf : gamma=0.9, c=1.5
Sigmoid : gamma=0.2, c=2.5

特征0:3 经四种不同的SVM核函数处理结果图
在这里插入图片描述
特征0:1 二维特征分布图
在这里插入图片描述相关参数设置:
Linear : c=1
Poly : degree=2,gamma=0.8,c=10
Rbf : gamma=0.9, c=1.5
Sigmoid : gamma=0.3, c=1.0

特征0:1 经四种不同的SVM核函数处理结果图在这里插入图片描述
特征1:2 二维分布图
在这里插入图片描述
相关参数设置:
Linear : c=1
Poly : degree=3, gamma=0.7, c=5
Rbf : gamma=0.9, c=1.5
Sigmoid : gamma=0.2, c=2.5

特征1:2 经四种不同的SVM核函数处理结果图
在这里插入图片描述
特征1:3 二维特征分布图
在这里插入图片描述
相关参数设置:
Linear : c=1
Poly : degree=3, gamma=0.9, c=1.5
Rbf : gamma=0.9,c=10
Sigmoid : gamma=0.2,c=2.5

特征1:3 经四种不同的SVM核函数处理结果图
在这里插入图片描述
特征2:3 二维特征分布图
在这里插入图片描述
相关参数设置:
Linear : c=1
Poly : degree=3, gamma=0.9, c=3
Rbf : gamma=0.9,c=50
Sigmoid : gamma=0.5,c=5

特征2:3 经四种不同的SVM核函数处理结果图
在这里插入图片描述

3.3 问题与分析

(1)特征超出标签数字索引,应为[0,1,2,3]。
在这里插入图片描述
(2)图像排版中,pylab.subplot(2,2,2):第一个2为行数,第二个2为列数,第三个为子图的顺序故如上图程序应更为pylab.subplot(1,4,…)
在这里插入图片描述
(3)在选取核函数是应注意核函数的简化名称,不应写错(注意首字母大小写)
在这里插入图片描述
(4)在运行降维图像时,更改维度特征会报错,但图依然可以更改出来。为了消除报错,在每次运行完后更改维度特征时,需要重新启动Python核
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_37758063/article/details/131082668