shuffle#

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

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

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

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

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

random_stateint, RandomState 实例或 None, 默认=None

确定用于打乱数据的随机数生成。 传递一个 int 以在多次函数调用中获得可重现的结果。 参见 术语表

n_samplesint, 默认=None

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

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

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

See also

resample

一致地重采样数组或稀疏矩阵。

Examples

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

>>> 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
<3x2 sparse matrix of type '<... 'numpy.float64'>'
    with 3 stored elements in Compressed Sparse Row format>

>>> 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])