Homer Jay Simpson
Homer Jay Simpson

Reputation: 1224

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 ?

Upvotes: 0

Views: 53

Answers (1)

pppig
pppig

Reputation: 1285

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

Upvotes: 1

Related Questions