ML之FE:特征工程中常用的五大数据集划分方法(特殊类型数据分割,如时间序列数据分割法)讲解及其代码

目录

特殊类型数据分割

5.1、时间序列数据分割TimeSeriesSplit


特殊类型数据分割

5.1、时间序列数据分割TimeSeriesSplit

class TimeSeriesSplit Found at: sklearn.model_selection._split

class TimeSeriesSplit(_BaseKFold):

"""Time Series cross-validator .. versionadded:: 0.18

Provides train/test indices to split time series data samples that are observed at fixed time intervals, in train/test sets. In each split, test indices must be higher than before, and thus shuffling in cross validator is inappropriate. This cross-validation object is a variation of :class:`KFold`.  In the kth split, it returns first k folds as train set and the (k+1)th fold as test set.

Note that unlike standard cross-validation methods, successive training sets are supersets of those that come before them.

Read more in the :ref:`User Guide <cross_validation>`.

Parameters

----------

n_splits : int, default=5. Number of splits. Must be at least 2. .. versionchanged:: 0.22 . ``n_splits`` default value changed from 3 to 5.

max_train_size : int, default=None. Maximum size for a single training set.

提供训练/测试索引,以分割时间序列数据样本,在训练/测试集中,在固定的时间间隔观察。在每次分割中,测试索引必须比以前更高,因此在交叉验证器中变换是不合适的。这个交叉验证对象是KFold 的变体。在第k次分割中,它返回第k次折叠作为序列集,返回第(k+1)次折叠作为测试集。

注意,与标准的交叉验证方法不同,连续训练集是之前那些训练集的超集。

更多信息请参见:ref: ' User Guide <cross_validation> '。</cross_validation>

参数

----------

n_splits :int,默认=5。数量的分裂。必须至少是2. ..versionchanged:: 0.22。' ' n_split ' ' '默认值从3更改为5。

max_train_size : int,默认None。单个训练集的最大容量。

Examples

--------

>>> import numpy as np

>>> from sklearn.model_selection import TimeSeriesSplit

>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]])

>>> y = np.array([1, 2, 3, 4, 5, 6])

>>> tscv = TimeSeriesSplit()

>>> print(tscv)

TimeSeriesSplit(max_train_size=None, n_splits=5)

>>> for train_index, test_index in tscv.split(X):

...     print("TRAIN:", train_index, "TEST:", test_index)

...     X_train, X_test = X[train_index], X[test_index]

...     y_train, y_test = y[train_index], y[test_index]

TRAIN: [0] TEST: [1]

TRAIN: [0 1] TEST: [2]

TRAIN: [0 1 2] TEST: [3]

TRAIN: [0 1 2 3] TEST: [4]

TRAIN: [0 1 2 3 4] TEST: [5]

Notes

-----

The training set has size ``i * n_samples // (n_splits + 1) + n_samples % (n_splits + 1)`` in the ``i``th split, with a test set of size ``n_samples//(n_splits + 1)``, where ``n_samples`` is the number of samples.

"""

@_deprecate_positional_args

def __init__(self, n_splits=5, *, max_train_size=None):

super().__init__(n_splits, shuffle=False, random_state=None)

self.max_train_size = max_train_size

def split(self, X, y=None, groups=None):

"""Generate indices to split data into training and test set.

Parameters

----------

X : array-like of shape (n_samples, n_features). Training data, where n_samples is the number of samples and n_features is the number of features.

y : array-like of shape (n_samples,). Always ignored, exists for compatibility.

groups : array-like of shape (n_samples,). Always ignored, exists for compatibility.

Yields

------

train : ndarray. The training set indices for that split.

test : ndarray. The testing set indices for that split.

"""

X, y, groups = indexable(X, y, groups)

n_samples = _num_samples(X)

n_splits = self.n_splits

n_folds = n_splits + 1

if n_folds > n_samples:

raise ValueError(

("Cannot have number of folds ={0} greater than the number of samples: {1}."). format(n_folds, n_samples))

indices = np.arange(n_samples)

test_size = n_samples // n_folds

