10 examples of 'python median' in Python

Every line of 'python median' 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
12def median(x):
13 return int(np.median(x))
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

26def median(values):
27 length = len(values)
28 values.sort()
29 if length % 2 != 0:
30 # Odd number of values, so chose middle one
31 return values[length/2]
32 else:
33 # Even number of values, so mean of middle two
34 return mean([values[length/2], values[(length/2)-1]])
26def median(r):
27 """Return the median of an iterable of numbers.
28
29 The median is the point at which half the numbers are lower than it and
30 half the numbers are higher. This gives a better sense of the majority
31 level than the mean (average) does, because the mean can be skewed by a few
32 extreme numbers at either end. For instance, say you want to calculate
33 the typical household income in a community and you've sampled four
34 households:
35
36 >>> incomes = [18000] # Fast food crew
37 >>> incomes.append(24000) # Janitor
38 >>> incomes.append(32000) # Journeyman
39 >>> incomes.append(44000) # Experienced journeyman
40 >>> incomes.append(67000) # Manager
41 >>> incomes.append(9999999) # Bill Gates
42 >>> median(incomes)
43 38000.0
44 >>> mean(incomes)
45 1697499.8333333333
46
47 The median here is somewhat close to the majority of incomes, while the
48 mean is far from anybody's income.
49
50 This implementation makes a temporary list of all numbers in memory.
51 """
52 s = list(r)
53 s_len = len(s)
54 if s_len == 0:
55 raise ValueError("can't calculate median of empty collection")
56 s.sort()
57 center = s_len // 2
58 is_odd = s_len % 2
59 if is_odd:
60 return s[center] # Return the center element.
61 # Return the average of the two elements nearest the center.
62 low = s[center-1]
63 high = s[center]
64 return mean([low, high])
106def median(x):
107 return sorted(x)[len(x) // 2]
136def findMedian(self):
137 small, large = self.heaps
138 if len(large) > len(small):
139 return float(large[0])
140 return (large[0] - small[0]) / 2.0
42def median(numbers):
43 """Return the median of the list of numbers.
44
45 found at: http://mail.python.org/pipermail/python-list/2004-December/253517.html"""
46 # Sort the list and take the middle element.
47 n = len(numbers)
48 copy = numbers[:] # So that "numbers" keeps its original order
49 copy.sort()
50 if n & 1: # There is an odd number of elements
51 return copy[n // 2]
52 else:
53 return (copy[n // 2 - 1] + copy[n // 2]) / 2.0
318def median(self):
319 return self.quantiles([0.5])[:,0]
995@property
996def median(self):
997 """Return the median."""
998 return 0
91def median(lst):
92 n = len(lst)
93 if n < 1:
94 return None
95 if n % 2 == 1:
96 return sorted(lst)[n//2]
97 else:
98 return sum(sorted(lst)[n//2-1:n//2+1])/2.0
897def median(arr):
898 """ Calculates the median value for each time series within tss.
899
900 :param arr: KHIVA array with the time series.
901 :return: KHIVA array with the median value of each time series within tss.
902 """
903 b = ctypes.c_void_p(0)
904 error_code = ctypes.c_int(0)
905 error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
906 KhivaLibrary().c_khiva_library.median(ctypes.pointer(arr.arr_reference),
907 ctypes.pointer(b), ctypes.pointer(error_code), error_message)
908 if error_code.value != 0:
909 raise Exception(str(error_message.value.decode()))
910
911 return Array(array_reference=b)

Related snippets