我有一个简单的numpy数组。
array([[10, 0, 10, 0],
[ 1, 1, 0, 0]
[ 9, 9, 9, 0]
[ 0, 10, 1, 0]])
我想分别取这个数组的每列的中位数。
但是,在计算中位数时,我想忽略各个0
个值。
为了进一步复杂化,我想将仅包含0
条目的列保留为0
的中位数。以这种方式,这些列将作为占位符的一部分,保持矩阵的尺寸相同。
numpy文档中没有任何可以满足我想要的参数(也许我被R获得的许多开关所破坏了!)
numpy.median(a, axis=None, out=None, overwrite_input=False)[source]
有人可以说明一个有效的方法来做到这一点,这符合numpy的精神?我可以把它搞砸,但在那种情况下,我觉得我首先打败了使用numpy的目的。
提前致谢。
答案 0 :(得分:11)
Masked array
总是很方便,但是懒散地说:
In [14]:
%timeit np.ma.median(y, axis=0).filled(0)
1000 loops, best of 3: 1.73 ms per loop
In [15]:
%%timeit
ans=np.apply_along_axis(lambda v: np.median(v[v!=0]), 0, x)
ans[np.isnan(ans)]=0.
1000 loops, best of 3: 402 µs per loop
In [16]:
ans=np.apply_along_axis(lambda v: np.median(v[v!=0]), 0, x)
ans[np.isnan(ans)]=0.; ans
Out[16]:
array([ 9., 9., 9., 0.])
np.nonzero
甚至更快:
In [25]:
%%timeit
ans=np.apply_along_axis(lambda v: np.median(v[np.nonzero(v)]), 0, x)
ans[np.isnan(ans)]=0.
1000 loops, best of 3: 384 µs per loop
答案 1 :(得分:6)
使用蒙面数组和np.ma.median(axis=0).filled(0)
来获取列的中位数。
In [1]: x = np.array([[10, 0, 10, 0], [1, 1, 0, 0], [9, 9, 9, 0], [0, 10, 1, 0]])
In [2]: y = np.ma.masked_where(x == 0, x)
In [3]: x
Out[3]:
array([[10, 0, 10, 0],
[ 1, 1, 0, 0],
[ 9, 9, 9, 0],
[ 0, 10, 1, 0]])
In [4]: y
Out[4]:
masked_array(data =
[[10 -- 10 --]
[1 1 -- --]
[9 9 9 --]
[-- 10 1 --]],
mask =
[[False True False True]
[False False True True]
[False False False True]
[ True False False True]],
fill_value = 999999)
In [6]: np.median(x, axis=0)
Out[6]: array([ 5., 5., 5., 0.])
In [7]: np.ma.median(y, axis=0).filled(0)
Out[7]:
array(data = [ 9. 9. 9., 0.])
答案 2 :(得分:1)
您可以使用masked arrays。
a = np.array([[10, 0, 10, 0], [1, 1, 0, 0],[9,9,9,0],[0,10,1,0]])
m = np.ma.masked_equal(a, 0)
In [44]: np.median(a)
Out[44]: 1.0
In [45]: np.ma.median(m)
Out[45]: 9.0
In [46]: m
Out[46]:
masked_array(data =
[[10 -- 10 --]
[1 1 -- --]
[9 9 9 --]
[-- 10 1 --]],
mask =
[[False True False True]
[False False True True]
[False False False True]
[ True False False True]],
fill_value = 0)
答案 3 :(得分:0)
这可能会有所帮助。获得非零数组后,您可以直接从[非零(a)]
获得中值numpy.nonzero的(a)[源]
Return the indices of the elements that are non-zero.
Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with:
a[nonzero(a)]
To group the indices by element, rather than dimension, use:
transpose(nonzero(a))
The result of this is always a 2-D array, with a row for each non-zero element.
Parameters :
a : array_like
Input array.
Returns :
tuple_of_arrays : tuple
Indices of elements that are non-zero.
See also
flatnonzero
Return indices that are non-zero in the flattened version of the input array.
ndarray.nonzero
Equivalent ndarray method.
count_nonzero
Counts the number of non-zero elements in the input array.
Examples
>>> x = np.eye(3)
>>> x
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> np.nonzero(x)
(array([0, 1, 2]), array([0, 1, 2]))
>>> x[np.nonzero(x)]
array([ 1., 1., 1.])
>>> np.transpose(np.nonzero(x))
array([[0, 0],
[1, 1],
[2, 2]])
A common use for nonzero is to find the indices of an array, where a condition is True. Given an array a, the condition a > 3 is a boolean array and since False is interpreted as 0, np.nonzero(a > 3) yields the indices of the a where the condition is true.
>>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a > 3
array([[False, False, False],
[ True, True, True],
[ True, True, True]], dtype=bool)
>>> np.nonzero(a > 3)
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
The nonzero method of the boolean array can also be called.
>>> (a > 3).nonzero()
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))