Home > Software design >  get sum of numpy array when another array equals 1
get sum of numpy array when another array equals 1

Time:04-13

im trying to create a custom scoring function but I am having trouble with the last line.

def scorer(y_true, y_pred):
    y_pred1 = shift(y_pred,1)
    y_pred2 = shift(y_pred,2)
    target = y_pred - y_true
    action = np.where(target > 0, 1, 0)
    diff = y_pred2 - y_pred1
    score = (action == 1).sum(diff)

My output is supposed to be the sum of the diff rows when the action is equal to 1 for that row.

Diff Action
1.28 1
2.56 0
.64 1
.32 0
5.12 0

For example, in this case it would be 1.28 .64=1.92

CodePudding user response:

You can put the diff directly into the np.where:

action = np.where(target > 0, y_pred2 - y_pred1, 0)     

then sum action, via

sum(action)
  • Related