[英]np.random.choice doesn't do sampling as designated probabilities.
I'm trying to resample my sample data to calculate bootstrap standard error. 我正在尝试对样本数据重新采样以计算引导程序标准错误。 But the results don't match the designated probabilities I made.
但是结果与我确定的概率不符。
for 'p' in numpy.random.choice(a, size=None, replace=True, p=None), I assinged a list of probabilities which is 对于numpy.random.choice(a,size = None,replace = True,p = None)中的'p',我提出了一个概率列表
[0.190872103, 0.120820803, 0.115160092, 0.008137272, 0.029541836, 0.0, 0.535467893, 0.0] for ['neutral', 'happy', 'sad', 'surprise', 'fear', 'disgust', 'anger','contempt'] each. [[中性],[快乐],[悲伤],[惊奇],[恐惧],[厌恶],[愤怒],[鄙视] [0.190872103、0.120820803、0.115160092、0.008137272、0.029541836、0.0、0.535467893、0.0] ]每个。
data = pd.read_csv(path+'shawshank_FER_entropy.csv', encoding = 'utf-8', delimiter='\t')
emo_list = ['neutral', 'happy', 'sad', 'surprise', 'fear', 'disgust', 'anger','contempt']
pb = data.andy
p = [float(pb.iloc[11]),float(pb.iloc[12]),float(pb.iloc[13]),float(pb.iloc[14]),float(pb.iloc[15]),float(pb.iloc[16]),float(pb.iloc[17]),float(pb.iloc[18])]
print(p)
emo_sample = np.random.choice(emo_list, 1000, p)
print(emo_sample)
unique, counts = np.unique(emo_sample, return_counts=True)
print(np.asarray((unique, counts)).T)
I expected results to be 1000 emotion words distributed as the probability I designated, but the results are uniformly distributed as below. 我希望结果是作为我指定的概率分布的1000个情感词,但是结果如下所示均匀分布。
[['anger' '128'] ['contempt' '140'] ['disgust' '101'] ['fear' '134'] ['happy' '121'] ['neutral' '120'] ['sad' '123'] ['surprise' '133']]
[['愤怒''128'] ['蔑视''140'] ['厌恶'101'] ['恐惧''134'] ['快乐''121'] ['中立''120'] [ 'sad''123'] ['surprise''133']]
Can you explain why my codes don't use the probability I specified? 您能解释为什么我的代码不使用我指定的概率吗?
The call signature of numpy.random.choice is: numpy.random.choice 的呼叫签名为:
numpy.random.choice(a, size=None, replace=True, p=None)
Notice that p
is the 4th parameter, not the 3rd. 请注意,
p
是第4个参数,而不是第3个。 So emo_sample = np.random.choice(emo_list, 1000, p)
is assigning p
to the replace
parameter instead of the p
parameter: 因此
emo_sample = np.random.choice(emo_list, 1000, p)
将p
分配给replace
参数而不是p
参数:
numpy.random.choice(a, size=None, replace=p, p=None)
One way to fix this is to use keyword parameters: 解决此问题的一种方法是使用关键字参数:
emo_sample = np.random.choice(emo_list, 1000, p=p)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.