Untitled

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

# Sample ungrouped data
data = [5, 7, 9, 9, 10, 11, 12, 12, 12, 15]

# Mean
mean = sum(data) / len(data)

# Median
median = statistics.median(data)

# Mode
mode = statistics.mode(data)

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


import numpy as np
from scipy import stats

# Grouped data (data and their frequencies)
data = [10, 20, 30, 40, 50]
frequencies = [5, 8, 12, 10, 5]

# Calculate the midpoint of each class
midpoints = [(data[i] + data[i+1]) / 2 for i in range(len(data)-1)]

# Calculate the sum of products of midpoints and frequencies
sum_products = sum([midpoints[i] * frequencies[i] for i in range(len(midpoints))])

# Calculate the total frequency
total_frequency = sum(frequencies)

# Calculate mean
mean = sum_products / total_frequency

# Calculate median
cumulative_freq = np.cumsum(frequencies)
median_index = (total_frequency + 1) / 2
median_class_index = np.where(cumulative_freq >= median_index)[0][0]
median_class = data[median_class_index]
lower_boundary = data[median_class_index - 1] if median_class_index > 0 else 0
cf = cumulative_freq[median_class_index - 1] if median_class_index > 0 else 0
median = lower_boundary + ((total_frequency / 2 - cf) / frequencies[median_class_index]) * (data[median_class_index] - data[median_class_index - 1])

# Calculate mode
mode_index = frequencies.index(max(frequencies))
mode = data[mode_index]

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