sklearn.utils.shuffle?

sklearn.utils.shuffle(*arrays, **options)

源碼

以一致的方式隨機排列數組或稀疏矩陣

這是一個方便的別名,用于resample(*arrays, replace=False)對集合進行隨機排列。

參數 說明
*arrays sequence of indexable data-structures
可索引的數據結構可以是數組、列表、數據矩陣或具有一致第一維的稀疏矩陣。
返回值 說明
shuffled_arrays sequence of indexable data-structures
集合中打亂的副本的序列。原始數組不受影響。
其他參數 說明
random_state int, RandomState instance or None, optional (default=None)
確定用于對數據進行混排的隨機數生成。 在多個函數調用之間傳遞int以獲得可重復的結果。 請參閱詞匯表
n_samples int, None by default
要生成的樣本數量。如果為空,則自動將其設置為數組的第一個維度。

另見:

sklearn.utils.resample

示例:

可以在同一運行中混合使用稀疏數組和密集數組:

>>> X = np.array([[1.0.], [2.1.], [0.0.]])
>>> y = np.array([012])

>>> 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([210])

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