鸢尾花种类预测

本实验介绍了使⽤Python进⾏机器学习的⼀些基本概念。 在本案例中,将使⽤K-Nearest Neighbor(KNN)算法对鸢 尾花的种类进⾏分类,并测量花的特征。

1. 数据集介绍

Iris数据集是常⽤的分类实验数据集,由Fisher, 1936收集整理。Iris也称鸢尾花卉数据集,是⼀类多重变量分析的数据 集。关于数据集的具体介绍:
在这里插入图片描述

2 scikit-learn中数据集介绍

2.1 scikit-learn数据集API介绍

  • sklearn.datasets
    加载获取流⾏数据集
    datasets.load_()
    -------获取⼩规模数据集,数据包含在datasets⾥
    datasets.fetch_
    (data_home=None)
    -------获取⼤规模数据集,需要从⽹络上下载,函数的第⼀个参数是data_home,表示数据集下载的⽬录,默认 是 ~/scikit_learn_data/
2.1.1 sklearn⼩数据集
  • sklearn.datasets.load_iris() # 加载并返回鸢尾花数据集
    在这里插入图片描述
2.1.2 sklearn⼤数据集
  • sklearn.datasets.fetch_20newsgroups(data_home=None,subset=‘train’)
    subset:‘train’或者’test’,‘all’,可选,选择要加载的数据集。
    训练集的“训练”,测试集的“测试”,两者的“全部

2.2 sklearn数据集返回值介绍

  • load和fetch返回的数据类型datasets.base.Bunch(字典格式)
    data:特征数据数组,是 [n_samples * n_features] 的⼆维 numpy.ndarray 数组
    target:标签数组,是 n_samples 的⼀维 numpy.ndarray 数组
    DESCR:数据描述
    feature_names:特征名,新闻数据,⼿写数字、回归数据集没有 t
    arget_names:标签名
from sklearn.datasets import load_iris 
# 获取鸢尾花数据集 
iris = load_iris()
 print("鸢尾花数据集的返回值:\n", iris) 
 # 返回值是⼀个继承⾃字典的Bench 
 print("鸢尾花的特征值:\n", iris["data"]) 
 print("鸢尾花的⽬标值:\n", iris.target) 
 print("鸢尾花特征的名字:\n", iris.feature_names) 
 print("鸢尾花⽬标值的名字:\n", iris.target_names) 
 print("鸢尾花的描述:\n", iris.DESCR)

2.3 查看数据分布

通过创建⼀些图,以查看不同类别是如何通过特征来区分的。 在理想情况下,标签类将由⼀个或多个特征对完美分 隔。 在现实世界中,这种理想情况很少会发⽣。

seaborn介绍

  • Seaborn 是基于 Matplotlib 核⼼库进⾏了更⾼级的 API 封装,可以让你轻松地画出更漂亮的图形。⽽ Seaborn 的漂亮主要体现在配⾊更加舒服、以及图形元素的样式更加细腻。
  • 安装 pip3 install seaborn
  • seaborn.lmplot() 是⼀个⾮常有⽤的⽅法,它会在绘制⼆维散点图时,⾃动完成回归拟合
    —sns.lmplot() ⾥的 x, y 分别代表横纵坐标的列名,
    ----data= 是关联到数据集,
    ---- hue=*代表按照 species即花的类别分类显示,
    ---- fit_reg=是否进⾏线性拟合。
    点击查看 API http://seaborn.pydata.org/
%matplotlib inline
 # 内嵌绘图 
 import seaborn as sns 
 import matplotlib.pyplot as plt
 import pandas as pd 
  # 把数据转换成dataframe的格式 
  iris_d = pd.DataFrame(iris['data'], columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']) 
  iris_d['Species'] = iris.target 
  def plot_iris(iris, col1, col2): 
    sns.lmplot(x = col1, y = col2, data = iris, hue = "Species", fit_reg = False) 
    plt.xlabel(col1)
     plt.ylabel(col2) 
     plt.title('鸢尾花种类分布图') 
     plt.show() 
 plot_iris(iris_d, 'Petal_Width', 'Sepal_Length')

在这里插入图片描述

2.4 数据集的划分

机器学习⼀般的数据集会划分为两个部分:

  • 训练数据:⽤于训练,构建模型
  • 测试数据:在模型检验时使⽤,⽤于评估模型是否有效

划分⽐例:

  • 训练集:70% 80% 75%
  • 测试集:30% 20% 25%

数据集划分api
sklearn.model_selection.train_test_split(arrays, *options)

  • 参数
    x 数据集的特征值
    y 数据集的标签值
    test_size 测试集的⼤⼩,⼀般为float
    random_state 随机数种⼦,不同的种⼦会造成不同的随机采样结果。相同的种⼦采样结果相同
  • returnx_train,
    x_test, y_train, y_test
from sklearn.datasets import load_iris 
from sklearn.model_selection import train_test_split
 # 1、获取鸢尾花数据集 
 iris = load_iris() 
 # 对鸢尾花数据集进⾏分割
 # 训练集的特征值x_train 测试集的特征值x_test 训练集的⽬标值y_train 测试集的⽬标值y_test x_train, 
 x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=22)
 print("x_train:\n", x_train.shape) 
 # 随机数种⼦ 
 x_train1, x_test1, y_train1, y_test1 = train_test_split(iris.data, iris.target, random_state=6) 
 x_train2, x_test2, y_train2, y_test2 = train_test_split(iris.data, iris.target, random_state=6) 
 print("如果随机数种⼦不⼀致:\n", x_train == x_train1) 
 print("如果随机数种⼦⼀致:\n", x_train1 == x_train2)

3. 总结在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值