shuffle#
- sklearn.utils.shuffle(*arrays, random_state=None, n_samples=None)[原始碼]#
以一致的方式打亂陣列或稀疏矩陣。
這是
resample(*arrays, replace=False)
的方便別名,用於對集合進行隨機排列。- 參數:
- *arrays可索引的資料結構序列
可索引的資料結構可以是陣列、列表、資料框架或具有一致第一維度的 scipy 稀疏矩陣。
- random_stateint,RandomState 實例或 None,預設值為 None
決定打亂資料的隨機數生成。傳遞 int 可在多次函數呼叫中獲得可重複的結果。請參閱詞彙表。
- n_samplesint,預設值為 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])