train_test_split 参数详解

简单用法如下:

from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
iris = load_iris()
print(iris.data.shape)
print(iris.DESCR)
(150, 4)
.. _iris_dataset:Iris plants dataset
--------------------**Data Set Characteristics:**:Number of Instances: 150 (50 in each of three classes):Number of Attributes: 4 numeric, predictive attributes and the class:Attribute Information:- sepal length in cm- sepal width in cm- petal length in cm- petal width in cm- class:- Iris-Setosa- Iris-Versicolour- Iris-Virginica:Summary Statistics:============== ==== ==== ======= ===== ====================Min  Max   Mean    SD   Class Correlation============== ==== ==== ======= ===== ====================sepal length:   4.3  7.9   5.84   0.83    0.7826sepal width:    2.0  4.4   3.05   0.43   -0.4194petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)petal width:    0.1  2.5   1.20   0.76    0.9565  (high!)============== ==== ==== ======= ===== ====================:Missing Attribute Values: None:Class Distribution: 33.3% for each of 3 classes.:Creator: R.A. Fisher:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov):Date: July, 1988The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher's paper. Note that it's the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.This is perhaps the best known database to be found in the
pattern recognition literature.  Fisher's paper is a classic in the field and
is referenced frequently to this day.  (See Duda & Hart, for example.)  The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant.  One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other... topic:: References- Fisher, R.A. "The use of multiple measurements in taxonomic problems"Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions toMathematical Statistics" (John Wiley, NY, 1950).- Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.(Q327.D83) John Wiley & Sons.  ISBN 0-471-22361-1.  See page 218.- Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New SystemStructure and Classification Rule for Recognition in Partially ExposedEnvironments".  IEEE Transactions on Pattern Analysis and MachineIntelligence, Vol. PAMI-2, No. 1, 67-71.- Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule".  IEEE Transactionson Information Theory, May 1972, 431-433.- See also: 1988 MLC Proceedings, 54-64.  Cheeseman et al"s AUTOCLASS IIconceptual clustering system finds 3 classes in the data.- Many, many more ...
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=33, stratify=y)
print(X_train.shape)
print(X_test.shape)
(112, 4)
(38, 4)
  • train_target:所要划分的样本结果

  • test_size:样本占比,如果是整数的话就是样本的数量

  • random_state:是随机数的种子。
    随机数种子:其实就是该组随机数的编号,在需要重复试验的时候,保证得到一组一样的随机数。比如你每次都填1,其他参数一样的情况下你得到的随机数组是一样的。但填0或不填,每次都会不一样。

  • stratify是为了保持split前类的分布。比如有100个数据,80个属于A类,20个属于B类。如果train_test_split(… test_size=0.25, stratify = y_all), 那么split之后数据如下:
    training: 75个数据,其中60个属于A类,15个属于B类。
    testing: 25个数据,其中20个属于A类,5个属于B类。

    用了stratify参数,training集和testing集的类的比例是 A:B= 4:1,等同于split前的比例(80:20)。通常在这种类分布不平衡的情况下会用到stratify。

    将stratify=X就是按照X中的比例分配

    将stratify=y就是按照y中的比例分配

整体总结起来各个参数的设置及其类型如下:

主要参数说明:

*arrays:可以是列表、numpy数组、scipy稀疏矩阵或pandas的数据框

test_size:可以为浮点、整数或None,默认为None

①若为浮点时,表示测试集占总样本的百分比

②若为整数时,表示测试样本样本数

③若为None时,test size自动设置成0.25

train_size:可以为浮点、整数或None,默认为None

①若为浮点时,表示训练集占总样本的百分比

②若为整数时,表示训练样本的样本数

③若为None时,train_size自动被设置成0.75

random_state:可以为整数、RandomState实例或None,默认为None

①若为None时,每次生成的数据都是随机,可能不一样

②若为整数时,每次生成的数据都相同

stratify:可以为类似数组或None

①若为None时,划分出来的测试集或训练集中,其类标签的比例也是随机的

②若不为None时,划分出来的测试集或训练集中,其类标签的比例同输入的数组中类标签的比例相同,可以用于处理不均衡的数据集

