I'm trying to count how many element that is exceed some criteria (for exemple: 0.7) and then convert them into percent, the element is in a multiple list that look like this:
[[[[0.00173012]
[0.0009075 ]
[0.00080378]
...
[0.00069336]
[0.00074539]
[0.00186453]]
[[0.00081442]
[0.00022855]
[0.00019197]
...
[0.00018318]
[0.00017222]
[0.00075811]]
[[0.00084458]
[0.00020444]
[0.0001783 ]
...
[0.00020849]
[0.00017066]
[0.00070635]]
...
[[0.00073932]
[0.00022051]
[0.00024553]
...
[0.00028661]
[0.00019603]
[0.0007242 ]]
[[0.00085666]
[0.0002345 ]
[0.00021651]
...
[0.0002319 ]
[0.00017067]
[0.00066847]]
[[0.00188439]
[0.00092146]
[0.00082662]
...
[0.00077084]
[0.00066442]
[0.00178707]]]]
info: there is ... because it is a long list and cant fit all the list in the output cell (it is originally an image)
I've tried using:
len(pex > 0.7)/100
#pex is variable contain the multiple list above
but it's not really working because the ouput from the len is just 1, and if i divide it by 100 the output will be just 0.01
Is there any way for me to easily count all the element and the element that exceed some criteria so i can convert them into percent?? TIA
If you are allowed to use
numpythis can be easily done, consider following exampleoutput
Explanation: convert nested lists into
numpy.arrayuse comparison to get same-shaped array withTrues where value greater than 5 andFalses elsewhere, then usenumpy.sum(not built-insumfunction) to get count, asTrueandFalseare treated as1and0when subjected to arithmetic operations (this also apply outsidenumpy, e.g.sum([True,True,True])gives3)