简体   繁体   中英

R to Python translation problem in element wise logical condition

I have the following code in 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

Trying to translate it in Python I am doing:

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))

but I receive an error:

TypeError: 'int' object is not iterable

How I can reproduce the same result in Python?

The sum function receives arguments in the wrong order.

After changing sum(la1,unifvec < 0.5) to sum(unifvec < 0.5, la1) it works fine.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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