numpy.random.zipf

random.zipf(a, size=None)

从Zipf分布中提取样本。

从具有指定参数的zipf分布中提取样本 a >1。

zipf分布(也称为zeta分布)是一个连续的概率分布,满足zipf定律:一个项目的频率与它在频率表中的排名成反比。

注解

新代码应该使用 zipf A方法 default_rng() 请参阅 快速启动 .

参数
a浮点数或类似浮点数的数组

分布参数。必须大于1。

sizeint或int的元组,可选

输出形状。如果给定的形状是,例如, (m, n, k) 然后 m * n * k 取样。如果尺寸是 None (默认),如果 a 是标量。否则, np.array(a).size 取样。

返回
outndarray或scalar

从参数化zipf分布中提取样本。

参见

scipy.stats.zipf

概率密度函数、分布或累积密度函数等。

Generator.zipf

应该用于新代码。

笔记

Zipf分布的概率密度为

System Message: WARNING/2 (p(x)=\frac x^-a \zeta(a),)

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Missing { inserted. <to be read again> } l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Missing } inserted. <inserted text> } l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Missing { inserted. <to be read again> } l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...x)=\frac x^-a \zeta(a),\end{split} ! Missing } inserted. <inserted text> } l.14 ...x)=\frac x^-a \zeta(a),\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 388 bytes). Transcript written on math.log.

在哪里? \zeta 是黎曼齐塔函数。

它是以美国语言学家乔治·金斯利·齐普夫命名的,他指出,语言样本中任何单词的频率与频率表中的排名成反比。

工具书类

1

Zipf,G.K.,“语言相对频率原理的精选研究”,马萨诸塞州剑桥:哈佛大学出版社,1932年。

实例

从分发中抽取样本:

>>> a = 2. # parameter
>>> s = np.random.zipf(a, 1000)

显示样本的直方图,以及概率密度函数:

>>> import matplotlib.pyplot as plt
>>> from scipy import special  

在50处截断S值,因此绘图很有趣:

>>> count, bins, ignored = plt.hist(s[s<50], 50, density=True)
>>> x = np.arange(1., 50.)
>>> y = x**(-a) / special.zetac(a)  
>>> plt.plot(x, y/max(y), linewidth=2, color='r')  
>>> plt.show()
../../../_images/numpy-random-zipf-1.png