Python 用中值替换 numpy 数组中的零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17056325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
replace zeroes in numpy array with the median value
提问by slashdottir
I have a numpy array like this:
我有一个像这样的 numpy 数组:
foo_array = [38,26,14,55,31,0,15,8,0,0,0,18,40,27,3,19,0,49,29,21,5,38,29,17,16]
I want to replace all the zeros with the median value of the whole array (where the zero values are not to be included in the calculation of the median)
我想用整个数组的中值替换所有零(其中零值不包括在中值计算中)
So far I have this going on:
到目前为止,我有这样的事情:
foo_array = [38,26,14,55,31,0,15,8,0,0,0,18,40,27,3,19,0,49,29,21,5,38,29,17,16]
foo = np.array(foo_array)
foo = np.sort(foo)
print "foo sorted:",foo
#foo sorted: [ 0 0 0 0 0 3 5 8 14 15 16 17 18 19 21 26 27 29 29 31 38 38 40 49 55]
nonzero_values = foo[0::] > 0
nz_values = foo[nonzero_values]
print "nonzero_values?:",nz_values
#nonzero_values?: [ 3 5 8 14 15 16 17 18 19 21 26 27 29 29 31 38 38 40 49 55]
size = np.size(nz_values)
middle = size / 2
print "median is:",nz_values[middle]
#median is: 26
Is there a clever way to achieve this with numpy syntax?
有没有一种聪明的方法可以用 numpy 语法来实现这一点?
Thank you
谢谢
采纳答案by bbayles
This solution takes advantage of numpy.median:
该解决方案利用了numpy.median:
import numpy as np
foo_array = [38,26,14,55,31,0,15,8,0,0,0,18,40,27,3,19,0,49,29,21,5,38,29,17,16]
foo = np.array(foo_array)
# Compute the median of the non-zero elements
m = np.median(foo[foo > 0])
# Assign the median to the zero elements
foo[foo == 0] = m
Just a note of caution, the median for your array (with no zeroes) is 23.5 but as written this sticks in 23.
请注意,您的数组的中位数(没有零)是 23.5,但正如所写的那样,它坚持在 23 中。
回答by Alex Szatmary
foo2 = foo[:]
foo2[foo2 == 0] = nz_values[middle]
Instead of foo2, you could just update fooif you want. Numpy's smart array syntax can combine a few lines of the code you made. For example, instead of,
而不是foo2,您可以foo根据需要进行更新。Numpy 的智能数组语法可以组合您编写的几行代码。例如,而不是,
nonzero_values = foo[0::] > 0
nz_values = foo[nonzero_values]
You can just do
你可以做
nz_values = foo[foo > 0]
You can find out more about "fancy indexing" in the documentation.
您可以在文档中找到有关“花式索引”的更多信息。

