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時,每次分割資料使用的亂數都會不同,如果像上例那樣給出一個種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]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/145724.html
標籤:Python
上一篇:python監聽、操作鍵盤滑鼠庫pynput詳細教程
下一篇:爬蟲
