A = numpy.array([[0,1,2], [3,4,5], [5,4,1]])
我想计算每一行的中位数,然后将所有小于中位数的值归零。我该怎么做?
答案 0 :(得分:4)
A = np.array([[0,1,2], [3,4,5], [5,4,1]])
medians=np.median(A,axis=1)[np.newaxis].T
A[A<medians]=0
A=
[[0 1 2]
[3 4 5]
[5 4 1]]
Medians=
[[ 1.]
[ 4.]
[ 4.]]
A after subtracting medians
[[0 1 2]
[0 4 5]
[5 4 0]]