Skip to content

Commit 613c0d4

Browse files
authored
Add example for linear_regression() with proportional=True. (gh-110133)
1 parent 14098b7 commit 613c0d4

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Doc/library/statistics.rst

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
.. testsetup:: *
1515

1616
from statistics import *
17+
import math
1718
__name__ = '<doctest>'
1819

1920
--------------
@@ -741,6 +742,24 @@ However, for reading convenience, most of the examples show sorted sequences.
741742

742743
*y = slope \* x + noise*
743744

745+
Continuing the example from :func:`correlation`, we look to see
746+
how well a model based on major planets can predict the orbital
747+
distances for dwarf planets:
748+
749+
.. doctest::
750+
751+
>>> model = linear_regression(period_squared, dist_cubed, proportional=True)
752+
>>> slope = model.slope
753+
754+
>>> # Dwarf planets: Pluto, Eris, Makemake, Haumea, Ceres
755+
>>> orbital_periods = [90_560, 204_199, 111_845, 103_410, 1_680] # days
756+
>>> predicted_dist = [math.cbrt(slope * (p * p)) for p in orbital_periods]
757+
>>> list(map(round, predicted_dist))
758+
[5912, 10166, 6806, 6459, 414]
759+
760+
>>> [5_906, 10_152, 6_796, 6_450, 414] # actual distance in million km
761+
[5906, 10152, 6796, 6450, 414]
762+
744763
.. versionadded:: 3.10
745764

746765
.. versionchanged:: 3.11

0 commit comments

Comments
 (0)