문제

Given my array

x = numpy.array([[1.0,2.0,3.0],[10.0,20.0,30.0]])

I could get the median of each element easily with

np.median(x, axis=0)

#output: array([  2.,  20.])

But I cannot do the same thing when I increase the length of one array

x = numpy.array([[1.0,2.0,3.0],[10.0,20.0,30.0, 40.0]])

output should be array([ 2., 25.])

Is there a way to still get the median for such an array?

도움이 되었습니까?

해결책

[np.median(i) for i in x]

Since the list's have unequal length you can't broadcast them. That's why the first work, but not the second.

In the fist case, your two list's will get broadcast into a 3x2 array where a median along a axis makes sense. Now the two list's of unequal length can't be broadcast this way.

다른 팁

Actually you don't have an array of numbers, but an array of lists. So the median makes no sense. You should iterate over your lists and compute the medians one at a time

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top