Skip to content

Commit f0cce63

Browse files
DOC: random: Update the zipf example for the legacy RandomState method.
1 parent cf71558 commit f0cce63

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

numpy/random/mtrand.pyx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,21 +3660,29 @@ cdef class RandomState:
36603660
--------
36613661
Draw samples from the distribution:
36623662
3663-
>>> a = 2. # parameter
3664-
>>> s = np.random.zipf(a, 1000)
3663+
>>> a = 4.0
3664+
>>> n = 20000
3665+
>>> s = np.random.zipf(a, n)
36653666
36663667
Display the histogram of the samples, along with
3667-
the probability density function:
3668+
the expected histogram based on the probability
3669+
density function:
36683670
36693671
>>> import matplotlib.pyplot as plt
3670-
>>> from scipy import special # doctest: +SKIP
3672+
>>> from scipy.special import zeta # doctest: +SKIP
3673+
3674+
`bincount` provides a fast histogram for small integers.
36713675
3672-
Truncate s values at 50 so plot is interesting:
3676+
>>> count = np.bincount(s)
3677+
>>> x = np.arange(1, s.max() + 1)
36733678
3674-
>>> count, bins, ignored = plt.hist(s[s<50], 50, density=True)
3675-
>>> x = np.arange(1., 50.)
3676-
>>> y = x**(-a) / special.zetac(a) # doctest: +SKIP
3677-
>>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP
3679+
>>> plt.bar(x, count[1:], alpha=0.5, label='sample count')
3680+
>>> plt.plot(x, n*(x**-a)/zeta(a), 'k.-', alpha=0.5,
3681+
... label='expected count') # doctest: +SKIP
3682+
>>> plt.semilogy()
3683+
>>> plt.grid(alpha=0.4)
3684+
>>> plt.legend()
3685+
>>> plt.title(f'Zipf sample, a={a}, size={n}')
36783686
>>> plt.show()
36793687
36803688
"""

0 commit comments

Comments
 (0)