{"id":1018943,"date":"2024-12-27T12:48:37","date_gmt":"2024-12-27T04:48:37","guid":{"rendered":""},"modified":"2024-12-27T12:48:43","modified_gmt":"2024-12-27T04:48:43","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e6%a6%82%e7%8e%87%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018943.html","title":{"rendered":"python\u5982\u4f55\u6c42\u6982\u7387\u95ee\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161532\/34f14ad9-f54d-4976-b994-9a227f840289.webp\" alt=\"python\u5982\u4f55\u6c42\u6982\u7387\u95ee\u9898\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6c42\u89e3\u6982\u7387\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u6a21\u62df\u5b9e\u9a8c\u3001\u7edf\u8ba1\u5206\u6790<\/strong>\u7b49\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u65f6\u8981\u6839\u636e\u95ee\u9898\u7684\u6027\u8d28\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u4f8b\u5982\uff0c<strong>\u5185\u7f6e\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6982\u7387\u5206\u5e03\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8ba1\u7b97\u7406\u8bba\u6982\u7387<\/strong>\uff0c\u800c<strong>\u6a21\u62df\u5b9e\u9a8c\u5219\u9002\u7528\u4e8e\u590d\u6742\u6216\u96be\u4ee5\u76f4\u63a5\u8ba1\u7b97\u7684\u6982\u7387\u95ee\u9898<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u5e93\u8fdb\u884c\u6982\u7387\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>Python\u4e2d\u6709\u591a\u4e2a\u5185\u7f6e\u5e93\u53ef\u4ee5\u7528\u4e8e\u6982\u7387\u8ba1\u7b97\uff0c\u6700\u5e38\u7528\u7684\u662f<code>scipy.stats<\/code>\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u5404\u79cd\u6982\u7387\u5206\u5e03\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u3001\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u3001\u5206\u4f4d\u6570\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528<code>scipy.stats<\/code>\u5e93<\/strong><\/li>\n<\/ol>\n<p><p><code>scipy.stats<\/code>\u5e93\u662fPython\u4e2d\u5904\u7406\u6982\u7387\u548c\u7edf\u8ba1\u95ee\u9898\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u652f\u6301\u591a\u79cd\u6982\u7387\u5206\u5e03\uff0c\u5982\u6b63\u6001\u5206\u5e03\u3001\u6cca\u677e\u5206\u5e03\u3001\u4e8c\u9879\u5206\u5e03\u7b49\u3002\u901a\u8fc7\u8fd9\u4e2a\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff08PDF\uff09\u3001\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff08CDF\uff09\u548c\u5206\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8ba1\u7b97\u6807\u51c6\u6b63\u6001\u5206\u5e03\u5728\u67d0\u4e2a\u70b9\u7684\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import norm<\/p>\n<h2><strong>\u8ba1\u7b97\u6807\u51c6\u6b63\u6001\u5206\u5e03\u5728z=1.96\u7684\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u503c<\/strong><\/h2>\n<p>prob = norm.cdf(1.96)<\/p>\n<p>print(prob)  # \u8f93\u51fa: 0.9750021048517795<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u610f\u5473\u7740\uff0c\u5728\u6807\u51c6\u6b63\u6001\u5206\u5e03\u4e2d\uff0c\u968f\u673a\u53d8\u91cf\u5c0f\u4e8e1.96\u7684\u6982\u7387\u7ea6\u4e3a0.975\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5e38\u7528\u7684\u6982\u7387\u5206\u5e03<\/strong><\/li>\n<\/ol>\n<p><p>\u5728<code>scipy.stats<\/code>\u4e2d\uff0c\u5e38\u7528\u7684\u6982\u7387\u5206\u5e03\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6b63\u6001\u5206\u5e03\uff08Normal Distribution\uff09<\/strong>\uff1a\u7528\u4e8e\u6d4b\u91cf\u5e73\u5747\u503c\u548c\u6807\u51c6\u5dee\u3002<\/li>\n<li><strong>\u6cca\u677e\u5206\u5e03\uff08Poisson Distribution\uff09<\/strong>\uff1a\u7528\u4e8e\u8ba1\u6570\u4e8b\u4ef6\u7684\u53d1\u751f\u3002<\/li>\n<li><strong>\u4e8c\u9879\u5206\u5e03\uff08Binomial Distribution\uff09<\/strong>\uff1a\u7528\u4e8e\u6210\u529f\u4e0e\u5931\u8d25\u7684\u4e8c\u5143\u7ed3\u679c\u3002<\/li>\n<li><strong>\u6307\u6570\u5206\u5e03\uff08Exponential Distribution\uff09<\/strong>\uff1a\u7528\u4e8e\u6d4b\u91cf\u65f6\u95f4\u95f4\u9694\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8c03\u7528\u8fd9\u4e9b\u5206\u5e03\u7684\u76f8\u5173\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u7406\u8bba\u6982\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6a21\u62df\u5b9e\u9a8c\u8fdb\u884c\u6982\u7387\u4f30\u8ba1<\/p>\n<\/p>\n<p><p>\u6a21\u62df\u5b9e\u9a8c\u662f\u4e00\u79cd\u901a\u8fc7\u91cd\u590d\u968f\u673a\u8bd5\u9a8c\u6765\u4f30\u8ba1\u6982\u7387\u7684\u65b9\u6cd5\uff0c\u901a\u5e38\u79f0\u4e3a\u8499\u7279\u5361\u7f57\u6a21\u62df\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u590d\u6742\u6216\u96be\u4ee5\u76f4\u63a5\u8ba1\u7b97\u7684\u6982\u7387\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u601d\u60f3<\/strong><\/li>\n<\/ol>\n<p><p>\u8499\u7279\u5361\u7f57\u6a21\u62df\u7684\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u5927\u89c4\u6a21\u7684\u968f\u673a\u8bd5\u9a8c\u6765\u903c\u8fd1\u7406\u8bba\u6982\u7387\u3002\u901a\u8fc7\u4e0d\u65ad\u91cd\u590d\u8bd5\u9a8c\uff0c\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u903c\u8fd1\u771f\u5b9e\u6982\u7387\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f30\u8ba1\u629b\u786c\u5e01\u65f6\u6b63\u9762\u671d\u4e0a\u7684\u6982\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def simulate_coin_flips(num_flips):<\/p>\n<p>    heads_count = 0<\/p>\n<p>    for _ in range(num_flips):<\/p>\n<p>        if random.random() &lt; 0.5:<\/p>\n<p>            heads_count += 1<\/p>\n<p>    return heads_count \/ num_flips<\/p>\n<h2><strong>\u6a21\u62df10000\u6b21\u629b\u786c\u5e01<\/strong><\/h2>\n<p>estimated_prob = simulate_coin_flips(10000)<\/p>\n<p>print(estimated_prob)  # \u8f93\u51fa\u63a5\u8fd10.5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5e94\u7528\u573a\u666f<\/strong><\/li>\n<\/ol>\n<p><p>\u6a21\u62df\u5b9e\u9a8c\u9002\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u6982\u7387\u95ee\u9898<\/strong>\uff1a\u5982\u591a\u7ef4\u968f\u673a\u53d8\u91cf\u7684\u6982\u7387\u8ba1\u7b97\u3002<\/li>\n<li><strong>\u65e0\u660e\u786e\u89e3\u6790\u89e3\u7684\u95ee\u9898<\/strong>\uff1a\u5982\u590d\u6742\u7684\u7ec4\u5408\u6982\u7387\u3002<\/li>\n<li><strong>\u9a8c\u8bc1\u7406\u8bba\u7ed3\u679c<\/strong>\uff1a\u901a\u8fc7\u6a21\u62df\u9a8c\u8bc1\u5df2\u77e5\u7684\u7406\u8bba\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u7edf\u8ba1\u5206\u6790\u4e2d\u7684\u6982\u7387\u8ba1\u7b97<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6982\u7387\u8ba1\u7b97\u5e38\u7528\u4e8e\u63cf\u8ff0\u6570\u636e\u7684\u5206\u5e03\u3001\u6d4b\u8bd5\u5047\u8bbe\u7b49\u3002Python\u7684<code>pandas<\/code>\u548c<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u652f\u6301\u8fd9\u4e9b\u5206\u6790\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u63cf\u8ff0<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8ba1\u7b97\u6570\u636e\u7684\u5747\u503c\u3001\u65b9\u5dee\u3001\u6807\u51c6\u5dee\u7b49\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf\uff0c\u53ef\u4ee5\u83b7\u5f97\u6570\u636e\u7684\u57fa\u672c\u6982\u7387\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])<\/p>\n<p>mean = data.mean()<\/p>\n<p>std_dev = data.std()<\/p>\n<p>print(f&quot;Mean: {mean}, Standard Deviation: {std_dev}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5047\u8bbe\u68c0\u9a8c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u5047\u8bbe\u68c0\u9a8c\u662f\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u6837\u672c\u6570\u636e\u662f\u5426\u652f\u6301\u67d0\u4e2a\u5047\u8bbe\u3002\u901a\u8fc7\u8ba1\u7b97p\u503c\uff0c\u53ef\u4ee5\u5224\u65ad\u89c2\u5bdf\u7ed3\u679c\u7684\u663e\u8457\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528t\u68c0\u9a8c\u6765\u5224\u65ad\u4e24\u4e2a\u6837\u672c\u5747\u503c\u662f\u5426\u76f8\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import ttest_ind<\/p>\n<h2><strong>\u4e24\u4e2a\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>sample1 = [1, 2, 3, 4, 5]<\/p>\n<p>sample2 = [2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u6267\u884ct\u68c0\u9a8c<\/strong><\/h2>\n<p>t_stat, p_value = ttest_ind(sample1, sample2)<\/p>\n<p>print(f&quot;T-statistic: {t_stat}, P-value: {p_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cp\u503c\u5c0f\u4e8e\u67d0\u4e2a\u663e\u8457\u6027\u6c34\u5e73\uff08\u59820.05\uff09\uff0c\u5219\u62d2\u7edd\u539f\u5047\u8bbe\uff0c\u5373\u8ba4\u4e3a\u4e24\u4e2a\u6837\u672c\u5747\u503c\u6709\u663e\u8457\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6982\u7387\u5206\u5e03\u7684\u53ef\u89c6\u5316<\/p>\n<\/p>\n<p><p>\u53ef\u89c6\u5316\u662f\u7406\u89e3\u6982\u7387\u5206\u5e03\u7684\u91cd\u8981\u5de5\u5177\u3002\u901a\u8fc7\u56fe\u5f62\u5316\u5c55\u793a\u6982\u7387\u5206\u5e03\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u6570\u636e\u7684\u5206\u5e03\u7279\u5f81\u3002Python\u7684<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u53ef\u89c6\u5316\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u76f4\u65b9\u56fe<\/strong><\/li>\n<\/ol>\n<p><p>\u76f4\u65b9\u56fe\u662f\u5c55\u793a\u6570\u636e\u5206\u5e03\u6700\u5e38\u7528\u7684\u56fe\u5f62\u4e4b\u4e00\uff0c\u53ef\u4ee5\u663e\u793a\u6570\u636e\u5728\u4e0d\u540c\u533a\u95f4\u7684\u9891\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>data = [1, 2, 2, 3, 3, 3, 4, 4, 5]<\/p>\n<p>plt.hist(data, bins=5, edgecolor=&#39;black&#39;)<\/p>\n<p>plt.title(&#39;Histogram&#39;)<\/p>\n<p>plt.xlabel(&#39;Value&#39;)<\/p>\n<p>plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff08PDF\uff09\u548c\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff08CDF\uff09<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7ed8\u5236\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u548c\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u8be6\u7ec6\u5730\u89c2\u5bdf\u6982\u7387\u5206\u5e03\u7684\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import seaborn as sns<\/p>\n<h2><strong>\u751f\u6210\u6b63\u6001\u5206\u5e03\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.normal(loc=0, scale=1, size=1000)<\/p>\n<h2><strong>\u7ed8\u5236PDF<\/strong><\/h2>\n<p>sns.kdeplot(data, bw=0.5)<\/p>\n<p>plt.title(&#39;Probability Density Function&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236CDF<\/strong><\/h2>\n<p>sns.ecdfplot(data)<\/p>\n<p>plt.title(&#39;Cumulative Distribution Function&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u8d1d\u53f6\u65af\u6982\u7387\u4e0e\u63a8\u65ad<\/p>\n<\/p>\n<p><p>\u8d1d\u53f6\u65af\u6982\u7387\u662f\u4e00\u79cd\u901a\u8fc7\u66f4\u65b0\u5148\u9a8c\u6982\u7387\u6765\u8ba1\u7b97\u540e\u9a8c\u6982\u7387\u7684\u65b9\u6cd5\u3002\u5b83\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u7edf\u8ba1\u63a8\u65ad\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8d1d\u53f6\u65af\u516c\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u8d1d\u53f6\u65af\u516c\u5f0f\u662f\u8d1d\u53f6\u65af\u6982\u7387\u7684\u6838\u5fc3\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e8b\u4ef6A\u53d1\u751f\u7684\u6982\u7387\uff0c\u7ed9\u5b9a\u4e8b\u4ef6B\u5df2\u7ecf\u53d1\u751f\u3002\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ P(A|B) = \\frac{P(B|A) \\cdot P(A)}{P(B)} ]<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5e94\u7528\u793a\u4f8b<\/strong><\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6709\u4e00\u9879\u6d4b\u8bd5\uff0c\u5176\u51c6\u786e\u7387\u4e3a99%\uff0c\u67d0\u79cd\u75be\u75c5\u7684\u53d1\u75c5\u7387\u4e3a0.1%\u3002\u5982\u679c\u6d4b\u8bd5\u7ed3\u679c\u4e3a\u9633\u6027\uff0c\u6c42\u60a3\u8005\u5b9e\u9645\u60a3\u75c5\u7684\u6982\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed9\u5b9a\u6570\u636e<\/p>\n<p>p_disease = 0.001<\/p>\n<p>p_positive_given_disease = 0.99<\/p>\n<p>p_positive_given_no_disease = 0.01<\/p>\n<h2><strong>\u8d1d\u53f6\u65af\u516c\u5f0f\u8ba1\u7b97\u540e\u9a8c\u6982\u7387<\/strong><\/h2>\n<p>p_no_disease = 1 - p_disease<\/p>\n<p>p_positive = (p_positive_given_disease * p_disease) + (p_positive_given_no_disease * p_no_disease)<\/p>\n<p>p_disease_given_positive = (p_positive_given_disease * p_disease) \/ p_positive<\/p>\n<p>print(f&quot;Probability of disease given positive test: {p_disease_given_positive}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8d1d\u53f6\u65af\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u5c3d\u7ba1\u6d4b\u8bd5\u51c6\u786e\u7387\u5f88\u9ad8\uff0c\u4f46\u7531\u4e8e\u75be\u75c5\u672c\u8eab\u7684\u53d1\u75c5\u7387\u6781\u4f4e\uff0c\u5b9e\u9645\u60a3\u75c5\u7684\u6982\u7387\u5e76\u4e0d\u9ad8\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u9a6c\u5c14\u53ef\u592b\u94fe\u4e0e\u6982\u7387\u8f6c\u79fb<\/p>\n<\/p>\n<p><p>\u9a6c\u5c14\u53ef\u592b\u94fe\u662f\u4e00\u79cd\u7528\u4e8e\u63cf\u8ff0\u7cfb\u7edf\u72b6\u6001\u8f6c\u79fb\u7684\u6570\u5b66\u6a21\u578b\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u968f\u673a\u8fc7\u7a0b\u548c\u52a8\u6001\u7cfb\u7edf\u7684\u7814\u7a76\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u9a6c\u5c14\u53ef\u592b\u94fe\u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/li>\n<\/ol>\n<p><p>\u9a6c\u5c14\u53ef\u592b\u94fe\u7531\u72b6\u6001\u7a7a\u95f4\u548c\u8f6c\u79fb\u6982\u7387\u77e9\u9635\u7ec4\u6210\u3002\u72b6\u6001\u7a7a\u95f4\u662f\u7cfb\u7edf\u53ef\u80fd\u7684\u72b6\u6001\u96c6\u5408\uff0c\u8f6c\u79fb\u6982\u7387\u77e9\u9635\u63cf\u8ff0\u4e86\u4ece\u4e00\u4e2a\u72b6\u6001\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u72b6\u6001\u7684\u6982\u7387\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5e94\u7528\u793a\u4f8b<\/strong><\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u5929\u6c14\u6a21\u578b\uff0c\u4ec5\u6709\u4e24\u79cd\u72b6\u6001\uff1a\u6674\u5929\u548c\u96e8\u5929\u3002\u72b6\u6001\u8f6c\u79fb\u77e9\u9635\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ P = \\begin{bmatrix} 0.8 &amp; 0.2 \\ 0.4 &amp; 0.6 \\end{bmatrix} ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u884c\u8868\u793a\u5f53\u524d\u72b6\u6001\uff0c\u5217\u8868\u793a\u4e0b\u4e00\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u4ece\u6674\u5929\u8f6c\u79fb\u5230\u96e8\u5929\u7684\u6982\u7387\u4e3a0.2\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6a21\u62df\u9a6c\u5c14\u53ef\u592b\u94fe\uff0c\u53ef\u4ee5\u9884\u6d4b\u672a\u6765\u5929\u6c14\u72b6\u6001\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u72b6\u6001\u8f6c\u79fb\u77e9\u9635<\/strong><\/h2>\n<p>P = np.array([[0.8, 0.2], [0.4, 0.6]])<\/p>\n<h2><strong>\u521d\u59cb\u72b6\u6001\uff1a\u6674\u5929<\/strong><\/h2>\n<p>state = 0  # 0\u8868\u793a\u6674\u5929\uff0c1\u8868\u793a\u96e8\u5929<\/p>\n<h2><strong>\u6a21\u62df10\u5929\u7684\u5929\u6c14\u53d8\u5316<\/strong><\/h2>\n<p>for day in range(10):<\/p>\n<p>    state = np.random.choice([0, 1], p=P[state])<\/p>\n<p>    print(f&quot;Day {day+1}: {&#39;Sunny&#39; if state == 0 else &#39;R<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ny&#39;}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u89c2\u611f\u53d7\u9a6c\u5c14\u53ef\u592b\u94fe\u5728\u6982\u7387\u8f6c\u79fb\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u6982\u7387\u95ee\u9898\u4e2d\u7684\u5e38\u89c1\u9677\u9631\u4e0e\u8bef\u533a<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6982\u7387\u95ee\u9898\u65f6\uff0c\u5bb9\u6613\u51fa\u73b0\u4e00\u4e9b\u5e38\u89c1\u7684\u9677\u9631\u4e0e\u8bef\u533a\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u72ec\u7acb\u6027\u5047\u8bbe<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8ba1\u7b97\u8054\u5408\u6982\u7387\u65f6\uff0c\u5e38\u5e38\u5047\u8bbe\u4e8b\u4ef6\u662f\u72ec\u7acb\u7684\u3002\u7136\u800c\uff0c\u5728\u5b9e\u9645\u95ee\u9898\u4e2d\uff0c\u4e8b\u4ef6\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u590d\u6742\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u8bef\u7528\u72ec\u7acb\u6027\u5047\u8bbe\u4f1a\u5bfc\u81f4\u9519\u8bef\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6df7\u6dc6\u56e0\u679c\u5173\u7cfb\u4e0e\u76f8\u5173\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u76f8\u5173\u6027\u4e0d\u4ee3\u8868\u56e0\u679c\u5173\u7cfb\u3002\u5728\u5206\u6790\u6982\u7387\u95ee\u9898\u65f6\uff0c\u8981\u8c28\u614e\u533a\u5206\u56e0\u679c\u5173\u7cfb\u4e0e\u7eaf\u7cb9\u7684\u7edf\u8ba1\u76f8\u5173\u6027\uff0c\u907f\u514d\u5f97\u51fa\u8bef\u5bfc\u6027\u7684\u7ed3\u8bba\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8fc7\u5ea6\u4f9d\u8d56\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u6982\u7387\u6a21\u578b\u662f\u5bf9\u73b0\u5b9e\u4e16\u754c\u7684\u7b80\u5316\uff0c\u4e0d\u80fd\u8fc7\u5ea6\u4f9d\u8d56\u6a21\u578b\u7ed3\u679c\u3002\u6a21\u578b\u5047\u8bbe\u548c\u53c2\u6570\u7684\u4e0d\u51c6\u786e\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u6982\u7387\u4f30\u8ba1\uff0c\u56e0\u6b64\u9700\u8981\u7ed3\u5408\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\u7684\u8be6\u7ec6\u63a2\u8ba8\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u8fdb\u884c\u6982\u7387\u95ee\u9898\u7684\u6c42\u89e3\u3002\u4ece\u5185\u7f6e\u5e93\u7684\u4f7f\u7528\u5230\u6a21\u62df\u5b9e\u9a8c\uff0c\u518d\u5230\u7edf\u8ba1\u5206\u6790\u548c\u8d1d\u53f6\u65af\u63a8\u65ad\uff0cPython\u4e3a\u6982\u7387\u95ee\u9898\u7684\u89e3\u51b3\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u5de5\u5177\u3002\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5404\u7c7b\u5b9e\u9645\u95ee\u9898\u4e2d\u7075\u6d3b\u5e94\u7528\u6982\u7387\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u6982\u7387\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u6982\u7387\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u5b66\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982NumPy\u548cSciPy\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5e93\u4e2d\u7684\u51fd\u6570\u6765\u5904\u7406\u968f\u673a\u53d8\u91cf\u3001\u5206\u5e03\u548c\u7edf\u8ba1\u5206\u6790\uff0c\u4ece\u800c\u5f97\u51fa\u76f8\u5173\u7684\u6982\u7387\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528<code>numpy.random<\/code>\u751f\u6210\u968f\u673a\u6570\uff0c\u5e76\u901a\u8fc7\u7edf\u8ba1\u8fd9\u4e9b\u968f\u673a\u6570\u6765<a href=\"https:\/\/docs.pingcode.com\/agile\/project-management\/estimation\" target=\"_blank\">\u4f30\u7b97<\/a>\u6982\u7387\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6982\u7387\u5206\u5e03\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cNumPy\u548cSciPy\u662f\u6700\u5e38\u7528\u7684\u6982\u7387\u5206\u5e03\u5e93\u3002NumPy\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u968f\u673a\u6570\u751f\u6210\u548c\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u800cSciPy\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u7edf\u8ba1\u5206\u5e03\u548c\u6982\u7387\u8ba1\u7b97\u529f\u80fd\u3002\u6b64\u5916\uff0cPandas\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u6570\u636e\u5206\u6790\u548c\u6982\u7387\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6570\u636e\u6846\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6982\u7387\u7684\u6a21\u62df\u5b9e\u9a8c\uff1f<\/strong><br \/>\u6a21\u62df\u5b9e\u9a8c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199Python\u4ee3\u7801\u6765\u5b9e\u73b0\uff0c\u901a\u8fc7\u968f\u673a\u62bd\u6837\u6765\u6a21\u62df\u67d0\u4e9b\u4e8b\u4ef6\u7684\u53d1\u751f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.choice<\/code>\u51fd\u6570\u8fdb\u884c\u62bd\u6837\uff0c\u6a21\u62df\u629b\u786c\u5e01\u3001\u62bd\u5956\u7b49\u60c5\u5883\u3002\u901a\u8fc7\u591a\u6b21\u6a21\u62df\uff0c\u53ef\u4ee5\u89c2\u5bdf\u5230\u4e8b\u4ef6\u53d1\u751f\u7684\u9891\u7387\uff0c\u4ece\u800c\u4f30\u7b97\u5176\u6982\u7387\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5b9e\u9645\u95ee\u9898\u4e2d\u975e\u5e38\u6709\u6548\uff0c\u5e76\u4e14\u6613\u4e8e\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6c42\u89e3\u6982\u7387\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u6a21\u62df\u5b9e\u9a8c\u3001\u7edf\u8ba1\u5206\u6790\u7b49\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18 [&hellip;]","protected":false},"author":3,"featured_media":1018960,"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\/1018943"}],"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=1018943"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018943\/revisions"}],"predecessor-version":[{"id":1018962,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018943\/revisions\/1018962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018960"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}