如何找到此数组的平均值,但仅针对 python 中不等于零的值
how do I find the average of this array but only for the values that are not equal to zero in python
数组([1500, 1520, 1540, 1590, 1590, 1600, 1600, 1560, 1560, 1560, 1580,
1520, 1460, 1510, 1520, 1320, 1320, 1300, 1300, 1320, 1320, 1320,
1320, 1300, 1300, 1340, 1300, 1300, 1300, 1400, 1480, 1360, 1420,
1480, 1580, 1530, 1500, 1480, 1480, 1480, 1460, 1540, 1490, 1480,
1480, 1520, 1500, 1460, 1480, 1480, 1500, 1500, 1600, 1540, 1480,
1460, 1560, 1600, 1560, 1600, 1600, 1600, 1620, 1600, 1580, 1600,
1700, 1620, 1620, 1620, 1700, 1700, 1680, 1640, 1620, 1670, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1670,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1600, 1680, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0], dtype=int64)
您可以使用列表理解和 numpy 来做到这一点:
import numpy as np
a = np.array([1500, 1520, 1540, 1590, 1590, 1600, 1600, 1560, 1560, 1560, 1580,
1520, 1460, 1510, 1520, 1320, 1320, 1300, 1300, 1320, 1320, 1320, 1320,
1300, 1300, 1340, 1300, 1300, 1300, 1400, 1480, 1360, 1420, 1480,
1580, 1530, 1500, 1480, 1480, 1480, 1460, 1540, 1490, 1480, 1480,
1520, 1500, 1460, 1480, 1480, 1500, 1500, 1600, 1540, 1480, 1460,
1560, 1600, 1560, 1600, 1600, 1600, 1620, 1600, 1580, 1600, 1700,
1620, 1620, 1620, 1700, 1700, 1680, 1640, 1620, 1670, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1670, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1600, 1680, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0], dtype=np.int64)
np.mean([i for i in a if i])
输出:
1508.4810126582279
可以过滤数组
arr = np.array([41, 42, 43, 44, 0, 0,0])
arr.mean() # 24.2857
arr[arr != 0].mean() # 42.5
编辑:(!=感谢@tdelaney,包括负数)
这里有一个简单的方法,就是只复制一个没有零的数组,然后计算平均值,这样:
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
数组([1500, 1520, 1540, 1590, 1590, 1600, 1600, 1560, 1560, 1560, 1580, 1520, 1460, 1510, 1520, 1320, 1320, 1300, 1300, 1320, 1320, 1320, 1320, 1300, 1300, 1340, 1300, 1300, 1300, 1400, 1480, 1360, 1420, 1480, 1580, 1530, 1500, 1480, 1480, 1480, 1460, 1540, 1490, 1480, 1480, 1520, 1500, 1460, 1480, 1480, 1500, 1500, 1600, 1540, 1480, 1460, 1560, 1600, 1560, 1600, 1600, 1600, 1620, 1600, 1580, 1600, 1700, 1620, 1620, 1620, 1700, 1700, 1680, 1640, 1620, 1670, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1670, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1600, 1680, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int64)
您可以使用列表理解和 numpy 来做到这一点:
import numpy as np
a = np.array([1500, 1520, 1540, 1590, 1590, 1600, 1600, 1560, 1560, 1560, 1580,
1520, 1460, 1510, 1520, 1320, 1320, 1300, 1300, 1320, 1320, 1320, 1320,
1300, 1300, 1340, 1300, 1300, 1300, 1400, 1480, 1360, 1420, 1480,
1580, 1530, 1500, 1480, 1480, 1480, 1460, 1540, 1490, 1480, 1480,
1520, 1500, 1460, 1480, 1480, 1500, 1500, 1600, 1540, 1480, 1460,
1560, 1600, 1560, 1600, 1600, 1600, 1620, 1600, 1580, 1600, 1700,
1620, 1620, 1620, 1700, 1700, 1680, 1640, 1620, 1670, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1670, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1600, 1680, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0], dtype=np.int64)
np.mean([i for i in a if i])
输出:
1508.4810126582279
可以过滤数组
arr = np.array([41, 42, 43, 44, 0, 0,0])
arr.mean() # 24.2857
arr[arr != 0].mean() # 42.5
编辑:(!=感谢@tdelaney,包括负数)
这里有一个简单的方法,就是只复制一个没有零的数组,然后计算平均值,这样:
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