train_test_split 参数详解相关推荐

  1. 数据集划分函数sklearn.model_selection.train_test_split参数详解

    该函数可以随机划分样本数据为训练集和测试集,并返回划分好的训练集和测试集数据. sklearn.model_selection.train_test_split(train_data,train_ta ...

  2. Lesson 8.3Lesson 8.4 ID3、C4.5决策树的建模流程CART回归树的建模流程与sklearn参数详解

    Lesson 8.3 ID3.C4.5决策树的建模流程 ID3和C4.5作为的经典决策树算法,尽管无法通过sklearn来进行建模,但其基本原理仍然值得讨论与学习.接下来我们详细介绍关于ID3和C4. ...

  3. Lesson 8.1Lesson 8.2 决策树的核心思想与建模流程CART分类树的建模流程与sklearn评估器参数详解

    Lesson 8.1 决策树的核心思想与建模流程 从本节课开始,我们将介绍经典机器学习领域中最重要的一类有监督学习算法--树模型(决策树). 可此前的聚类算法类似,树模型也同样不是一个模型,而是一类模 ...

  4. FastText 总结:文本分类、词向量训练、参数详解

    FastText:文本分类.词向量训练.参数详解 前言 - FastText 简介 一.FastText - 安装 1.1 - Github下载安装 1.2 - 编译器安装 二.FastText - ...

  5. 支持向量机之SVR 用法与参数详解 python

    1. 概念: 针对二分类问题,寻求最优超平面 SVM: 使到超平面最近的样本点的"距离"最大 SVR: 使到超平面最远的样本点的"距离"最小. SVR回归的优势 ...

  6. catboost参数详解及实战(强推)

    目录 一 参数详解 二 实战 1 导包 2 数据读取 3 贷后y标签分布,逾期率20% 4 预处理 5 特征分布 6 特征分组 7 初始参数 8 catboost建模函数 9 初始模型 10 特征重要 ...

  7. TPOT自动机器学习参数详解

    TPOT自动机器学习参数详解 分类: 代码 class tpot.TPOTClassifier(generations=100, population_size=100,offspring_size= ...

  8. CI流水线配置文件参数详解(一)

    文章目录 4. 参数详解(一) 4.1 ``script`` 4.2 ``image`` 指定使用Docker镜像.如 ``iamge:name`` ,暂时忽略. 4.3 ``before_scrip ...

  9. 内存性能参数详解(转载)

    内存性能参数详解 先说说最有效提高你机器内存性能的几个参数:CL,TRP,TRCD CAS Latency "列地址选通脉冲潜伏期" BIOS中可能的其他描述为:tCL.CAS L ...

最新文章

  1. 如何理解Android中的xmlns
  2. Redis:redis cluster的实现细节
  3. 我为什么雇佣家庭主妇做软件测试
  4. JS对象的属性名规则
  5. spring boot 初步学习
  6. 【Java SE】记录一次Java实验(多态,集合,泛型)
  7. 数据库分类与四大类NoSQL数据库
  8. Windows必备软件效率有哪些?
  9. dos窗口运行.java文件
  10. JavaWeb 简单实现客户信息管理系统
  11. Word导出带目录的PDF
  12. 树莓派(Raspberry )开机自动启动Python程序
  13. 简述人工智能的研究目标
  14. qt的cross comple相关
  15. “高定美学”品牌矩阵:「莲玉芳华」「琢我」「佐我」佐我气运系列之进击
  16. 2021全国特种设备-Q2桥式起重机司机模拟考试题库一[安考星]
  17. 76%都存在漏洞?!Docker镜像安全扫描应该这样做
  18. 【观点】如何度过自己的研究生生涯,看看院士的回答,兴许会有一些收获。
  19. Xunsearch与Sphinx的预比较
  20. 华为机试java_华为java机试面试题目大全

热门文章

  1. 信息论与编码matlab实验报告,信息论实验报告(实验三、香农编码)
  2. 微信小程序上传图片时校验所选图片是否为身份证图片
  3. ykhmi是什么触摸屏软件_YKHMI组态编程软件|中达优控触摸屏编程软件(YKBuilder)下载 v5.0.200 官方版 - 比克尔下载...
  4. 中秋征文“好文尝鲜奖”榜单公布,快来领取你的定制礼盒!
  5. 智慧农业建设方案思考
  6. 空气质量等级c语言编程,华中科技大学C语言课设 空气质量检测信息管理系统.docx...
  7. Python3 多线程编程
  8. 天津ISO9001质量管理体系认证办理流程
  9. php rename函数_php中rename()函数
  10. 元音音频时域波形与频谱的简洁分析