0

I want to find quantiles of element n in sublists.

Let's say I have (in reality it's much bigger):

List=[[[1,3,0,1],[1,2,0,1],[1,3,0,1]],[[2,2,1,0],[2,2,1,0],[2,2,1,0]]]

I want a way to find quantiles (like numpy.percentile) for the 2:nd elements in the sublist [[1,3,1,1],[1,2,0,1],[9,3,2,1]] and in [[1,2,3,4],[0,2,0,0],[1,2,2,2]] and then I want to do a maximum function so I know which subgroup of those two had the highest chosen quantile, and I also want to know the values the other 3 constant values (1:st, 3:rd and 4:th elements) has at that maximum.

  • Hi! Welcome to StackOverflow! Take a minute to see the [how to ask](http://stackoverflow.com/help/how-to-ask) section! SO users expect questioners to come up with some code to look at... – jkalden Nov 16 '15 at 18:13

1 Answers1

0

Here's one possible way. Assuming (as in your question)

List=[[[1,3,0,1],[1,2,0,1],[1,3,0,1]],[[2,2,1,0],[2,2,1,0],[2,2,1,0]]]

Then one can convert each first-level tuple to a numpy matrix first, which allows easily selecting the 2nd column, to which one can apply the numpy.percentile function. Shortly,

import numpy as np
quartiles = [np.percentile(np.matrix(l)[:,1], 25) for l in List] 

which gives as output the quartiles (25-percentiles) of each first-level tuple:

[2.5, 2.0]

One can then find the maximum with numpy.argmax:

am = np.argmax(quartiles)

and then use it to select the other 3 constant elements

other3 = [List[am][0][0], List[am][0][2], List[am][0][3]]
JARS
  • 1,109
  • 7
  • 10