{"id":972377,"date":"2024-12-27T05:48:15","date_gmt":"2024-12-26T21:48:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972377.html"},"modified":"2024-12-27T05:48:17","modified_gmt":"2024-12-26T21:48:17","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e6%ad%a3%e6%80%81%e5%88%86%e5%b8%83","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/972377.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u6b63\u6001\u5206\u5e03"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195054\/a15d8ec5-8e50-4edd-a143-58fc0cbebdc2.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u6b63\u6001\u5206\u5e03\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8ba1\u7b97\u6b63\u6001\u5206\u5e03\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528SciPy\u5e93\u4e2d\u7684<code>norm<\/code>\u6a21\u5757\u3001\u4f7f\u7528NumPy\u5e93\u4e2d\u7684\u51fd\u6570\u3001\u624b\u52a8\u5b9e\u73b0\u6b63\u6001\u5206\u5e03\u516c\u5f0f\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0cSciPy\u5e93\u7684<code>norm<\/code>\u6a21\u5757\u662f\u6700\u5e38\u7528\u4e14\u529f\u80fd\u6700\u5f3a\u5927\u7684\u5de5\u5177\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0e\u6b63\u6001\u5206\u5e03\u76f8\u5173\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u3001\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u3001\u9006\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u7b49\u8ba1\u7b97\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97\u6b63\u6001\u5206\u5e03\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SciPy\u5e93\u8ba1\u7b97\u6b63\u6001\u5206\u5e03<\/p>\n<\/p>\n<p><p>SciPy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5176\u4e2d\u7684<code>stats<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>norm<\/code>\u7684\u5b50\u6a21\u5757\uff0c\u7528\u4e8e\u5904\u7406\u6b63\u6001\u5206\u5e03\u3002\u901a\u8fc7<code>norm<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff08PDF\uff09\u3001\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff08CDF\uff09\u3001\u9006\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff08PDF\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u7528\u4e8e\u63cf\u8ff0\u6b63\u6001\u5206\u5e03\u4e2d\u5404\u4e2a\u70b9\u7684\u6982\u7387\u5bc6\u5ea6\u3002\u4f7f\u7528<code>norm.pdf()<\/code>\u51fd\u6570\u53ef\u4ee5\u8ba1\u7b97\u7ed9\u5b9a\u70b9\u7684\u6982\u7387\u5bc6\u5ea6\u503c\u3002\u5176\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import norm<\/p>\n<h2><strong>\u8bbe\u7f6e\u5747\u503c\u548c\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>mu = 0<\/p>\n<p>sigma = 1<\/p>\n<h2><strong>\u8ba1\u7b97\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u503c<\/strong><\/h2>\n<p>pdf_value = norm.pdf(0, mu, sigma)<\/p>\n<p>print(f&quot;PDF at x=0: {pdf_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff08CDF\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u968f\u673a\u53d8\u91cf\u5c0f\u4e8e\u6216\u7b49\u4e8e\u67d0\u4e2a\u503c\u7684\u6982\u7387\u3002\u901a\u8fc7<code>norm.cdf()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5f97\u5230\u6b63\u6001\u5206\u5e03\u7684\u7d2f\u79ef\u5206\u5e03\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u503c<\/p>\n<p>cdf_value = norm.cdf(0, mu, sigma)<\/p>\n<p>print(f&quot;CDF at x=0: {cdf_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9006\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff08PPF\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u9006\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u7ed9\u5b9a\u6982\u7387\u5bf9\u5e94\u7684\u5206\u5e03\u503c\u3002\u901a\u8fc7<code>norm.ppf()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u6982\u7387\u4e0b\u7684\u5206\u5e03\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u9006\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u503c<\/p>\n<p>ppf_value = norm.ppf(0.5, mu, sigma)<\/p>\n<p>print(f&quot;PPF at probability=0.5: {ppf_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u6b63\u6001\u5206\u5e03<\/p>\n<\/p>\n<p><p>\u867d\u7136NumPy\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u7ec4\u548c\u77e9\u9635\u7684\u64cd\u4f5c\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u751f\u6210\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u7684<code>numpy.random.normal()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u8bbe\u7f6e\u5747\u503c\u548c\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>mu = 0<\/p>\n<p>sigma = 1<\/p>\n<h2><strong>\u751f\u621010\u4e2a\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570<\/strong><\/h2>\n<p>random_numbers = np.random.normal(mu, sigma, 10)<\/p>\n<p>print(f&quot;Random numbers: {random_numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u624b\u52a8\u5b9e\u73b0\u6b63\u6001\u5206\u5e03\u516c\u5f0f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u66f4\u6df1\u5165\u5730\u7406\u89e3\u6b63\u6001\u5206\u5e03\u7684\u8ba1\u7b97\u539f\u7406\uff0c\u53ef\u4ee5\u9009\u62e9\u624b\u52a8\u5b9e\u73b0\u6b63\u6001\u5206\u5e03\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u516c\u5f0f\u3002\u6b63\u6001\u5206\u5e03\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ f(x) = \\frac{1}{\\sqrt{2\\pi\\sigma^2}} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}} ]<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u5b9e\u73b0\u8be5\u516c\u5f0f\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def normal_pdf(x, mu=0, sigma=1):<\/p>\n<p>    return (1 \/ (math.sqrt(2 * math.pi) * sigma)) * math.exp(-((x - mu) &lt;strong&gt; 2) \/ (2 * sigma &lt;\/strong&gt; 2))<\/p>\n<h2><strong>\u8ba1\u7b97\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u503c<\/strong><\/h2>\n<p>pdf_value_manual = normal_pdf(0)<\/p>\n<p>print(f&quot;Manual PDF at x=0: {pdf_value_manual}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u5b9e\u7528\u529f\u80fd<\/p>\n<\/p>\n<ol>\n<li><strong>\u7ed8\u5236\u6b63\u6001\u5206\u5e03\u66f2\u7ebf<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7Matplotlib\u5e93\uff0c\u53ef\u4ee5\u7ed8\u5236\u6b63\u6001\u5206\u5e03\u7684\u6982\u7387\u5bc6\u5ea6\u66f2\u7ebf\uff0c\u4ee5\u4fbf\u66f4\u76f4\u89c2\u5730\u89c2\u5bdf\u5176\u7279\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210x\u8f74\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-5, 5, 1000)<\/p>\n<h2><strong>\u8ba1\u7b97y\u8f74\u6570\u636e\uff08\u6982\u7387\u5bc6\u5ea6\uff09<\/strong><\/h2>\n<p>y = norm.pdf(x, mu, sigma)<\/p>\n<h2><strong>\u7ed8\u5236\u66f2\u7ebf<\/strong><\/h2>\n<p>plt.plot(x, y, label=&#39;Normal Distribution&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;Probability Density&#39;)<\/p>\n<p>plt.title(&#39;Normal Distribution Curve&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u751f\u6210\u591a\u7ef4\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u5e93\u8fd8\u652f\u6301\u751f\u6210\u591a\u7ef4\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u8fd9\u5728\u591a\u5143\u7edf\u8ba1\u5206\u6790\u4e2d\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u5b9a\u5747\u503c\u5411\u91cf\u548c\u534f\u65b9\u5dee\u77e9\u9635<\/p>\n<p>mean = [0, 0]<\/p>\n<p>cov = [[1, 0], [0, 1]]<\/p>\n<h2><strong>\u751f\u6210\u4e8c\u7ef4\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570<\/strong><\/h2>\n<p>multivariate_random_numbers = np.random.multivariate_normal(mean, cov, 10)<\/p>\n<p>print(f&quot;Multivariate random numbers: {multivariate_random_numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a\u5728Python\u4e2d\u8ba1\u7b97\u6b63\u6001\u5206\u5e03\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0cSciPy\u5e93\u63d0\u4f9b\u4e86\u529f\u80fd\u9f50\u5168\u7684\u7edf\u8ba1\u6a21\u5757\uff0cNumPy\u5e93\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u968f\u673a\u6570\uff0c\u800c\u624b\u52a8\u5b9e\u73b0\u516c\u5f0f\u5219\u6709\u52a9\u4e8e\u7406\u89e3\u5176\u6570\u5b66\u539f\u7406\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u4e0e\u6b63\u6001\u5206\u5e03\u76f8\u5173\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\u3002\u4f7f\u7528<code>numpy.random.normal<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6307\u5b9a\u5747\u503c\u3001\u6807\u51c6\u5dee\u548c\u751f\u6210\u6570\u636e\u7684\u6570\u91cf\u3002\u4f8b\u5982\uff0c<code>numpy.random.normal(loc=0.0, scale=1.0, size=1000)<\/code>\u4f1a\u751f\u62101000\u4e2a\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\u7684\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u53ef\u89c6\u5316\u6b63\u6001\u5206\u5e03\u7684\u5de5\u5177\uff1f<\/strong><br \/>\u4e3a\u4e86\u53ef\u89c6\u5316\u6b63\u6001\u5206\u5e03\uff0cMatplotlib\u548cSeaborn\u662f\u4e24\u79cd\u5e38\u7528\u7684\u5e93\u3002\u4f7f\u7528Matplotlib\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>plt.hist(data, bins=30, density=True)<\/code>\u7ed8\u5236\u76f4\u65b9\u56fe\uff0c\u5e76\u901a\u8fc7<code>plt.plot(x, y)<\/code>\u53e0\u52a0\u6b63\u6001\u5206\u5e03\u66f2\u7ebf\u3002Seaborn\u5219\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u7684\u63a5\u53e3\uff0c\u4f7f\u7528<code>seaborn.histplot(data, kde=True)<\/code>\u53ef\u4ee5\u540c\u65f6\u7ed8\u5236\u76f4\u65b9\u56fe\u548c\u6838\u5bc6\u5ea6\u4f30\u8ba1\u3002<\/p>\n<p><strong>\u5982\u4f55\u8ba1\u7b97\u6b63\u6001\u5206\u5e03\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff08PDF\uff09\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u7684<code>scipy.stats.norm.pdf<\/code>\u51fd\u6570\u8ba1\u7b97\u6b63\u6001\u5206\u5e03\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u3002\u60a8\u9700\u8981\u63d0\u4f9b\u5747\u503c\u3001\u6807\u51c6\u5dee\u548c\u60f3\u8981\u8ba1\u7b97\u7684\u70b9\u3002\u4f8b\u5982\uff0c<code>scipy.stats.norm.pdf(x, loc=0, scale=1)<\/code>\u5c06\u8fd4\u56de\u5728\u5747\u503c\u4e3a0\u548c\u6807\u51c6\u5dee\u4e3a1\u7684\u6b63\u6001\u5206\u5e03\u4e0b\uff0c\u70b9x\u7684\u6982\u7387\u5bc6\u5ea6\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8ba1\u7b97\u6b63\u6001\u5206\u5e03\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528SciPy\u5e93\u4e2d\u7684norm\u6a21\u5757\u3001\u4f7f\u7528NumPy\u5e93\u4e2d\u7684\u51fd\u6570\u3001\u624b\u52a8\u5b9e\u73b0\u6b63 [&hellip;]","protected":false},"author":3,"featured_media":972387,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972377"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=972377"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972377\/revisions"}],"predecessor-version":[{"id":972390,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972377\/revisions\/972390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972387"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}