Skip to content

Instantly share code, notes, and snippets.

@carlok
Created April 18, 2016 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlok/bc8829cdd7d5e30beb4a6fd1649fc99e to your computer and use it in GitHub Desktop.
Save carlok/bc8829cdd7d5e30beb4a6fd1649fc99e to your computer and use it in GitHub Desktop.
get top percentile values of an array of numbers
#!/usr/bin/python
import math
percentile = 0.95
# http://sqa.fyicenter.com/Online_Test_Tools/Random_Real_Number_Float_Value_Generator.php
pixel_values = [31.674171850011, -14.601817055329, -50.326219329371, -16.396269623954, 60.024497592015, 9.1736615676739, 15.208291195156, -72.45523509377, -85.000746439726, -49.873983136473, 89.013266951911, 90.902370949905, -40.423479737516, -46.220096055987, -53.861242107865, -80.838195878063, 13.397552791211, -13.217278723279, -3.1931508546449, 5.4144153734829, -71.64644837003, -66.123504455591, -99.949724953312, 36.715438631751, -56.529802065148, -89.545732965393, -52.416536020418, 19.02381212714, 27.063275691617, 33.577258338636, 44.723530160998, -7.7681148545126, -48.941860835529, -43.828084610234, -1.9154353945261, 28.575669211676, -41.679723018346, -90.067412411598, 95.817001923065, 63.608590404067, 71.931231441271, 18.765631420463, -96.782253104387, -56.17893632162, -60.945051574294, -93.087966594846, 50.743922395115, 54.283560244883, 74.524974791892, -48.982292395181, -96.418446367222, 69.834825676185, 88.629505903472, -38.981757325219, 38.469125948179, -57.107216082593, 66.008788353332, -3.7091484937656, -2.6331679187447, -5.2334728960063, 54.07879800565, -41.694427376382, 6.8751114153634, 78.036986475213, 32.416369076224, -51.592887452152, 10.419297281246, 71.710657036907, 2.3222894013109, 83.699181881219, 14.402726564907, 48.982064966435, -70.077270679238, -35.66461491697, 32.534164500366, 99.325490882269, -96.059659546849, -78.171065499502, 94.757155377936, 52.325405079993, 81.786762245873, -44.124463380997, 14.697520319364, 15.764924167103, 43.677609482143, -65.628349981121, 29.21167574709, 62.245279127449, -65.314740362967, 55.849531302124, 84.621918508901, -7.6955692756781, 60.373196336344, 95.727194763225, 21.619647784966, 43.500312756373, 9.5335774292757, 38.664283746697, -64.727447537451, -99.460790869233]
pixel_values = sorted(pixel_values, reverse=True)
histogram_percentile = pixel_values[int(math.floor((1 - percentile) * len(pixel_values))) - 1]
print percentile
print len(pixel_values)
print pixel_values
print histogram_percentile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment