UNQ_C7 mapping pair outcomes to benefit / use lists in python

I am stuck at the programming assignment of week 1. I guess it is basic python knowledge, but I cannot get behind the idea:

# mapping pair outcomes to benefit
obs_benefit_dict = {     
   (0, 0): 0,
   (0, 1): -1,
   (1, 0): 1,
   (1, 1): 0,
tmp_pairs = [((0.64, 1), (0.54, 0)), 
             ((0.44, 0),(0.40, 1))]

# compute observed benefit for each pair
obs_benefit = ???
# compute average predicted benefit for each pair
pred_benefit = ???

How do I map the dictionary to the double tuple in the list? Do I have to use the map function? How?

How do I take the average of the first value in each patient’s tuple? I do not understand how I can access the value in the list.

Any help is appreciated! :slight_smile:

Hi @henninghhh
The solution is simple. First use a for loop to take each tuple values. In the for loop you can use tuple unpacking to unpack the outer tuple. For tuple unpacking use (i, j) in the for loop. Then to take the first value of the two inner tuples use indexing in the two unpacked variables (i, j) to access it.

You can do this in a single line with list comprehension. To take the average use np. mean.

I think you have also asked about how to access the values in dictionary. The same concept I have explained above will work.

Access the 1st index of two inner tuples and convert it to tuple and you can access the dictionary like dict[(x, y)]. You can even do it in one line with list comprehension.

Thank you very much, @bharathikannan ! List comprehension was a good hint. It now works :+1:

1 Like

I’m trying to create a new topic for AI4M Course 3 (I’m enrolled) but I can’t, can you help me with this please? The option appears grayed out. Thanks

@J_Zam :slight_smile: , You need to select a particular week to create the topic. In category ‘all’ , It is grayed out.

1 Like