Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.9 kB
2
Indexable
Never
def calculate_mean(grouped_data):
    total_frequency = sum(item[1] for item in grouped_data)
    mean = sum(item[0] * item[1] for item in grouped_data) / total_frequency
    return mean

def calculate_median(grouped_data):
    total_frequency = sum(item[1] for item in grouped_data)
    cumulative_frequency = [0]
    cumulative_frequency.extend(sum(item[1] for item in grouped_data[:i+1]) for i in range(len(grouped_data)))

    for i in range(len(cumulative_frequency)-1):
        if cumulative_frequency[i] < total_frequency/2 and cumulative_frequency[i+1] >= total_frequency/2:
            lower_limit = grouped_data[i][0]
            upper_limit = grouped_data[i+1][0]
            median = lower_limit + (upper_limit - lower_limit) * (total_frequency/2 - cumulative_frequency[i]) / (cumulative_frequency[i+1] - cumulative_frequency[i])
            return median

def calculate_mode(grouped_data):
    mode = max(grouped_data, key=lambda x: x[1])[0]
    return mode

grouped_data = [(10, 12), (20, 15), (30, 20), (40, 18), (50, 10)]

mean = calculate_mean(grouped_data)
median = calculate_median(grouped_data)
mode = calculate_mode(grouped_data)

print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)





def calculate_mean(data):
   
    return sum(data) / len(data)

def calculate_median(data):
    
    data.sort()
    n = len(data)
    if n % 2 == 0:
        
        median = (data[n//2 - 1] + data[n//2]) / 2
    else:
        
        median = data[n//2]
    return median

def calculate_mode(data):
   
    from collections import Counter
    count = Counter(data)
    mode = max(count, key=count.get)
    return mode


data = [3, 5, 6, 6, 8, 9]
mean = calculate_mean(data)
median = calculate_median(data)
mode = calculate_mode(data)
print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
Leave a Comment