虽然 Scikit-Learn 有可以划分数据集的函数 train_test_split ,但在有些特殊情况我们只希望它将 DataFrame 数据直接划分为 train, test 而不是像 train_test_split 返回四个值。这里写了一个类似功能的函数:

import numpy as np
import pandas as pd
from sklearn.utils import shuffle as reset def train_test_split(data, test_size=0.3, shuffle=True, random_state=None):
'''Split DataFrame into random train and test subsets Parameters
----------
data : pandas dataframe, need to split dataset. test_size : float
If float, should be between 0.0 and 1.0 and represent the
proportion of the dataset to include in the train split. random_state : int, RandomState instance or None, optional (default=None)
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by `np.random`. shuffle : boolean, optional (default=None)
Whether or not to shuffle the data before splitting. If shuffle=False
then stratify must be None.
''' if shuffle:
data = reset(data, random_state=random_state) train = data[int(len(data)*test_size):].reset_index(drop = True)
test = data[:int(len(data)*test_size)].reset_index(drop = True) return train, test

效果如下:

函数实现将 DataFrame 数据直接划分为测试集训练集的更多相关文章

  1. LUA中将未分类数据分为测试集和训练集

    require 'torch' require 'image' local setting = {parent_root = '/home/pxu/image'} function list_chil ...

  2. Matlab划分测试集和训练集

    % x是原数据集,分出训练样本和测试样本 [ndata, D] = size(X); %ndata样本数,D维数 R = randperm(ndata); %1到n这些数随机打乱得到的一个随机数字序列 ...

  3. Machine Learning笔记整理 ------ (二)训练集与测试集的划分

    在实际应用中,一般会选择将数据集划分为训练集(training set).验证集(validation set)和测试集(testing set).其中,训练集用于训练模型,验证集用于调参.算法选择等 ...

  4. spark 将dataframe数据写入Hive分区表

    从spark1.2 到spark1.3,spark SQL中的SchemaRDD变为了DataFrame,DataFrame相对于SchemaRDD有了较大改变,同时提供了更多好用且方便的API.Da ...

  5. 将DataFrame数据如何写入到Hive表中

    1.将DataFrame数据如何写入到Hive表中?2.通过那个API实现创建spark临时表?3.如何将DataFrame数据写入hive指定数据表的分区中? 从spark1.2 到spark1.3 ...

  6. JVM 运行时数据区域划分

    目录 前言 什么是JVM JRE/JDK/JVM是什么关系 JVM执行程序的过程 JVM的生命周期 JVM垃圾回收 JVM的内存区域划分 一.运行时数据区包括哪几部分? 二.运行时数据区的每部分到底存 ...

  7. python重要的第三方库pandas模块常用函数解析之DataFrame

    pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...

  8. Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据

    Title:Linux C 调用MYSQL API 函数mysql_escape_string()转义插入数据 --2013-10-11 11:57 #include <stdio.h> ...

  9. SQL Server 基础 04 函数与分组查询数据

    函数与分组查询数据 系统函数分 聚合函数.数据类型转换函数.日期函数.数学函数 . . . 1. 聚合函数 主要是对一组值进行计算,然后返回一个值. 聚合函数包括 sum(求和).avg(求平均值). ...

随机推荐

  1. Shell if 参数含义列表

    [ -a FILE ]  如果 FILE 存在则为真.   [ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真.   [ -c FILE ]  如果 FILE 存在且是一个字特殊文 ...

  2. 混沌映射初始化种群之Logistic映射

    Logstic混沌映射初始化种群 Step 1:     随机生成一个\(d\)维向量\({X_0}\),向量的每个分量在0-1之间. Step 2:     利用Logistic映射生成N个向量.L ...

  3. heap exploit about ptmalloc in glibc version 2.31

    学习的一下高版本的libc的利用方式. 项目地址:https://github.com/StarCross-Tech/heap_exploit_2.31 tcache_dup 源代码: 1 #incl ...

  4. GoLang设计模式17 - 访客模式

    说明 访客模式是一种行为型设计模式.通过访客模式可以为struct添加方法而不需要对其做任何调整. 来看一个例子,假如我们需要维护一个对如下形状执行操作的库: 方形(Square) 圆形(Circle ...

  5. 自定义日历(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 日历有三种:标准日历.24小时日历和夜班日历. 但这三种在现实中远远不够用,别的不说,就说那个标准日历,默认是8点到12点 ...

  6. 初识requests

    Make a Request 一开始要导入 Requests 模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取 Github 的公共时间线 ...

  7. MySQLs数据库建外键时自动跑到缩影处,真奇怪

    MySQLs数据库建外键时自动跑到缩影处,真奇怪MyISAM引擎不支持外键:InnoDB存储引擎支持外键.如何解决的,把表修改成innodb类型吧用的工具是SQLyog Ultimate如图所示:

  8. 20款GitHub上优秀的Go开源项目

    docker 无人不知的虚拟华平台,开源的应用容器引擎,借助该引擎,开发者可以打包他们的应用,移植到任何平台上. https://github.com/docker/docker 38154 star ...

  9. 资源分享 | PyTea:不用运行代码,静态分析pytorch模型的错误

    ​  前言  ​​​​​​​本文介绍一个Pytorch模型的静态分析器 PyTea,它不需要运行代码,即可在几秒钟之内扫描分析出模型中的张量形状错误.文末附使用方法. 本文转载自机器之心 编辑:CV技 ...

  10. layui(layer)的loading方法显示位置不居中

    要在layer.load之前使用layer.ready方法 layui.use('layer', function () { layer.ready(function(){ index = layer ...