當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python sklearn shuffle用法及代碼示例

本文簡要介紹python語言中 sklearn.utils.shuffle 的用法。

用法:

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

以一致的方式洗牌數組或稀疏矩陣。

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

參數

*arrays可轉位序列data-structures

可索引data-structures 可以是具有一致第一維的數組、列表、數據幀或 scipy 稀疏矩陣。

random_stateint、RandomState 實例或無,默認=無

確定用於打亂數據的隨機數生成。傳遞 int 以獲得跨多個函數調用的可重現結果。請參閱術語表。

n_samples整數,默認=無

要生成的樣本數。如果保留為 None,則會自動將其設置為數組的第一維。它不應大於數組的長度。

返回

shuffled_arrays可轉位序列data-structures

集合的洗牌副本的序列。原始數組不受影響。

例子

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

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

相關用法


注:本文由純淨天空篩選整理自scikit-learn.org大神的英文原創作品 sklearn.utils.shuffle。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。