如何将每行中值小于零的值归零?

时间:2015-03-27 08:09:04

标签: python numpy

A = numpy.array([[0,1,2], [3,4,5], [5,4,1]])

我想计算每一行的中位数,然后将所有小于中位数的值归零。我该怎么做?

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]]