1

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.

user2498193
  • 1,072
  • 2
  • 13
  • 32
  • Please reopen this question. Only part of the problem is answered by the list comprehension question referred to. The second part of the question is about why ```for bigthing in smallthing``` works. This seems to be linked to the list comprehension expression, as expanding the for loop expression seems to change the behaviour – user2498193 May 17 '20 at 10:43

1 Answers1

2

It is called list comprehension in python. The basic idea is to write statements in more compact way. Instead of

arr = []
for x in range(10):
  arr.append(x)

we can write

arr = [ x for x in range(10)]

Check out more on this here.

Kishor
  • 450
  • 8
  • 11
  • Thanks. But also in my example I dont' get the order of the for loop - it is ```for bigthing in smallthing``` but somehow works ? – user2498193 May 16 '20 at 17:29
  • 1
    @user2498193 I don't know much about numpy but theta has shape of (2,3) and thetai has (2,2,3) which clearly means thetai is a 'bigthing' and theta is a 'smallthing'. You can always use print statements and theta.shape to check your arrays and dimensions. Hope it helps. – Kishor May 17 '20 at 02:21
  • ```theta``` has shape (5,2,2,3) as declared in the ```np.random.rand``` statement. The list comprehension code maintains those dimensions. Note that when I express the code in a for loop ```for theta in thetai: theta[np.newaxis, ...]``` this changes the shape of theta somehow to be (2,3). So either I've coded that wrong, or the list comprehension expression is evaulated differently somehow – user2498193 May 17 '20 at 10:46
  • I will just open a new question for this – user2498193 May 18 '20 at 09:03
  • 1
    @user2498193 Sorry I couldn't answer your question properly. You said you are doing `for bigthing in smallthing`, but you aren't. theta inside `for theta in thetai` has shape (2,3). But original theta has shape of (5, 2, 2, 3). This means these thetas aren't same. In fact, `for x in something` denotes that 'x' is a temporary variable inside the loop. Your original theta has global scope to the module. – Kishor May 19 '20 at 07:21
  • 1
    No thank you for your efforts! Yes someone explained that to me here: https://stackoverflow.com/questions/61866304/unusual-list-comprehension-behaviour/61866901#61866901 I simply didn't realise the theta in the for loop was a diffferent theta. I would not choose theta for this iterative loop counter myself! – user2498193 May 19 '20 at 12:24