summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-09-05 07:18:47 (GMT)
committerGitHub <noreply@github.com>2019-09-05 07:18:47 (GMT)
commite4810b2a6c1d0db1a27ad046831b8fa3b57967a4 (patch)
tree18c2c6412afb287e8e60f44bfd11ca46ff388638 /Lib
parent8f9cc8771ffb8d0e21be287eaed42ae06087acca (diff)
downloadcpython-e4810b2a6c1d0db1a27ad046831b8fa3b57967a4.zip
cpython-e4810b2a6c1d0db1a27ad046831b8fa3b57967a4.tar.gz
cpython-e4810b2a6c1d0db1a27ad046831b8fa3b57967a4.tar.bz2
bpo-36324: Apply review comments from Allen Downey (GH-15693)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/statistics.py38
1 files changed, 17 insertions, 21 deletions
diff --git a/Lib/statistics.py b/Lib/statistics.py
index c7d6568..4b17266 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -322,7 +322,6 @@ def fmean(data):
"""Convert data to floats and compute the arithmetic mean.
This runs faster than the mean() function and it always returns a float.
- The result is highly accurate but not as perfect as mean().
If the input dataset is empty, it raises a StatisticsError.
>>> fmean([3.5, 4.0, 5.25])
@@ -538,15 +537,16 @@ def mode(data):
``mode`` assumes discrete data, and returns a single value. This is the
standard treatment of the mode as commonly taught in schools:
- >>> mode([1, 1, 2, 3, 3, 3, 3, 4])
- 3
+ >>> mode([1, 1, 2, 3, 3, 3, 3, 4])
+ 3
This also works with nominal (non-numeric) data:
- >>> mode(["red", "blue", "blue", "red", "green", "red", "red"])
- 'red'
+ >>> mode(["red", "blue", "blue", "red", "green", "red", "red"])
+ 'red'
- If there are multiple modes, return the first one encountered.
+ If there are multiple modes with same frequency, return the first one
+ encountered:
>>> mode(['red', 'red', 'green', 'blue', 'blue'])
'red'
@@ -615,28 +615,28 @@ def multimode(data):
# position is that fewer options make for easier choices and that
# external packages can be used for anything more advanced.
-def quantiles(dist, /, *, n=4, method='exclusive'):
- """Divide *dist* into *n* continuous intervals with equal probability.
+def quantiles(data, /, *, n=4, method='exclusive'):
+ """Divide *data* into *n* continuous intervals with equal probability.
Returns a list of (n - 1) cut points separating the intervals.
Set *n* to 4 for quartiles (the default). Set *n* to 10 for deciles.
Set *n* to 100 for percentiles which gives the 99 cuts points that
- separate *dist* in to 100 equal sized groups.
+ separate *data* in to 100 equal sized groups.
- The *dist* can be any iterable containing sample data or it can be
+ The *data* can be any iterable containing sample data or it can be
an instance of a class that defines an inv_cdf() method. For sample
data, the cut points are linearly interpolated between data points.
- If *method* is set to *inclusive*, *dist* is treated as population
+ If *method* is set to *inclusive*, *data* is treated as population
data. The minimum value is treated as the 0th percentile and the
maximum value is treated as the 100th percentile.
"""
if n < 1:
raise StatisticsError('n must be at least 1')
- if hasattr(dist, 'inv_cdf'):
- return [dist.inv_cdf(i / n) for i in range(1, n)]
- data = sorted(dist)
+ if hasattr(data, 'inv_cdf'):
+ return [data.inv_cdf(i / n) for i in range(1, n)]
+ data = sorted(data)
ld = len(data)
if ld < 2:
raise StatisticsError('must have at least two data points')
@@ -745,7 +745,7 @@ def variance(data, xbar=None):
def pvariance(data, mu=None):
"""Return the population variance of ``data``.
- data should be an iterable of Real-valued numbers, with at least one
+ data should be a sequence or iterator of Real-valued numbers, with at least one
value. The optional argument mu, if given, should be the mean of
the data. If it is missing or None, the mean is automatically calculated.
@@ -766,10 +766,6 @@ def pvariance(data, mu=None):
>>> pvariance(data, mu)
1.25
- This function does not check that ``mu`` is actually the mean of ``data``.
- Giving arbitrary values for ``mu`` may lead to invalid or impossible
- results.
-
Decimals and Fractions are supported:
>>> from decimal import Decimal as D
@@ -913,8 +909,8 @@ class NormalDist:
"NormalDist where mu is the mean and sigma is the standard deviation."
if sigma < 0.0:
raise StatisticsError('sigma must be non-negative')
- self._mu = mu
- self._sigma = sigma
+ self._mu = float(mu)
+ self._sigma = float(sigma)
@classmethod
def from_samples(cls, data):