shuffle#
- sklearn.utils.shuffle(*arrays, random_state=None, n_samples=None)[source]#
以一致的方式打乱数组或稀疏矩阵。
这是一个便捷别名,相当于
resample(*arrays, replace=False),用于对集合进行随机排列。- 参数:
- *arrays可索引数据结构的序列
可索引的数据结构可以是数组、列表、数据框(dataframe)或具有一致第一维度的 scipy 稀疏矩阵。
- random_stateint, RandomState instance or None, default=None
决定用于打乱数据的随机数生成。传入一个整数以在多次函数调用中获得可重现的结果。参见 术语表。
- n_samplesint, default=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_array >>> X_sparse = coo_array(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 array 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])