资源简介
实现svm对鸢尾花进行分类,3个不同品种的花每个50个数据进行分类,鸢尾花数据:archive.ics.uci.edu/ml/datasets/Ilis
代码片段和文件信息
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/3/2 13:57
# @Author: HanBingru
# @File : ywh.py
import numpy as np
from matplotlib import colors
from sklearn import svm
from sklearn.svm import SVC
from sklearn import model_selection
import matplotlib.pyplot as plt
import matplotlib as mpl
def load_data():
# 导入数据
data = np.loadtxt(r‘H:\开学\章永来\鸢尾花\iris.data‘ dtype=float delimiter=‘‘ converters={4: iris_type})
return data
def iris_type(s):
# 数据转为整型,数据集标签类别由string转为int
it = {b‘Iris-setosa‘: 0 b‘Iris-versicolor‘: 1 b‘Iris-virginica‘: 2}
return it[s]
# 定义分类器
def classifier():
clf = svm.SVC(C=0.5 # 误差项惩罚系数
kernel=‘linear‘ # 线性核 kenrel=“rbf“:高斯核
decision_function_shape=‘ovr‘) # 决策函数
return clf
def train(clf x_train y_train):
# x_train:训练数据集
# y_train:训练数据集标签
# 训练开始
clf.fit(x_train y_train.ravel() sample_weight=None) # 同flnumpy.ravelatten将矩阵拉平
def show_accuracy(a b tip):
acc = a.ravel() == b.ravel()
print(a)
print(b)
print(acc)
print(‘%s Accuracy:%.3f‘ % (tip np.mean(acc)))
def print_accuracy(clf x_train y_train x_test y_test):
# print(x_train)
show_accuracy(clf.predict(x_train) y_train ‘traing data‘)
show_accuracy(clf.predict(x_test) y_test ‘testing data‘)
# print(x_train)
# print(y_train.ravel())
# print(clf.predict(x_train))
def draw(clf x): # 写完一个函数要运行,否则报错:函数未定义
‘‘‘
print(x.shape)
(150 2)
‘‘‘
iris_feature = ‘sepal length‘ ‘sepal width‘ ‘petal lenght‘ ‘petal width‘
x1_min x1_max = x[: 0].min() x[: 0].max() # 第0列的范围
x2_min x2_max = x[: 1].min() x[: 1].max() # 第1列的范围
x1 x2 = np.mgrid[x1_min:x1_max:200j x2_min:x2_max:200j] # 生成网格采样点
grid_test = np.stack((x1.flat x2.flat) axis=1) # 测试点
‘‘‘
print(grid_test.shape)
(40000 2)
‘‘‘
# print(‘grid_test:\n‘ grid_test)
z = clf.decision_function(grid_test)
# print(‘the distance to decision plane:\n‘ z)
grid_hat = clf.predict(grid_test) # 预测分类值 得到【00.。。。22
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论