繁体   English   中英

R 到 Python 元素逻辑条件下的翻译问题

[英]R to Python translation problem in element wise logical condition

我在 R 中有以下代码:

N = 100 # number of data points
unifvec = runif(N)
d1 = rpois(sum(unifvec < 0.5),la1);d1
 [1] 3 1 1 0 0 0 0 2 1 1 1 0 2 1 0 1 2 0 1 0 1 1 0 0 1 1 0 1 1 3 0
[32] 2 2 1 4 0 1 0 1 1 1 1 3 0 0 2 0 1 1 1 1 3

试图在 Python 中翻译它我正在做:

la1 = 1
N = 100 # number of data points
unifvec = np.random.uniform(0,1,N)
d1 = np.random.poisson(la1,sum(la1,unifvec < 0.5))

但我收到一个错误:

TypeError: 'int' object is not iterable

如何在 Python 中重现相同的结果?

sum function 以错误的顺序接收 arguments。

sum(la1,unifvec < 0.5)更改为sum(unifvec < 0.5, la1)后,它工作正常。

import numpy as np

la1 = 1
N = 100  # number of data points
unifvec = np.random.uniform(0, 1, N)
d1 = np.random.poisson(la1, sum(unifvec < 0.5, la1))

暂无
暂无

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

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