8 examples of 'python percentile' in Python

Every line of 'python percentile' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
84def percentile(n):
85 def percentile_(x):
86 return np.percentile(x, n)
87
88 percentile_.__name__ = 'percentile_%s' % n
89 return percentile_
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

44def get_percentile(self, percentile):
45 sum = 0
46 index = 0
47 while sum < percentile * self.total:
48 sum += self.buckets[index]
49 index += 1
50
51 if index == 0:
52 return 0
53 elif index - 1 >= len(self.BUCKET_OFFSETS):
54 return sys.maxint
55 else:
56 return self.BUCKET_OFFSETS[index - 1] - 1
53def percentile(self, histogram, bins, percent):
54 """ Calculate the <<percent>> percentile of the histogram """
55 # handle > 100 or < 0
56 if float(percent) <= 0:
57 return 0
58 elif float(percent) >= 100:
59 return bins[-1]
60
61 # if histogram is all 0s, return 0
62 if len(np.nonzero(histogram)[0]) == 0:
63 return 0
64
65 # normalize the histogram
66 hist_norm = np.zeros(histogram.shape)
67
68 hist_sum = 0.0 # dividing by a float ensures we get a float for normalized histograms
69 for binval in histogram:
70 hist_sum += binval
71
72 for i, binval in enumerate(histogram):
73 hist_norm[i] = histogram[i] / hist_sum
74
75 # compute the percentile using the normalized histogram
76 bin_sum = 0.0 # the normalized histogram is floating point
77 i = 0
78 pfloat = float(percent) * 0.01
79 while bin_sum < pfloat and i < hist_norm.shape[0]:
80 bin_sum += hist_norm[i]
81 i += 1
82
83 if i+1 >= len(bins):
84 return bins[-1]
85
86 return bins[i+1]
617def _func(self, values, axis=1, **kwargs):
618 return pd.np.percentile(values, kwargs["value"], axis=axis)
366def percentile(values, p):
367 if not isinstance(p, float) or not(0.0 <= p <= 1.0):
368 raise ValueError("p must be a float in the range [0.0; 1.0]")
369
370 values = sorted(values)
371 if not values:
372 raise ValueError("no value")
373
374 k = (len(values) - 1) * p
375 f = math.floor(k)
376 c = math.ceil(k)
377 if f != c:
378 d0 = values[f] * (c - k)
379 d1 = values[c] * (k - f)
380 return d0 + d1
381 else:
382 return values[int(k)]
290def percentile25(arr):
291 return np.percentile(arr, (25), interpolation='midpoint')
52def percentile(results, percent):
53 return _percentile(sorted(results), percent)
16def get_percentile(stat):
17 if not stat.startswith('percentile_'):
18 raise ValueError("must start with 'percentile_'")
19 qstr = stat.replace("percentile_", '')
20 q = float(qstr)
21 if q > 100.0:
22 raise ValueError('percentiles must be <= 100')
23 if q < 0.0:
24 raise ValueError('percentiles must be >= 0')
25 return q

Related snippets