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

本文涉及的产品
云原生大数据计算服务 MaxCompute,5000CU*H 100GB 3个月
云原生大数据计算服务MaxCompute,500CU*H 100GB 3个月
简介: ML之FE:特征工程中常用的五大数据集划分方法(特殊类型数据分割,如时间序列数据分割法)讲解及其代码

特殊类型数据分割


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]

 


相关实践学习
基于MaxCompute的热门话题分析
本实验围绕社交用户发布的文章做了详尽的分析,通过分析能得到用户群体年龄分布,性别分布,地理位置分布,以及热门话题的热度。
SaaS 模式云数据仓库必修课
本课程由阿里云开发者社区和阿里云大数据团队共同出品,是SaaS模式云原生数据仓库领导者MaxCompute核心课程。本课程由阿里云资深产品和技术专家们从概念到方法,从场景到实践,体系化的将阿里巴巴飞天大数据平台10多年的经过验证的方法与实践深入浅出的讲给开发者们。帮助大数据开发者快速了解并掌握SaaS模式的云原生的数据仓库,助力开发者学习了解先进的技术栈,并能在实际业务中敏捷的进行大数据分析,赋能企业业务。 通过本课程可以了解SaaS模式云原生数据仓库领导者MaxCompute核心功能及典型适用场景,可应用MaxCompute实现数仓搭建,快速进行大数据分析。适合大数据工程师、大数据分析师 大量数据需要处理、存储和管理,需要搭建数据仓库?学它! 没有足够人员和经验来运维大数据平台,不想自建IDC买机器,需要免运维的大数据平台?会SQL就等于会大数据?学它! 想知道大数据用得对不对,想用更少的钱得到持续演进的数仓能力?获得极致弹性的计算资源和更好的性能,以及持续保护数据安全的生产环境?学它! 想要获得灵活的分析能力,快速洞察数据规律特征?想要兼得数据湖的灵活性与数据仓库的成长性?学它! 出品人:阿里云大数据产品及研发团队专家 产品 MaxCompute 官网 https://www.aliyun.com/product/odps&nbsp;
相关文章
|
5月前
|
机器学习/深度学习 数据采集 分布式计算
【机器学习】Spark ML 对数据进行规范化预处理 StandardScaler 与向量拆分
标准化Scaler是数据预处理技术,用于将特征值映射到均值0、方差1的标准正态分布,以消除不同尺度特征的影响,提升模型稳定性和精度。Spark ML中的StandardScaler实现此功能,通过`.setInputCol`、`.setOutputCol`等方法配置并应用到DataFrame数据。示例展示了如何在Spark中使用StandardScaler进行数据规范化,包括创建SparkSession,构建DataFrame,使用VectorAssembler和StandardScaler,以及将向量拆分为列。规范化有助于降低特征重要性,提高模型训练速度和计算效率。
|
5月前
|
资源调度 算法 数据挖掘
R语言有限混合模型(FMM,finite mixture model)EM算法聚类分析间歇泉喷发时间
R语言有限混合模型(FMM,finite mixture model)EM算法聚类分析间歇泉喷发时间
|
12月前
|
数据采集 机器学习/深度学习 数据处理
类别数据处理:你必须知道的技巧与方法
类别数据处理:你必须知道的技巧与方法
116 0
|
JSON 算法 数据格式
优化cv2.findContours()函数提取的目标边界点,使语义分割进行远监督辅助标注
可以看到cv2.findContours()函数可以将目标的所有边界点都进行导出来,但是他的点存在一个问题,太过密集,如果我们想将语义分割的结果重新导出成labelme格式的json文件进行修正时,这就会存在点太密集没有办法进行修改,这里展示一个示例:没有对导出的结果进行修正,在labelme中的效果图。
174 0
|
机器学习/深度学习 算法 数据处理
ML之FE:数据处理—特征工程之稀疏特征的简介、如何处理、案例应用之详细攻略
ML之FE:数据处理—特征工程之稀疏特征的简介、如何处理、案例应用之详细攻略
ML之FE:数据处理—特征工程之稀疏特征的简介、如何处理、案例应用之详细攻略
|
数据可视化
ML之FE:对人类性别相关属性数据集进行数据特征分布可视化分析与挖掘
ML之FE:对人类性别相关属性数据集进行数据特征分布可视化分析与挖掘
ML之FE:对人类性别相关属性数据集进行数据特征分布可视化分析与挖掘
|
存储 机器学习/深度学习 数据采集
【GNN】task4-数据完整存储与内存的数据集类+节点预测与边预测任务实践
对于占用内存有限的数据集,我们可以将整个数据集的数据都存储到内存里。PyG为我们提供了方便的方式来构造数据完全存于内存的数据集类(简称为InMemory数据集类)。在此小节我们就将学习构造InMemory数据集类的方式。
535 0
|
机器学习/深度学习 索引 Python
ML之FE:特征工程中常用的五大数据集划分方法(特殊类型数据分割,如时间序列数据分割法)讲解及其代码
ML之FE:特征工程中常用的五大数据集划分方法(特殊类型数据分割,如时间序列数据分割法)讲解及其代码
ML之FE:基于单个csv文件数据集(自动切分为两个dataframe表)利用featuretools工具实现自动特征生成/特征衍生
ML之FE:基于单个csv文件数据集(自动切分为两个dataframe表)利用featuretools工具实现自动特征生成/特征衍生
|
机器学习/深度学习 编解码 监控
复杂场景数据集设计
随着计算机应用技术的发展,政府和企业越来越重视对于计算机视觉的应用,而多目标跟踪作为计算机视觉中具有挑战性的任务,在智能交通、智能监控和自动驾驶等多个领域起着重要的作用。多目标跟踪是对某个场景中的多个目标对象进行跟踪,但是由于场景的多变性和复杂性,以及被跟踪目标自身的各种变化,多目标跟踪会面对种种困难。
490 0
下一篇
无影云桌面