Stuck on C3W1Q7

Hi! I’ve been stuck for a while on the specification of observed and predicted benefit in the code - Question 7. I used several techniques with for loops, but i’m afraid i got confused with the structure of the tuples of pairs.
Here’s what i’ve got:

obs_benefit =
for i in range(len(pairs)):

    x = pairs[i][0][1]
    y = pairs[i][1][1]
    obs_benefit.append(obs_benefit_dict[(x,y)])


# compute average predicted benefit for each pair
pred_benefit = []
for i in range(len(pairs)):

    a = pairs[i][0][0]
    b = pairs[i][1][0]
    pred_benefit.append(np.mean(a,b))

I am pretty sure i miss something in the indexing, would be happy for any help :smiling_face_with_tear:
(i get the error “tuple indices must be integers or slices, not float”)

Hi @Vlad_Poltorak ,

Is this the “c_for_benefit_score” exercise?

If so, the exercise offers you a template for you to enter your code. What I see is that you may have modified this template.

I am copying here the template provided for your convenience. You’ll have to start from this structure to develop your solution.

START CODE HERE (REPLACE INSTANCES OF ‘None’, ‘False’, and ‘pass’ with your code)

# compute observed benefit for each pair
obs_benefit = None

# compute average predicted benefit for each pair
pred_benefit = None

concordant_count, permissible_count, risk_tie_count = 0, 0, 0

# iterate over pairs of pairs
for i in range(len(pairs)):
    for j in range(i + 1, len(pairs)):
        
        # if the observed benefit is different, increment permissible count
        if obs_benefit[i] != obs_benefit[j]:

            # increment count of permissible pairs
            None
            
            # if concordant, increment count
            
            if (False): # change to check for concordance
                
                pass

            # if risk tie, increment count
            if (False): #change to check for risk ties
                pass


# compute c-statistic-for-benefit
cstat = None

# END CODE HERE

Hi, i actually can’t see how can i wrap the ‘obs_benefit’ in one line of code, without using a for loop to go through all the pairs - to check their first and second element respectively…
That’s why a attempted to use the loop.

Ah! I get you know. There’s a way to do it by using one-line-for-loop in Python.

Example:

arr_x= [arr_dict[(i[1],j[1])] for (i,j) in pairs]

Try that! it is very cool.

1 Like

Got it! Very cool indeed, thanks :slightly_smiling_face:

I am glad it works now!

Good luck with the rest of the exercise!

Juan

I am struck in the same lines of code where I am not able to define/calculate the pred_benefit from the tuple pairs. Can someone please help

Best regards
Arun Kumar Sukumar

hi @ArunSukumar

It is always considered a good practice to create a new topic whenever you encounter any issue. You can share the link of this post in your created post, and provide some information on why you are stuck at this line and what kind of help you need.

In case you have got any error or output other than expected output then share screenshot of that output but make sure not to post any codes on the public post as it is against community guidelines and you are posting for the first time, so wanted you to let you know about this.

Regards
DP