Я хочу вернуть среднее значение, медиану и (среднее_50) среднее значение 50% значения в середине диапазона списка чисел.моя функция работает на среднем и среднем, но значит_50 не правильно.
для мер:
[10.1, 10.2, 9.9, 10.1, 11.3, 14.2, 12.3, 12.3, 12.3, 12.1, 12.3, 11.9, 10.2, 10.1, 9.8, 8.9, 9.7, 9.8, 9.8, 10.2, 10.3, 10.5, 12.3, 10.1, 8.9, 9.8, 10.5, 10.1, 10.6, 10.7, 10.0, 9.9, 13.0, 13.1, 13.0, 13.2, 14.0]
- возвращена моя функция:
(10.3, 11.013513513513514, 10.845833333333333)
- Ожидаемое значение:
(10.3, 11.013513513513514, 10.664705882352942)
IЯ пытался изменить метод расчета квартилей на основе https://en.wikipedia.org/wiki/Quartile
Я пытался включить или исключить медиану из расчета
Я также пытался включить и исключить первое и последнее значения квартиля
MEASURES_SAMPLE = [
10.1, 10.2, 9.9, 10.1, 11.3, 14.2, 12.3, 12.3, 12.3, 12.1, 12.3, 11.9, 10.2,
10.1, 9.8, 8.9, 9.7, 9.8, 9.8, 10.2, 10.3, 10.5, 12.3, 10.1, 8.9, 9.8, 10.5,
10.1, 10.6, 10.7, 10.0, 9.9, 13.0, 13.1, 13.0, 13.2, 14.0
]
def calculate_med(liste):
# calcul mediane
data = sorted(liste)
lgr = len(data)
# Use the median to divide the ordered data set into two halves
if lgr % 2 == 0:
# cas liste de longueur paire, la mediane est la moyenne des 2 valeurs centrales
# peut etre le probleme
vlr_min = round(lgr/2-1)
vlr_max = round(lgr/2+1)
vlr_med = (data[vlr_min - 1] + data[vlr_max]) / 2
else:
vlr_med = data[round((lgr / 2)-1)]
return vlr_med
def median_and_means(measures):
"""
To get an accurate summary of a set of measure, this function computes the
following value and returns them in a tuple:
- median
- mean (average)
- mean of the values between the first quartile and third quartile
E.g., median_and_means([1,2,3,4,5,6,7,8,9]) is (5, 5, 5)
Note: if measures is an empty list, your function should return the object None.
:param measures: list of measures
:return: tuple (med, mean, mean_50) where med is the median of the values
in measures, mean is the mean and mean_50 the mean of the 50% of value in
the middle of the range.
:rtype: tuple
"""
length = len(measures)
somme = 0
liste = []
# nb_val = 0
moyenne_50 = 0
a_retourner = ()
# moyenne
if measures == []:
return None
else:
for cpt in range(0, length):
somme += measures[cpt]
cpt += 1
mean = somme / length
# mediane
# sort the existing list we dont want to modify initial list
valeur_med = calculate_med(measures)
# centile
liste = sorted(measures)
liste_first_q = []
liste_third_q = []
vlr_min = 0
vlr_max = 0
# identify the first quatile and the last quartile
if length % 2 == 0:
# If there is an even number of data points in the original ordered data set,
# split this data set exactly in half.
liste_first_q = [liste[i] for i in range(int(length/2))]
liste_third_q = [liste[i] for i in range(int(length/2), length)]
else:
# If there is an odd number of data points in the original ordered data set,
# do not include the median (the central value in the ordered list) in either half.
liste_first_q = [liste[i] for i in range(round(length/2)+1)]
liste_third_q = [liste[i] for i in range(round(length/2), length)]
# identify fist quarter value and third quarte which is the median of each lists
vlr_min = calculate_med(liste_first_q)
vlr_max = calculate_med(liste_third_q)
# calculate the mean od the values between both median
moyenne_50 = 0
nb_val = 0
for cpt in range(length):
if vlr_min <= liste[cpt] <= vlr_max:
moyenne_50 += liste[cpt]
nb_val += 1
mean_50 = moyenne_50 / nb_val
a_retourner = (valeur_med, mean, mean_50)
return a_retourner
- возвращена моя функция:
(10.3, 11.013513513513514, 10.845833333333333)
- Ожидаемое значение:
(10.3, 11.013513513513514, 10.664705882352942)