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. 关于nginx访问 静态文件 403 的错误

    例如 ngixn的配置的静态文件访问 如下: location /static { root /var/app/lxxxx/web; } 1.检查所有的文件有无读权限 chmod 644 -R 2.检 ...

  2. LeetCode Maximum Average Subarray I

    原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...

  3. Android UI--提高Android UI体验

    1,自定义虚拟键盘 当一个用户被要求在一个文本框输入时希望又怎样的体验?  从用户需求来看,虚拟键盘应该改变以帮助用户输入的数据.这里是一些例子: 如果一个视图是一个电子邮件地址,一个键盘的“@”符号 ...

  4. word 2007,以不同颜色突出显示文本的快捷键,highlight命令

    命令:highlight 默认快捷键:Ctrl+Alt+H   查询或自定义快捷键的方法: 打开一个文档→单击左上角的office图标→word选项 左边的列表中选择自定义→在右边的窗口中,底部有个“ ...

  5. QAbstractSocket::connectToHost() called when already looking up or connecting/connected to

    tcpSocket_connect_HBJ->abort();//取消已有连接,重置套接字,tcpSocket_connect_HBJ是QTcpSocket类的对象 就不会报错了.

  6. hl7 v2.X 版本中RSP_K23消息的构造

    RSP_K23消息有MSH, MSA, ERR, QAK, QPD, PID几个segment,其中ERR,PID为可选. 1. 当MSA有err时,ERR段填充出错的详细信息. 2. 当MSA为AA ...

  7. bash 中的行处理命令 awk

    转自:http://blog.chinaunix.net/uid-23302288-id-3785105.html

  8. java的reflection和introspector

    JAVA反射机制是在运行状态中,对于任意一个类,都能够得到这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制 ...

  9. 使用script转储终端命令输出,或者录制并播放session的内容

    摘自:http://leohot.blog.163.com/blog/static/1348656022012729113658473/ 注意: 1. 启动script时没有指定文件名,它会自动记录到 ...

  10. UE4异步载入资源

    转自:http://blog.ch-wind.com/ue4%E5%BC%82%E6%AD%A5%E8%BD%BD%E5%85%A5%E8%B5%84%E6%BA%90/ 所有的“硬”指针指向的资源都 ...