gpt4 book ai didi

python - 使用来自 pythons pandas 数据帧的数据从正态分布中采样

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:42 28 4
gpt4 key购买 nike

我正在尝试使用存储在 pandas DataFrames 中的均值和标准差从正态分布中采样。

例如:

means= numpy.arange(10)
means=means.reshape(5,2)

产生:

   0  1
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9

和:

sts=numpy.arange(10,20)
sts=sts.reshape(5,2)

产生:

    0   1
0 10 11
1 12 13
2 14 15
3 16 17
4 18 19

我将如何生成另一个具有相同形状但使用相应均值和标准差从正态分布中采样的值的 pandas 数据框。

即这个新数据帧的位置 0,0 将从具有 mean=0standard deviation=10 的正态分布中采样,依此类推。

到目前为止我的功能:

    def make_distributions(self):
num_data_points,num_species= self.means.shape
samples=[]
for i,j in zip(self.means,self.stds):
for k,l in zip(self.means[i],self.stds[j]):
samples.append( numpy.random.normal(k,l,self.n) )

将为我从分布中抽样,但我很难将数据放回与均值和标准差 dfs 相同形状的数据框中。有人对如何执行此操作有任何建议吗?

提前致谢。

最佳答案

您可以使用 numpy.random.normal从随机正态分布中抽样。
IIUC,那么这可能是最简单的,利用 broadcasting :

import numpy as np
np.random.seed(1) # only for demonstration
np.random.normal(means,sts)

array([[ 16.24345364, -5.72932055],
[ -4.33806103, -10.94859209],
[ 16.11570681, -29.52308045],
[ 33.91698823, -5.94051732],
[ 13.74270373, 4.26196287]])

检查它是否有效:

np.random.seed(1)
print np.random.normal(0,10)
print np.random.normal(1,11)

16.2434536366
-5.72932055015

如果您需要 pandas DataFrame:

import pandas as pd
pd.DataFrame(np.random.normal(means,sts))

关于python - 使用来自 pythons pandas 数据帧的数据从正态分布中采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36086650/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com