Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python looping list

I have function 1

def predict(x, y):
    z = []
    for i in x:
        if i >= y:
            z.append(1)
        else:
            z.append(0)
    return z

Now, for each thresholds value, I want to use that and pred_probs to call my first function and then populate the empty lists.
so the first call would be predict(pred_probs, .0.00). the second call in the loop would be predict(pred_probs, 0.25) and so on. each loop would append the output the lists

   pred_1 = []
    pred_2 = []
    pred_3 = []
    pred_4 = []
    pred_5 = []
    thresholds = [0.00, 0.25, 0.50, 0.75, 1.00]
    pred_probs = [0.875, 0.325, 0.6, 0.09, 0.4]

    for i in thresholds:
        pred =  predict(pred_probs,i)
        pred1.append(pred)

The desired outcome would be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pred_1 = [1,1,1,1,1]
pred_2 = [1,1,1,0,1]
pred_3 = [1,0,1,0,0]
pred_4 = [1,0,0,0,0]
pred_5 = [0,0,0,0,0]

the problem is that I’m not sure how to access the lists individually.

this is the output i receive:

[1, 1, 1, 1, 1]
[1, 1, 1, 0, 1]
[1, 0, 1, 0, 0]
[1, 0, 0, 0, 0]
[0, 0, 0, 0, 0] 

however, i’m not sure how to take the first list and assign it to pred_1 and then son

>Solution :

Here is how you could do exactly what you asked.

for i, arr in zip(thresholds, [pred_1, pred_2, pred_3, pred_4, pred_5]):
    pred = predict(pred_probs, i)
    arr.extend(pred)

However, you may consider whether 5 lists is really what you want. It might be easier to do

pred = []
for i in thresholds:
    vals = predict(pred_probs, i)
    pred.append(vals)
print(pred)

This will give

[[1, 1, 1, 1, 1],
 [1, 1, 1, 0, 1],
 [1, 0, 1, 0, 0],
 [1, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading