0

I'm new to Python, and while reading a script, I encountered the following syntax:

def approximate_random_effects(data, labels, group):

    correlation_per_donor = {}
    for donor_id in set(data[group]):
        correlation_per_donor[donor_id], _, _, _, _ = \
          linregress(
            list(data[labels[0]][data[group] == donor_id]),
            list(data[labels[1]][data[group] == donor_id]))
    average_slope = np.array(correlation_per_donor.values()).mean()
    t, p_val = ttest_1samp(correlation_per_donor.values(), 0)

Why does the LHS of correlation_per_donor[donor_id] have _, and why does the RHS have so many () with consecutive [] nested inside it? I do not understand it and even running through Codeacademy's lists/dict tutorial it did not help.

Advice is needed, thanks!

EDIT: Is my comprehension of the script correct? Attached below:

for each donor ID,

for-loop picks out donor_id in group residing in DATA:

LHS has 5 variables, but only makes donor_id a part of a dictionary "correlation_per_donor"

RHS does linregress on 2 lists, with (labels[first item] in DATA, group in DATA == donor_id) and (labels[second item] in DATA, group in DATA == donor_id)

I don't understand why == donor_id is required

Community
  • 1
  • 1
poopyheadjoe
  • 39
  • 10

1 Answers1

1

_ is just a variable name, in this case used to say "I'm ignoring this data".

The right hand side has []'s because it's a nested data structure of lists and dictionaries.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • What would "I'm not ignoring this data" look like then? Do i fill it up with ` correlation_per_donor[donor_id]`? I know this script is highly specific, but i hope you can narrow it down so i know what to look for, thanks! – poopyheadjoe May 11 '15 at 16:04
  • 1
    You would have a variable name there, which is then used further down. `correlation_per_donor[donor_id], foo, bar, l, q` f ex. – Lennart Regebro May 11 '15 at 16:06
  • Alright thanks @Lennart! Do you mind verbally describing what that RHS side does though? I'll post what I understand from it in my question, will edit in a min. – poopyheadjoe May 11 '15 at 16:13
  • The RHS side does a LOT, what exactly is unclear? – Lennart Regebro May 11 '15 at 16:14
  • 1
    `data[group] == donor_id` will return True if data[group] equals donor_id. Hence `foo[data[group] == donor_id]` will pick different items from `foo` depending on what donor_id is. – Lennart Regebro May 11 '15 at 16:27
  • I'd recommend you to pick apart the right hand side but by bit into separate variables, so you can see what it does. As it is no it's not good programming. – Lennart Regebro May 11 '15 at 16:27