资源简介
SVM支持向量机分类鸢尾花数据集iris及代码,数据集有Excel、data、txt文件格式,代码有data、txt格式演示

代码片段和文件信息
# coding=utf-8
from sklearn import svm
import numpy as np
from sklearn import model_selection
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import colors
# # 当使用numpy中的loadtxt函数导入该数据集时,假设数据类型dtype为浮点型,但是很明显数据集的第五列的数据类型是字符串并不是浮点型。
# # 因此需要额外做一个工作,即通过loadtxt()函数中的converters参数将第五列通过转换函数映射成浮点类型的数据。
# # 首先,我们要写出一个转换函数:
# # 定义一个函数,将不同类别标签与数字相对应
def iris_type(s):
class_label={b‘setosa‘:0b‘versicolor‘:1b‘virginica‘:2}
return class_label[s]
def train(model x_train y_train):
model.fit(x_trainy_train.ravel())
def show_accuracy(a b tip):
acc = (a.ravel() == b.ravel())
print(“%s Accuracy:%.3f“%(tip np.mean(acc)))
def print_accuracy(model x_train y_trainx_test y_test ):
print(‘SVM-输出训练集的准确率为:‘ model.score(x_train y_train))
print(“SVM-输出测试集的准确率为:“ model.score(x_test y_test))
#原始结果与预测结果进行对比
show_accuracy(model.predict(x_train) y_train ‘traing data‘)
show_accuracy(model.predict(x_test) y_test ‘testing data‘)
#计算决策函数的值
print(‘decision_function:\n‘ model.decision_function(x_train))
def draw(model x):
x1_min x1_max = x[: 0].min() x[: 0].max() # 第0列的范围 x[: 0] “:“表示所有行,0表示第1列
x2_min x2_max = x[: 1].min() x[: 1].max() # 第1列的范围 x[: 0] “:“表示所有行,1表示第2列
x1 x2 = np.mgrid[x1_min:x1_max:200j x2_min:x2_max:200j] # 生成网格采样点(用meshgrid函数生成两个网格矩阵X1和X2)
grid_test = np.stack((x1.flat x2.flat) axis=1) # 测试点,再通过stack()函数,axis=1,生成测试点
# .flat 将矩阵转变成一维数组 (与ravel()的区别:flatten:返回的是拷贝
grid_hat = model.predict(grid_test) # 预测分类值
grid_hat = grid_hat.reshape(x1.shape) # 使之与输入的形状相同
# # 2.指定默认字体
# mpl.rcParams[‘font.sans-serif‘] = [u‘SimHei‘]
# mpl.rcParams[‘axes.unicode_minus‘] = False
# 3.绘制
cm_light = mpl.colors.ListedColormap([‘#A0FFA0‘ ‘#FFA0A0‘ ‘#A0A0FF‘])
cm_dark = mpl.colors.ListedColormap([‘g‘ ‘r‘ ‘b‘])
# alpha = 0.5
plt.pcolormesh(x1 x2 grid_hat cmap=cm_light) # 预测值的显示
# plt.plot(x[: 0] x[: 1] ‘o‘ alpha=alpha color=‘blue‘ markeredgecolor=‘k‘)
plt.scatter(x[: 0] x[: 1] c=np.squeeze(y) edgecolor=‘k‘ s=50 cmap = cm_dark ) # 圈中测试集样本
plt.scatter(x_test[: 0] x_test[: 1] s=120 facecolors=‘none‘ zorder=10) # 圈中测试集样本
plt.xlabel(‘sepal length‘ fontsize=13)
plt.ylabel(‘sepal width‘ fontsize=13)
plt.xlim(x1_min x1_max)
plt.ylim(x2_min x2_max)
plt.title(‘SVM feature‘ fontsize=15)
# plt.grid()
plt.show()
if __name__ == ‘__main__‘:
#1.数据准备
#1.1加载数据
#使用numpy中的loadtxt读入数据文件
filepath=‘iris2.txt‘ # 数据文件路径
data=np.loadtxt(filepathdtype=floatdelimiter=Noneconverters={4:iris_type})
#1.2数据分割
X y = np.split(data (4)axis=1) # np.split 按照列(axis=1)进行分割,从第四列开始往后的作为y 数据,之前的作为X 数据。函数 split(数据,分割位置,轴=1(水平分割) or 0(垂直分割))。
x = X[:0:2] # 在 X中取前两列作为特征(为了后期的可视化画图更加直观,故只取前两列特征值向量进行训练)
x_trainx_testy_trainy_test=model_selection.tr
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3867 2020-09-29 18:53 SVM支持向量机分类鸢尾花数据集iris及代码\iris.csv
文件 4969 2020-09-29 18:37 SVM支持向量机分类鸢尾花数据集iris及代码\iris.txt
文件 811 2020-09-29 19:39 SVM支持向量机分类鸢尾花数据集iris及代码\iris2.data
文件 3808 2020-09-30 08:05 SVM支持向量机分类鸢尾花数据集iris及代码\iris2.txt
文件 6015 2020-09-29 19:25 SVM支持向量机分类鸢尾花数据集iris及代码\svm01.py
文件 4526 2020-09-29 19:39 SVM支持向量机分类鸢尾花数据集iris及代码\svm02.py
目录 0 2020-09-30 08:22 SVM支持向量机分类鸢尾花数据集iris及代码
----------- --------- ---------- ----- ----
23996 7
相关资源
- 基于蚁群算法优化SVM的瓦斯涌出量预
- 基于模糊聚类和SVM的瓦斯涌出量预测
- 基于CAN总线与ZigBee的瓦斯实时监测及
- SVM-Light资料,使用说明
- 果蝇算法融合SVM的开采沉陷预测模型
- BoW|Pyramid BoW+SVM进行图像分类
- 基于libsvm的图像分割代码
- 基于SVM及两类运动想象的多通道特征
- 小波包和SVM在轴承故障识别中的应用
- 林智仁教授最新版本LibSVM工具箱
- 台湾林教授的支持向量机libsvm
- 新闻分类语料
- libsvm-3.20
- 7种支持向量机SVM工具包
- A Practical Guide to Support Vector Classifica
- libSVM的代码详细解析,注释非常详细
- 台湾林志恒的LIBSVM的中文简体说明文
- SVM算法-回归拟合程序.zip
- 基于Grid-Search_PSO优化SVM回归预测矿井
- 新冒落带高度算法FOA-SVM预计模型
- 官方最新版本libsvm-3.23
- 基于PCA和SVM的个性化睡眠分期研究
- 基于svm四种工具箱
- 自然语言处理之文本主题判别
- HOG+SVM实现数字识别
- opencv自带SVM分类器使用程序
- 基于HOG特征提取的svm行人头肩训练
- 遗传算法优化支持向量机算法
- 经典的行人检测算法,利用HOG和SVM实
- 基于GA参数优化的在线学习SVM算法及其
评论
共有 条评论