Element-median and percentiles of arrays with numeric Python

I am using Numeric Python. Unfortunately, NumPy is not an option. If I have multiple arrays like:

a=Numeric.array(([1,2,3],[4,5,6],[7,8,9]))
b=Numeric.array(([9,8,7],[6,5,4],[3,2,1]))
c=Numeric.array(([5,9,1],[5,4,7],[5,2,3]))

      

How do I return an array representing the average median of arrays a, b and c? ... for example,

array(([5,8,3],[5,5,6],[5,2,3]))

      

And then looking at a more general situation: Given the number of arrays, how do you find the percentiles of each element? For example, return an array that represents the 30th percentile of 10 arrays. Thank you so much for your help!

+3


source to share


2 answers


Concatenate a stack of two-dimensional arrays into one three-dimensional array d = Numeric.array([a, b, c])

, and then sort by the third dimension. The consecutive 2D planes will then have a ranking order so you can extract planes for low, high, quartile, percentile, or median.



+1


source


Well, I don't know Numeric, but I'll start with a naive solution and see if we can do it better.

To get the 30th percentile of a foo

let list x=0.3

, sort the list and select an elementfoo[int(len(foo)*x)]

For your data, you want to put it in a matrix, transpose, sort each row, and get the median for each row.

A matrix in Numeric (like numpy) is an array with two dimensions.



I think what bar = Numeric.array(a,b,c)

will Array do you want and then you can get the nth column with "bar [:, n]" if Numeric has the same slicing methods as Numpy.

foo = sorted(bar[:,n])
foo[int(len(foo)*x)]

      

I hope this helps you.

0


source







All Articles