3

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

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 that represents the element-wise median of arrays a,b and c?...such as,

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

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

2
  • What do you mean by "NumPy is not an option"? Isn't numeric python the 'numpy'? Commented Apr 1, 2012 at 19:43
  • I think they're too separate things. import Numeric works for me, but import numpy doesn't. Commented Apr 1, 2012 at 22:13

3 Answers 3

1

Combine your stack of 2-D arrays into one 3-D array, d = Numeric.array([a, b, c]) and then sort on the third dimension. Afterwards, the successive 2-D planes will be rank order so you can extract planes for the low, high, quartiles, percentiles, or median.

Sign up to request clarification or add additional context in comments.

2 Comments

Would you be able to write this out for me? I'd really appreciate it. Thanks.
@steelymatt I know this is late but I wrote something similar out - element-wise median of three 2-d arrays - as an answer to another question.
0

Well, I'm not versed in Numeric, but I'll just start with a naive solution and see if we can make it any better.

To get the 30th percentile of list foo let x=0.3, sort the list, and pick the the element at foo[int(len(foo)*x)]

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

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

I think that bar = Numeric.array(a,b,c) would make Array you want, and then you could get the nth column with 'bar[:,n]' if Numeric has the same slicing techniques as Numpy.

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

I hope that helps you.

Comments

0

Putting Raymond Hettinger's description into python:

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

d = Numeric.array([a, b, c])
d.sort(axis=0)

Since there are n=3 input matrii so the median would be that of the middle one, the one indexed by one,

print d[n//2]
[[5 8 3]
 [5 5 6]
 [5 2 3]]

And if you had 4 input matrii, you would have to get the mean-elements of d[1] and d[2].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.