shuffle的方法们

一、利用pandas.DataFrame/Series.sample:

train_df = train_df.sample(frac=1.)  # Shuffle the data.

https://www.kaggle.com/mihaskalic/lstm-is-all-you-need-well-maybe-embeddings-also

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sample.html

https://zhuanlan.zhihu.com/p/38255793

二、利用 sklearn.utils.shuffle(*arrays, **options)

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

 

posted @ 2019-01-15 18:06  大胖子球花  阅读(218)  评论(0)    收藏  举报