通過源代碼注釋了解sklearn中train_test_split的使用


  sklearn中的train_test_split用於對數據集進行分割。如果不看文檔,網上目前的教程主要都是將屬性和標簽分別進行分割,即:將 X 和 y 划分為 X_train, X_test, y_train, y_test 。事實上,該函數可以分割任意多的數據集,以更好地滿足我們使用的需要。

首先,安裝sklearn包並導入 

from sklearn.model_selection import train_test_split

函數頭格式為:

train_test_split(*arrays, **options)

  可見,第一個參數為需要被分割的數據集,而第二個參數是一些選項。

源碼中注釋摘錄與介紹

  在函數的源代碼中,詳細地描述了各個參數的使用方法,並給出了例子,這里把源代碼中的注釋部分摘錄,並進行了部分解釋:

參數說明

    Parameters
    ----------
    *arrays : sequence of indexables with same length / shape[0]
        Allowed inputs are lists, numpy arrays, scipy-sparse
        matrices or pandas dataframes.

    test_size : float or int, default=None
        If float, should be between 0.0 and 1.0 and represent the proportion
        of the dataset to include in the test split. If int, represents the
        absolute number of test samples. If None, the value is set to the
        complement of the train size. If ``train_size`` is also None, it will
        be set to 0.25.

    train_size : float or int, default=None
        If float, should be between 0.0 and 1.0 and represent the
        proportion of the dataset to include in the train split. If
        int, represents the absolute number of train samples. If None,
        the value is automatically set to the complement of the test size.

    random_state : int or RandomState instance, default=None
        Controls the shuffling applied to the data before applying the split.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.


    shuffle : bool, default=True
        Whether or not to shuffle the data before splitting. If shuffle=False
        then stratify must be None.

    stratify : array-like, default=None
        If not None, data is split in a stratified fashion, using this as
        the class labels.

  其中,必須的參數只有 *array 。通過說明可知,train_test_split 可以接收任意數量的待分割數據集,條件是它們的長度、每條數據的形狀要相同。所以,在分割數據集時,如果拿到的是屬性和標簽在一起的一個數組,也可以直接進行分割,例如:

data_train, data_test =train_test_split(data,test_size=0.1, random_state=666)

可以將數據集data按照 9:1 分割為訓練集和測試集。

  參數 test_size、train_size指定了測試集、訓練集的大小在整個數據集中的比例。這兩個參數的類型可以是float或int。若為float型,可以取值在0~1之間,表示占比;若為int型,則表示數據集的具體大小(數據個數)。如果二者只指定了其一,另一個參數會自動設為互補值(float:1-x,int:[length of dataset]-x)。

  另外一個比較常用的參數是 random_state 。當值為0時,每次分割數據使用的隨機數都會不同。如果像上例那樣給出一個特定的種子,如果其他參數相同,每次都會得到相同的分割結果。

返回值說明

    Returns
    -------
    splitting : list, length=2 * len(arrays)
        List containing train-test split of inputs.

        .. versionadded:: 0.16
            If the input is sparse, the output will be a
            ``scipy.sparse.csr_matrix``. Else, output type is the same as the
            input type.

  返回分割好的數據集。返回數據集的數量,是 *array 中包含的數據集數量的兩倍。即,如果像上例中那樣只有一個data,將返回兩個數據集,其中訓練集在前,測試集在后,例中返回 data_train, data_test;如果像多數教程中那樣分割 X, y ,則會返回X_train, X_test, y_train, y_test 。

示例

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.model_selection import train_test_split
    >>> X, y = np.arange(10).reshape((5, 2)), range(5)
    >>> X
    array([[0, 1],
           [2, 3],
           [4, 5],
           [6, 7],
           [8, 9]])
    >>> list(y)
    [0, 1, 2, 3, 4]

    >>> X_train, X_test, y_train, y_test = train_test_split(
    ...     X, y, test_size=0.33, random_state=42)
    ...
    >>> X_train
    array([[4, 5],
           [0, 1],
           [6, 7]])
    >>> y_train
    [2, 0, 3]
    >>> X_test
    array([[2, 3],
           [8, 9]])
    >>> y_test
    [1, 4]

    >>> train_test_split(y, shuffle=False)
    [[0, 1, 2], [3, 4]]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM