Average positive numbers in a row

前端 未结 2 1477
我在风中等你
我在风中等你 2021-01-23 18:20

I have a numpy matrix, and each row has a combination of positive and negative numbers.

I want to create a new vector, that gives me the average of all the positive numb

2条回答
  •  既然无缘
    2021-01-23 18:30

    A possible solution would be

    • select all non-negative
    • compute explicitly the mean as sum/count

    In code:

    notneg = x >= 0
    result = (x * notneg).sum(1) / notneg.sum(1)
    

提交回复
热议问题