shuffle#

sklearn.utils.shuffle(*arrays, random_state=None, n_samples=None)[source]#

以一致的方式打乱数组或稀疏矩阵。

这是 resample(*arrays, replace=False) 的一个便捷别名,用于对集合进行随机置换。

参数:
*arrays可索引数据结构的序列

可索引数据结构可以是数组、列表、数据帧或具有一致第一维的 SciPy 稀疏矩阵。

random_state整型、RandomState 实例或 None,默认为 None

决定数据混洗的随机数生成。传入一个整型值可以在多次函数调用中获得可重现的结果。参见术语表

n_samples整型,默认为 None

要生成的样本数量。如果留空(None),则会自动设置为数组的第一个维度。它不应大于数组的长度。

返回:
shuffled_arrays可索引数据结构的序列

集合的混洗副本序列。原始数组不受影响。

另请参见

resample

以一致的方式重新采样数组或稀疏矩阵。

示例

在同一运行中可以混合稀疏和密集数组

>>> import numpy as np
>>> X = np.array([[1., 0.], [2., 1.], [0., 0.]])
>>> y = np.array([0, 1, 2])

>>> from scipy.sparse import coo_matrix
>>> X_sparse = coo_matrix(X)

>>> from sklearn.utils import shuffle
>>> X, X_sparse, y = shuffle(X, X_sparse, y, random_state=0)
>>> X
array([[0., 0.],
       [2., 1.],
       [1., 0.]])

>>> X_sparse
<Compressed Sparse Row sparse matrix of dtype 'float64'
    with 3 stored elements and shape (3, 2)>

>>> X_sparse.toarray()
array([[0., 0.],
       [2., 1.],
       [1., 0.]])

>>> y
array([2, 1, 0])

>>> shuffle(y, n_samples=2, random_state=0)
array([0, 1])