I'm trying to port some code from Python to R and I've come across a use of for
in the Python code I've not seen before and I can't find answers by googling:
The line of code is:
logp = [theta[np.newaxis, ...] for theta in thetai]
I understand the theta[np.newaxis, ...]
part, but I cannot figure out the for
clause after it for a few reasons
I've made a small reproducible example here:
import numpy as np
theta = np.random.rand(5, 2, 2, 3)
thetai = theta[0]
logp = [theta[np.newaxis, ...] for theta in thetai]
Produces this output:
logp
Out[406]:
[array([[[0.305, 0.071, 0.483],
[0.005, 0.627, 0.24 ]]]),
array([[[0.648, 0.524, 0.254],
[0.257, 0.367, 0.796]]])]
I don't understand a couple of things.
1. What is the for clause doing - is it a means to subset the array?
2. How can you for theta
in thetai
- when thetai
is actually a subset of theta
?
Appreciate any assistance here, many thanks in advance.