简体   繁体   English

排除 Numpy 中的零 数组行的分位数计算

[英]exclude zeros in Numpy quantile calculation of rows of an array

I have a 2D-array with zero values in each row.我有一个二维数组,每行都有零值。

[[5, 3, 2, 0, 0, 1, 6, 9, 11, 1, 4, 1],
 [0, 0, 12, 0, 1, 0, 0, 2, 0, 30, 2, 2],
 [120, 2, 10, 3, 0, 0, 2, 7, 9, 5, 0, 0]]

Is there a way to calculate the 0.75 quantile of each row by excluding the zero values in the calculation?有没有办法通过排除计算中的零值来计算每行的 0.75 分位数?

For example, in the second row, only 6 non-zero values [12,1,2,30,2,2] should be used in the calculation.例如,在第二行中,计算中只应使用 6 个非零值[12,1,2,30,2,2] I tried using np.quantile() but it will includes all zero values in the calculation.我尝试使用np.quantile()但它将在计算中包含所有零值。

You can replace the zero values with nan and pass the array into np.nanquantile() to compute the quantile of non- nan values您可以用nan替换零值并将数组传递给np.nanquantile()以计算非nan值的分位数

>>> arr = np.array([[5, 3, 2, 0, 0, 1, 6, 9, 11, 1, 4, 1],
                    [0, 0, 12, 0, 1, 0, 0, 2, 0, 30, 2, 2],
                    [120, 2, 10, 3, 0, 0, 2, 7, 9, 5, 0, 0]], dtype='f')
 
>>> arr[arr==0] = np.nan
>>> arr
[[  5.   3.   2.  nan  nan   1.   6.   9.  11.   1.   4.   1.]
 [ nan  nan  12.  nan   1.  nan  nan   2.  nan  30.   2.   2.]
 [120.   2.  10.   3.  nan  nan   2.   7.   9.   5.  nan  nan]]

>>> arr_quantile75 = np.nanquantile(arr, 0.75, axis=1)  #by row-axis
>>> arr_quantile75
[5.75 9.5  9.25]

np.nanquantile() compute the qth quantile of the data along the specified axis, while ignoring nan values [source] np.nanquantile()沿指定轴计算数据的第 q 个分位数,同时忽略 nan 值[来源]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM