Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
1
Indexable
Never
from collections import Counter

def find_mean(data):
    total_values = sum(frequency * value for value, frequency in data)
    total_frequency = sum(frequency for _, frequency in data)
    mean = total_values / total_frequency
    return mean

def find_median(data):
    cumulative_frequency = 0
    n = sum(frequency for _, frequency in data)
    median_class = None

    for value, frequency in data:
        cumulative_frequency += frequency
        if cumulative_frequency >= n / 2:
            median_class = value
            break

    h = data[1][0] - data[0][0]
    f = next(frequency for val, frequency in data if val == median_class)
    L = median_class - h/2
    median = L + ((n / 2 - (cumulative_frequency - f)) / f) * h

    return median

def find_mode(data):
    value, frequency = max(data, key=lambda x: x[1])
    return value

# Example grouped data
grouped_data = [(10, 4), (20, 7), (30, 10), (40, 5), (50, 2)]

mean = find_mean(grouped_data)
median = find_median(grouped_data)
mode = find_mode(grouped_data)

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