Reputation: 23
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
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
Upvotes: 1
Views: 70
Reputation: 192
here's an alternate way that vectorizes the function
import numpy as np
def predict(x, y):
if x >= y:
return 1
return 0
thresholds = [0.00, 0.25, 0.50, 0.75, 1.00]
pred_probs = [0.875, 0.325, 0.6, 0.09, 0.4]
pred_lists = np.ndarray(shape=(len(thresholds), len(pred_probs)), dtype=np.int32)
v_predict = np.vectorize(predict)
for n, threshold in enumerate(thresholds):
pred_lists[n, :] = v_predict(pred_probs, threshold)
print(pred_lists)
Output
[[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]]
Upvotes: 1
Reputation: 2374
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]]
Upvotes: 1