bundles / numpy latest / numpy / random / _generator / Generator / zipf
cython_function_or_method
numpy.random._generator:Generator.zipf
Signature
def zipf ( a , size = None ) Summary
Draw samples from a Zipf distribution.
Extended Summary
Samples are drawn from a Zipf distribution with specified parameter a > 1.
The Zipf distribution (also known as the zeta distribution) is a discrete probability distribution that satisfies Zipf's law: the frequency of an item is inversely proportional to its rank in a frequency table.
Parameters
a: float or array_like of floatsDistribution parameter. Must be greater than 1.
size: int or tuple of ints, optionalOutput shape. If the given shape is, e.g.,
(m, n, k), thenm * n * ksamples are drawn. If size isNone(default), a single value is returned ifais a scalar. Otherwise,np.array(a).sizesamples are drawn.
Returns
out: ndarray or scalarDrawn samples from the parameterized Zipf distribution.
Notes
The probability mass function (PMF) for the Zipf distribution is
for integers , where is the Riemann Zeta function.
It is named for the American linguist George Kingsley Zipf, who noted that the frequency of any word in a sample of a language is inversely proportional to its rank in the frequency table.
Examples
Draw samples from the distribution:a = 4.0 n = 20000 rng = np.random.default_rng() s = rng.zipf(a, size=n)✓
import matplotlib.pyplot as plt
✓count = np.bincount(s) k = np.arange(1, s.max() + 1)✓
plt.bar(k, count[1:], alpha=0.5, label='sample count') plt.semilogy()✗
plt.grid(alpha=0.4)
✓plt.legend() plt.title(f'Zipf sample, a={a}, size={n}')✗
plt.show()
✓
See also
- scipy.stats.zipf
probability density function, distribution, or cumulative density function, etc.
Aliases
-
numpy.random.Generator.zipf