This documentation is for scikit-learn version 0.11-gitOther versions

Citing

If you use the software, please consider citing scikit-learn.

This page

8.3.9. sklearn.cross_validation.train_test_split

sklearn.cross_validation.train_test_split(*arrays, **options)

Split arrays or matrices into random train and test subsets

Quick utility that wraps calls to check_arrays and iter(ShuffleSplit(n_samples)).next() and application to input data into a single call for splitting (and optionally subsampling) data in a oneliner.

Parameters :

*arrays : sequence of arrays or scipy.sparse matrices with same shape[0]

Python lists or tuples occurring in arrays are converted to 1D numpy arrays.

test_fraction : float (default 0.25)

Should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split.

train_fraction : float or None (default is None)

Should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If None, the value is automatically set to the complement of the test fraction.

random_state : int or RandomState

Pseudo-random number generator state used for random sampling.

dtype : a numpy dtype instance, None by default

Enforce a specific dtype.

Examples

>>> import numpy as np
>>> from sklearn.cross_validation import train_test_split
>>> a, b = np.arange(10).reshape((5, 2)), range(5)
>>> a
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])
>>> b
[0, 1, 2, 3, 4]
>>> a_train, a_test, b_train, b_test = train_test_split(
...     a, b, test_fraction=0.33, random_state=42)
...
>>> a_train
array([[4, 5],
       [0, 1],
       [6, 7]])
>>> b_train
array([2, 0, 3])
>>> a_test
array([[2, 3],
       [8, 9]])
>>> b_test
array([1, 4])