随机取样:
~
#df = pd.DataFrame(np.arange(5*4).reshape((5, 4)))
df:
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
4 16 17 18 19
~
#随机生成新顺序的整数数组--permutation函数
sam = np.random.permutation(5)
[1 3 4 2 0]
~
#根据随机序列 提取出新的数组
print(df.take(sam, axis=0))
0 1 2 3
1 4 5 6 7
3 12 13 14 15
4 16 17 18 19
2 8 9 10 11
0 0 1 2 3
~
#随机取样
#随机提取三行数据
print(df.sample(n=3, axis=0))
0 1 2 3
2 8 9 10 11
0 0 1 2 3
1 4 5 6 7
~
#通过替换的方式随机取样
ch = pd.Series([5, 7, 1, 6, 3])
print(ch)
print(ch.sample(n=10, replace=True)) #拿出 放回 再随机拿出 再放回
0 5
1 7
2 1
3 6
4 3
dtype: int64
1 7
3 6
0 5
3 6
1 7
4 3
4 3
0 5
3 6
3 6
dtype: int64
Process finished with exit code 0