The Cross-Validation Procedure

In cross-validation, we run our modeling process on different subsets of the data to get multiple measures of model quality. For example, we could have 5 folds or experiments. We divide the data into 5 pieces, each being 20% of the full dataset.

We run an experiment called experiment 1 which uses the first fold as a holdout set, and everything else as training data. This gives us a measure of model quality based on a 20% holdout set, much as we got from using the simple train-test split.
We then run a second experiment, where we hold out data from the second
fold (using everything except the 2nd fold for training the model.) This
gives us a second estimate of model quality.
We repeat this process, using every fold once as the holdout. Putting
this together, 100% of the data is used as a holdout at some point.

Returning to our example above from train-test split, if we have 5000
rows of data, we end up with a measure of model quality based on 5000
rows of holdout (even if we don't use all 5000 rows simultaneously.

Trade-offs Between Cross-Validation and Train-Test Split

Cross-validation gives a more accurate measure of model quality, which is especially important if you are making a lot of modeling decisions. However, it can take more time to run, because it estimates models once for each fold. So it is doing more total work.

Given these tradeoffs, when should you use each approach? On small datasets, the extra computational burden of running cross-validation isn't a big deal. These are also the problems where model quality scores would be least reliable with train-test split. So, if your dataset is smaller, you should run cross-validation.

For the same reasons, a simple train-test split is sufficient for larger datasets. It will run faster, and you may have enough data that there's little need to re-use some of it for holdout.

There's no simple threshold for what constitutes a large vs small dataset. If your model takes a couple minute or less to run, it's probably worth switching to cross-validation. If your model takes much longer to run, cross-validation may slow down your workflow more than it's worth.

Alternatively, you can run cross-validation and see if the scores for each experiment seem close. If each experiment gives the same results, train-test split is probably sufficient.

# First we read the data
import pandas as pd
data = pd.read_csv('../input/melb_data.csv')
cols_to_use = ['Rooms', 'Distance', 'Landsize', 'BuildingArea', 'YearBuilt']
X = data[cols_to_use]
y = data.Price # Then specify a pipeline of our modeling steps (It can be very difficult to do cross-validation properly if you arent't using pipelines)
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import Imputer
my_pipeline = make_pipeline(Imputer(), RandomForestRegressor()) # Finally get the cross-validation scores:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(my_pipeline, X, y, scoring='neg_mean_absolute_error')
print(scores) # It is a little surprising that we specify negative mean absolute error in this case. Scikit-learn has a convention where all
# metrics are defined so a high number is better. Using negatives here allows them to be consistent with that convention, though
# negative MAE is almost unheard of elsewhere. print('Mean Absolute Error %2f' %(-1 * scores.mean()))

kaggle Cross-Validation的更多相关文章

  1. validation set以及cross validation的常见做法

    如果给定的样本充足,进行模型选择的一种简单方法是随机地将数据集切分成三部分,分为训练集(training set).验证集(validation set)和测试集(testing set).训练集用来 ...

  2. 交叉验证(Cross Validation)原理小结

    交叉验证是在机器学习建立模型和验证模型参数时常用的办法.交叉验证,顾名思义,就是重复的使用数据,把得到的样本数据进行切分,组合为不同的训练集和测试集,用训练集来训练模型,用测试集来评估模型预测的好坏. ...

  3. 交叉验证 Cross validation

    来源:CSDN: boat_lee 简单交叉验证 hold-out cross validation 从全部训练数据S中随机选择s个样例作为训练集training set,剩余的作为测试集testin ...

  4. Cross Validation done wrong

    Cross Validation done wrong Cross validation is an essential tool in statistical learning 1 to estim ...

  5. 交叉验证(cross validation)

    转自:http://www.vanjor.org/blog/2010/10/cross-validation/ 交叉验证(Cross-Validation): 有时亦称循环估计, 是一种统计学上将数据 ...

  6. 10折交叉验证(10-fold Cross Validation)与留一法(Leave-One-Out)、分层采样(Stratification)

    10折交叉验证 我们构建一个分类器,输入为运动员的身高.体重,输出为其从事的体育项目-体操.田径或篮球. 一旦构建了分类器,我们就可能有兴趣回答类似下述的问题: . 该分类器的精确率怎么样? . 该分 ...

  7. Cross Validation(交叉验证)

    交叉验证(Cross Validation)方法思想 Cross Validation一下简称CV.CV是用来验证分类器性能的一种统计方法. 思想:将原始数据(dataset)进行分组,一部分作为训练 ...

  8. S折交叉验证(S-fold cross validation)

    S折交叉验证(S-fold cross validation) 觉得有用的话,欢迎一起讨论相互学习~Follow Me 仅为个人观点,欢迎讨论 参考文献 https://blog.csdn.net/a ...

  9. 交叉验证(Cross Validation)简介

    参考    交叉验证      交叉验证 (Cross Validation)刘建平 一.训练集 vs. 测试集 在模式识别(pattern recognition)与机器学习(machine lea ...

  10. cross validation笔记

    preface:做实验少不了交叉验证,平时常用from sklearn.cross_validation import train_test_split,用train_test_split()函数将数 ...

随机推荐

  1. zookeeper安装搭建

    一  zookeeper介绍 因为要使用kafka,但是不想用kafka自带的,而且考虑到后面别的地方需要使用,比如分布式job,觉得独立的比较好. zookeeper目前资料一大把,但是一是我需要锻 ...

  2. grpc asp.net core 集成时一些配置的说明

    一  什么是grpc google出了一款分布式通讯框架:grpc.我想这也不是新的东西了,在13年的一个项目中,用在了数据层和业务端之间的通讯上,当时并没有觉得怎么样,因为wcf很轻松的也可以可以实 ...

  3. MMS(mongodb监控工具)

    今天好几个人问我如何查看mongodb的连接数,在mongo shell中执行: shard1:PRIMARY> db.serverStatus().connections { "cu ...

  4. ajax中error函数参数详解

    xhr.status和error函数中的status是不一样的,error函数中的status主要包括:"success"."notmodified".&quo ...

  5. Cache缓存优化

    降低数据库压力 <appSettings><add key="/></appSettings> //设置实体缓存时间 public RupengWang. ...

  6. HAproxy+Mycat

    haproxy+mycat搭建haproxy server 10.0.1.134mycat server 10.0.1.134,10.0.1.135mysql master 10.0.1.134mys ...

  7. 如何通过ISO安装win7程序

    从下载u启动工具到安装Ghost Win7系统教程 来源:http://www.uqidong.com时间:2013-08-12 09:17:38 怎样用u启动u盘启动盘安装ghost系统?u启动制作 ...

  8. Celery-4.1 用户指南: Task(任务)

    任务是构建 celery 应用的基础块. 任务是可以在任何除可调用对象外的地方创建的一个类.它扮演着双重角色,它定义了一个任务被调用时会发生什么(发送一个消息),以及一个工作单元获取到消息之后将会做什 ...

  9. mysql添加用户

    增加新用户: 格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码" 例1.增加一个用户test1密码为abc,让他可以在任 ...

  10. 初识python notes

    python数据类型 数字 字符串 列表 元祖 字典 1.为什么要编程 编程的目的是解放人力,这就需要人通过编写程序的方式计算机代替人去自动干活 2.什么是编程语言 编程语言就是人与计算机之间沟通的介 ...