0

This question is a new one (I've already looked into similar questions and did not find what I need). Therefore:

What is the most efficient way to apply a weighted median to every subarray of a 2d numpy matrix efficiently? (No extra frameworks, but pure numpy if possible)

Data = np.asarray([[ 1.1,  7.8,  3.3, 4.9],
[ 6.1,  9.8,  5.3, 7.9],
[ 4.1,  4.8,  3.3, 7.1],
... 
[ 1.1,  7.4,  3.1, 4.9],
[ 7.1,  3.8,  7.3, 8.1],
[ 19.1,  2.8,  3.2, 1.1]])

weights = [0.64, 0.79, 0.91, 0]

Note: the answers to the other questions only show an 1d problem. This problem hast to deal with 1.000.000 subarrays efficiently

hgb123
  • 13,869
  • 3
  • 20
  • 38
  • Looks like I misunderstood what "weighted median" is. [This answer](https://stackoverflow.com/questions/20601872/numpy-or-scipy-to-calculate-weighted-median) came up when I looked up for the meaning of weighted median. – Joonyoung Park Oct 16 '20 at 00:41

1 Answers1

0

Using Data provided by @JoonyoungPark, you can use a list comprehension:

[np.median(i*weights) for i in Data]

[1.8535000000000001,
 4.3635,
 2.8135,
 1.7625000000000002,
 3.7729999999999997,
 2.5620000000000003]
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Edit: are you sure this is the 'weighted median'? actually, by weighted, the weights in terms of percentile is meant. (correct me if I'm wrong or if the result would be the same) – HR_quantile Oct 15 '20 at 20:44