随机采样

14 篇文章 0 订阅
6 篇文章 0 订阅

方法一: from sklearn.utils import shuffle

方法二:df.sample(frac=1)

方法三:np.random.permutation()+pd.take() 

 

import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(50).reshape(10,5),columns=['a','b','c','d','e'])
print(df)

output:
    a   b   c   d   e
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19
4  20  21  22  23  24
5  25  26  27  28  29
6  30  31  32  33  34
7  35  36  37  38  39
8  40  41  42  43  44
9  45  46  47  48  49

 

eg1:sklearn(机器学习的库)中也有shuffle的方法

from sklearn.utils import shuffle
df = shuffle(df).reset_index(drop=True)
print(df)

output:

eg2:df.sample()

df.sample(frac=1).reset_index(drop=True)

output:

eg3: np.random.permutation()+pd.take()

 

sample_df = np.random.permutation(len(df))
print(sample_df)

output:
[4 3 6 5 0 2 8 9 1 7]

 

df_new = df.take(sample_df[:5])
print(df_new)

output:
    a   b   c   d   e
4  20  21  22  23  24
3  15  16  17  18  19
6  30  31  32  33  34
5  25  26  27  28  29
0   0   1   2   3   4



参考:

https://blog.csdn.net/qq_22238533/article/details/70917102

https://blog.csdn.net/bqw18744018044/article/details/79964599

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值