test_starts = range(test_size + n_samples % n_folds, n_samples,

test_size)

for test_start in test_starts:

if self.max_train_size and self.max_train_size < test_start:

yield indices[test_start - self.max_train_size:test_start], indices

[test_start:test_start + test_size]

else:

yield indices[:test_start], indices[test_start:test_start + test_size]

Examples

--------

>>> import numpy as np

>>> from sklearn.model_selection import TimeSeriesSplit

>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]])

>>> y = np.array([1, 2, 3, 4, 5, 6])

>>> tscv = TimeSeriesSplit()

>>> print(tscv)

TimeSeriesSplit(max_train_size=None, n_splits=5)

>>> for train_index, test_index in tscv.split(X):

...     print("TRAIN:", train_index, "TEST:", test_index)

...     X_train, X_test = X[train_index], X[test_index]

...     y_train, y_test = y[train_index], y[test_index]

TRAIN: [0] TEST: [1]

TRAIN: [0 1] TEST: [2]

TRAIN: [0 1 2] TEST: [3]

TRAIN: [0 1 2 3] TEST: [4]

TRAIN: [0 1 2 3 4] TEST: [5]

Notes

-----

The training set has size ``i * n_samples // (n_splits + 1) + n_samples % (n_splits + 1)`` in the ``i``th split, with a test set of size ``n_samples//(n_splits + 1)``, where ``n_samples`` is the number of samples.

 

"""

@_deprecate_positional_args

def __init__(self, n_splits=5, *, max_train_size=None):

super().__init__(n_splits, shuffle=False, random_state=None)

self.max_train_size = max_train_size

def split(self, X, y=None, groups=None):

"""Generate indices to split data into training and test set.

Parameters

----------

X : array-like of shape (n_samples, n_features). Training data, where n_samples is the number of samples and n_features is the number of features.

y : array-like of shape (n_samples,). Always ignored, exists for compatibility.

groups : array-like of shape (n_samples,). Always ignored, exists for compatibility.

Yields

------

train : ndarray. The training set indices for that split.

test : ndarray. The testing set indices for that split.

