classification
Title: statistics.mean of bools
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: statistics module - incorrect results with boolean input
View: 24068
Assigned To: Nosy List: Peter.Norvig, r.david.murray, steven.daprano
Priority: normal Keywords:

Created on 2016-05-02 20:59 by Peter.Norvig, last changed 2016-05-02 21:41 by r.david.murray. This issue is now closed.

Messages (2)
msg264670 - (view) Author: Peter Norvig (Peter.Norvig) Date: 2016-05-02 20:59
mean([True, True, True, False]) should be 0.75, but it returns 0.25.

The fix is to change _sum so that when the type is bool, the result should be coerced to int, not bool.

Why it is important for statistics.mean to work with bools:
It is natural to say something like
    mean(x > threshold for x in data)
and expect to get the percentage of items in data that are above threshold.
msg264675 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-02 21:41
Already fixed: #24068.
History
Date User Action Args
2016-05-02 21:41:14r.david.murraysetstatus: open -> closed

superseder: statistics module - incorrect results with boolean input

nosy: + r.david.murray
messages: + msg264675
resolution: duplicate
stage: resolved
2016-05-02 21:24:42zach.waresetversions: - Python 3.4
2016-05-02 21:05:49berker.peksagsetnosy: + steven.daprano
2016-05-02 20:59:11Peter.Norvigcreate