hans-t
hans-t

Reputation: 3223

How to zero out values that are less than median in each row?

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

I want to compute median of each row and then zero out all values that are less than the median. How do I do that?

Upvotes: 1

Views: 79

Answers (1)

Dennis Sakva
Dennis Sakva

Reputation: 1467

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

Upvotes: 4

Related Questions