Obtain median values for arrays of different sizes

Asked 2 years ago, Updated 2 years ago, 39 views

I understand that median is easy to get in an array of the same size.

For example, A = [1,2,3,4,5,6,7,8,9] and if you want to break 3 each to get the median.

median = np.median(A.reshape(3,3), axis=1)
print(median)
>> [2. 5. 8.]

You could do this.

But if you want to cut the number differently, for example, cut A into 2, 3, and 4.

median = func(A)
print(median)
>> [1.5, 4, 7.5]

can do this? I want to not spend any iteration.

python array

2022-09-21 11:09

1 Answers

In [63]: A = np.arange(1, 10)

In [64]: A
Out[64]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

In [65]: list(map(np.median, np.split(A, [2,5])))
Out[65]: [1.5, 4.0, 7.5]


2022-09-21 11:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.