繁体   English   中英

具有不同标准偏差和每行平均值的 Numpy 数组

[英]Numpy array with different standard deviation and mean per row

我有一个包含两列的熊猫数据框。 它们代表平均值和标准偏差。

如何执行矢量化采样? 我想每行采样 1 个观察值。

import numpy as np
import pandas as pd

rng = np.random.RandomState(0)

#n_points = 4_000_000
n_points = 10
d_dimensions = 2

X = rng.random_sample((n_points, d_dimensions))

df = pd.DataFrame(X)
display(df.head())

df['raondomized'] = df.apply(lambda x: np.random.normal(x[0], x[1], 1), axis = 1)
df.head()

当记录数增加时,速度很慢。

每行具有不同标准偏差的Numpy数组

np.random.seed(444) arr = np.random.normal(loc=0., scale=[1., 2., 3.], size=(1000, 3)).T print(arr.mean(axis=1)) # [-0.06678394 -0.12606733 -0.04992722] print(arr.std(axis=1)) # [0.99080274 2.03563299 3.01426507]

展示如何以相同的方式执行矢量化采样 - 如何将其更改为支持不同的方式,就像我使用apply天真版本一样,但速度更快?

A:

np.random.normal(df[0], df[1], 1)

即使指定了多个均值/标准差,也仅返回单个标量值。

df['raondomized'] = np.random.normal(df[0], df[1])

重要的是不要指定元素的数量。

怎么样

np.random.normal(df[0], df[1], len(df))

您还可以指定每个规范的运行次数(例如 1000),

np.random.normal(df[0], df[1], (1000, len(df)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM