runtime
439 / 500
question 439 Visualization & EDA · hard

Choose a histogram's bin count from the data

The Freedman–Diaconis rule picks a bin width from the data: width = 2 × IQR ÷ n^(1/3), where n is the number of values and IQR uses pandas' default quantiles. Write fd_bins(values): return the number of bins, the range (max − min) divided by that width, rounded up, and at least 1. If the IQR is 0 (or there are fewer than 2 values), return 1. Ignore missing values (None).

Functionfd_bins(values)
Returnsan int ≥ 1
Examples
callfd_bins([107, 107, 87, 108, 133, 117, 79, 124, 69, 95, 126, 99, 95, 75, 134, 104])returns4
callfd_bins([1, 2, 3, 4, 5, 6, 7, 8])returns2
callfd_bins([5, 5, 5, 5])returns1

Loading your progress…

Choose a histogram's bin count from the data — Python practice · Runtime