这里有一个简单的方法,就是只复制一个不带零的数组,然后计算平均值,方法如下:
import numpy as np
new_array = array[array!=0] #Create a copy of the array excluding the zeros
avg = np.average(new_array)
这里还有一个不使用任何numpy方法的想法:
count=0
total=0
for number in array:
if number != 0:
total+=number #adds the non-zero values together
count+=1 #counts the non-zero values
avg= total/count