DataFrame随机采样
from pandas import DataFrame,Series import pandas as pd import numpy as np # 使用numpy.random.permutation可实现对Series或DataFrame的列排列 df = DataFrame(np.arange(5*4).reshape(5,4)) print(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 ''' sampler = np.random.permutation(5) # 随机采样 print(sampler) ''' [3 4 1 0 2] ''' print(df.take(sampler)) ''' 0 1 2 3 3 12 13 14 15 4 16 17 18 19 1 4 5 6 7 0 0 1 2 3 2 8 9 10 11 '''
import numpy as np bag = np.array([5,7,-1,6,4]) sampler = np.random.randint(0,len(bag),size=10) print(sampler) # [0 3 4 2 1 2 0 1 1 4] draws = bag.take(sampler) print(draws) #[ 5 6 4 -1 7 -1 5 7 7 4]
本文来自博客园,作者:OTAKU_nicole,转载请注明原文链接:https://www.cnblogs.com/nicole-zhang/p/14959452.html