"""

X, y, groups = indexable(X, y, groups)

n_samples = _num_samples(X)

n_splits = self.n_splits

n_folds = n_splits + 1

if n_folds > n_samples:

raise ValueError(

("Cannot have number of folds ={0} greater than the number of samples: {1}."). format(n_folds, n_samples))

indices = np.arange(n_samples)

test_size = n_samples // n_folds

test_starts = range(test_size + n_samples % n_folds, n_samples,

test_size)

for test_start in test_starts:

if self.max_train_size and self.max_train_size < test_start:

yield indices[test_start - self.max_train_size:test_start], indices

[test_start:test_start + test_size]

else:

yield indices[:test_start], indices[test_start:test_start + test_size]

 

ML之FE:特征工程中常用的五大数据集划分方法(特殊类型数据分割,如时间序列数据分割法)讲解及其代码相关推荐

  1. ML之FE:特征工程中常用的一些处理手段(缺失值填充、异常值检测等)及其对应的底层代码的实现

    ML之FE:特征工程中常用的一些处理手段(缺失值填充.异常值检测等)及其对应的底层代码的实现 目录 特征工程中常用的一些处理手段(缺失值填充.异常值检测等)及其对应的底层代码的实现

  2. 机器学习-特征工程中的样本不均衡处理方法

    如果你才开始学习机器学习,那么你使用过的数据集很大程度上都是简单.普通.规整的.其中一个原因就是,当你构建分类器时,样本类都是平衡的.在教学中,数据集通常是处理过的,这样才能够把注意力集中在特定算法或 ...

  3. BPR项目实施中常用的五大技术和方法 (转载)

    对于一个企业来说,BPR是一个重大而复杂的系统工程,在项目实施过程中涉及到多方面的活动和工作.参与企业信息化的成员在整个BPR过程中,不但应当知道如何进行BPR,由谁来进行BPR,而且还需要了解一些进 ...

  4. BPR项目实施中常用的五大技术和方法(转载)

    对于一个企业来说,BPR是一个重大而复杂的系统工程,在项目实施过程中涉及到多方面的活动和工作.参与企业信息化的成员在整个BPR过程中,不但应当知道如何进行BPR,由谁来进行BPR,而且还需要了解一些进 ...

  5. ML之FE:特征工程中的特征拼接处理(常用于横向拼接自变量特征和因变量特征)(daiding)

    ML之FE:特征工程中的特征拼接处理(常用于横向拼接自变量特征和因变量特征) 目录 特征工程中的特征拼接处理(常用于横向拼接自变量特征和因变量特征) 输出结果 实现代码 特征工程中的特征拼接处理(常用 ...

  6. ML之FE:特征工程中数据缺失值填充的简介、方法、全部代码实现之详细攻略

    ML之FE:特征工程中数据缺失值填充的简介.方法.全部代码实现之详细攻略 目录 特征工程中数据缺失值填充的简介.方法.经典案例

  7. ML之FE:基于FE特征工程对RentListingInquries数据集进行预处理并导出为三种格式文件(csv格式/txt格式/libsvm稀疏txt格式)

    ML之FE:基于FE特征工程对RentListingInquries数据集进行预处理并导出为三种格式文件(csv格式/txt格式/libsvm稀疏txt格式) 目录 输出结果 设计思路 核心代码 输出 ...

  8. ML之FE:利用FE特征工程(分析两两数值型特征之间的相关性)对AllstateClaimsSeverity(Kaggle2016竞赛)数据集实现索赔成本值的回归预测

    ML之FE:利用FE特征工程(分析两两数值型特征之间的相关性)对AllstateClaimsSeverity(Kaggle2016竞赛)数据集实现索赔成本值的回归预测 目录 输出结果 设计思路 核心代 ...

  9. ML之FE:利用FE特征工程(单个特征及其与标签关系的可视化)对RentListingInquries(Kaggle竞赛)数据集实现房屋感兴趣程度的多分类预测

    ML之FE:利用FE特征工程(单个特征及其与标签关系的可视化)对RentListingInquries(Kaggle竞赛)数据集实现房屋感兴趣程度的多分类预测 目录 输出结果 设计思路 核心代码 输出 ...

最新文章

  1. C++ new 的使用
  2. iOS 相册和网络图片的存取
  3. BETA:由清华大学等多家研究机构联合提出的面向SSVEP-BCI应用程序的大型基准测试数据库...
  4. C语言:关键字volatile详解!
  5. Shrio Unable to execute ‘doFinal‘ with cipher instance
  6. VS2017桌面应用程序打包成.msi或者.exe
  7. Python 奇技淫巧
  8. .NET Framework 如何:提高性能
  9. XCode10 swift4.2 适配遇到的坑
  10. [导入]SQL Injection cheat sheet
  11. 如何保持婚姻的新鲜感?
  12. python中英文字频率_python统计文本字符串里单词出现频率的方法
  13. SCUT - 290 - PARCO的因数游戏 - 博弈论
  14. 计算机网络基础 习题,计算机网络基础练习题集.pdf
  15. java发微信字体颜色_java微信公众号发送消息模板
  16. Win10家庭中文版如何添加本地用户
  17. 工控随笔_08_西门子_Win10安装Step7.V5.6中文版授权管理器不能正常启动
  18. 章文嵩-构建云计算平台的实践
  19. Python 微信自动化工具开发系列01_自动获取微信聊天信息(2023年1月可用)
  20. java毕业设计校园快递柜存取件系统(附源码、数据库)

热门文章

  1. java与 C++ 之间进行 SOCKET 通讯要点简要解析
  2. python dlib学习(七):人脸特征点对齐
  3. python dlib学习(一):人脸检测
  4. Retrofit解析网页Json数据简单实例
  5. [Web 前端] mobx教程(二)-mobx主要概念
  6. dede 会员中心编辑添加和修改图集的时候自定义的字段模型显示不出来的问题...
  7. Linux正則表達式-定位元字符
  8. shell 脚本执行 sql
  9. 高性能服务器架构思路「不仅是思路」
  10. 分布式锁选型背后的架构设计思维【附源码】