{"id":1089016,"date":"2025-01-08T13:49:40","date_gmt":"2025-01-08T05:49:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089016.html"},"modified":"2025-01-08T13:49:43","modified_gmt":"2025-01-08T05:49:43","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e7%bb%9f%e8%ae%a1%e5%88%86%e6%9e%90-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089016.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u7edf\u8ba1\u5206\u6790"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24201211\/8b0b7218-c312-4bf1-a53f-51c5dfb9a4b0.webp\" alt=\"\u5982\u4f55\u7528python\u505a\u7edf\u8ba1\u5206\u6790\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u505a\u7edf\u8ba1\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>Python \u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u7edf\u8ba1\u5206\u6790\u4e2d\u3002<strong>\u9996\u5148\u9700\u8981\u5bfc\u5165\u76f8\u5173\u5e93\u3001\u51c6\u5907\u548c\u6e05\u6d17\u6570\u636e\u3001\u8fdb\u884c\u63cf\u8ff0\u6027\u7edf\u8ba1\u5206\u6790\u3001\u53ef\u89c6\u5316\u6570\u636e\u3001\u8fdb\u884c\u63a8\u65ad\u6027\u7edf\u8ba1\u5206\u6790\u3001\u89e3\u91ca\u548c\u62a5\u544a\u7ed3\u679c<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u70b9\uff0c\u5373\u51c6\u5907\u548c\u6e05\u6d17\u6570\u636e\uff0c\u8fd9\u662f\u7edf\u8ba1\u5206\u6790\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><p><strong>\u6570\u636e\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u7684\u57fa\u7840<\/strong>\uff0c\u56e0\u4e3a\u539f\u59cb\u6570\u636e\u901a\u5e38\u5305\u542b\u7f3a\u5931\u503c\u3001\u91cd\u590d\u503c\u3001\u5f02\u5e38\u503c\u7b49\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u4f1a\u5f71\u54cd\u540e\u7eed\u7684\u5206\u6790\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u4e4b\u524d\uff0c\u5fc5\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u9884\u5904\u7406\u3002\u6570\u636e\u6e05\u6d17\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5904\u7406\u7f3a\u5931\u503c\u3001\u53bb\u9664\u91cd\u590d\u503c\u3001\u5904\u7406\u5f02\u5e38\u503c\u3001\u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316\u6570\u636e\u3002\u901a\u8fc7\u6570\u636e\u6e05\u6d17\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u8d28\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u5206\u6790\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u76f8\u5173\u5e93<\/h3>\n<\/p>\n<p><p>Python \u6709\u8bb8\u591a\u7528\u4e8e\u7edf\u8ba1\u5206\u6790\u7684\u5e93\uff0c\u4f8b\u5982 pandas\u3001numpy\u3001scipy \u548c statsmodels \u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u7edf\u8ba1\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><h4>1.1 Pandas<\/h4>\n<\/p>\n<p><p>Pandas \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.2 Numpy<\/h4>\n<\/p>\n<p><p>Numpy \u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u5404\u79cd\u6570\u5b66\u51fd\u6570\u3002\u5b83\u662f\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><h4>1.3 Scipy<\/h4>\n<\/p>\n<p><p>Scipy \u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u548c\u5de5\u7a0b\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u6570\u5b66\u51fd\u6570\u548c\u7edf\u8ba1\u5de5\u5177\u3002\u5b83\u5efa\u7acb\u5728 Numpy \u4e4b\u4e0a\uff0c\u6269\u5c55\u4e86 Numpy \u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1.4 Statsmodels<\/h4>\n<\/p>\n<p><p>Statsmodels \u662f\u4e00\u4e2a\u7528\u4e8e\u7edf\u8ba1\u5efa\u6a21\u548c\u6570\u636e\u5206\u6790\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7edf\u8ba1\u6a21\u578b\u548c\u6d4b\u8bd5\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u56de\u5f52\u5206\u6790\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u51c6\u5907\u548c\u6e05\u6d17\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u7684\u57fa\u7840\u3002\u539f\u59cb\u6570\u636e\u901a\u5e38\u5305\u542b\u7f3a\u5931\u503c\u3001\u91cd\u590d\u503c\u3001\u5f02\u5e38\u503c\u7b49\u95ee\u9898\uff0c\u8fd9\u4e9b\u95ee\u9898\u4f1a\u5f71\u54cd\u540e\u7eed\u7684\u5206\u6790\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u4e4b\u524d\uff0c\u5fc5\u987b\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u9884\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u7f3a\u5931\u503c\u662f\u6570\u636e\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5904\u7406\u7f3a\u5931\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u5220\u9664\u542b\u6709\u7f3a\u5931\u503c\u7684\u8bb0\u5f55\u3001\u7528\u5747\u503c\u6216\u4e2d\u4f4d\u6570\u586b\u8865\u7f3a\u5931\u503c\u3001\u63d2\u503c\u6cd5\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, None, 4, 5],<\/p>\n<p>        &#39;B&#39;: [None, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5220\u9664\u542b\u6709\u7f3a\u5931\u503c\u7684\u8bb0\u5f55<\/strong><\/h2>\n<p>df.dropna(inplace=True)<\/p>\n<h2><strong>\u7528\u5747\u503c\u586b\u8865\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df[&#39;A&#39;].fillna(df[&#39;A&#39;].mean(), inplace=True)<\/p>\n<p>df[&#39;B&#39;].fillna(df[&#39;B&#39;].mean(), inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u53bb\u9664\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u91cd\u590d\u503c\u4f1a\u5f71\u54cd\u6570\u636e\u5206\u6790\u7684\u7ed3\u679c\uff0c\u56e0\u6b64\u9700\u8981\u53bb\u9664\u6570\u636e\u4e2d\u7684\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>data = {&#39;A&#39;: [1, 2, 2, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 2, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u53bb\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df.drop_duplicates(inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u5904\u7406\u5f02\u5e38\u503c<\/h4>\n<\/p>\n<p><p>\u5f02\u5e38\u503c\u662f\u6307\u6570\u636e\u4e2d\u504f\u79bb\u6b63\u5e38\u8303\u56f4\u7684\u503c\uff0c\u5904\u7406\u5f02\u5e38\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u5220\u9664\u5f02\u5e38\u503c\u3001\u7528\u6b63\u5e38\u503c\u66ff\u4ee3\u5f02\u5e38\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 100, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 500]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97Z\u5206\u6570<\/strong><\/h2>\n<p>df_zscore = (df - df.mean()) \/ df.std()<\/p>\n<h2><strong>\u5220\u9664\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>df_cleaned = df[(np.abs(df_zscore) &lt; 3).all(axis=1)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.4 \u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316\u662f\u6570\u636e\u9884\u5904\u7406\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u6570\u636e\u5177\u6709\u76f8\u540c\u7684\u5c3a\u5ea6\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler, MinMaxScaler<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6807\u51c6\u5316\u6570\u636e<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>df_standardized = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)<\/p>\n<h2><strong>\u5f52\u4e00\u5316\u6570\u636e<\/strong><\/h2>\n<p>scaler = MinMaxScaler()<\/p>\n<p>df_normalized = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u63cf\u8ff0\u6027\u7edf\u8ba1\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u63cf\u8ff0\u6027\u7edf\u8ba1\u5206\u6790\u662f\u5bf9\u6570\u636e\u8fdb\u884c\u603b\u7ed3\u548c\u63cf\u8ff0\u7684\u8fc7\u7a0b\uff0c\u5e38\u7528\u7684\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf\u5305\u62ec\u5747\u503c\u3001\u4e2d\u4f4d\u6570\u3001\u4f17\u6570\u3001\u65b9\u5dee\u3001\u6807\u51c6\u5dee\u7b49\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5747\u503c\u3001\u4e2d\u4f4d\u6570\u548c\u4f17\u6570<\/h4>\n<\/p>\n<p><p>\u5747\u503c\u662f\u6570\u636e\u7684\u5e73\u5747\u503c\uff0c\u4e2d\u4f4d\u6570\u662f\u6570\u636e\u7684\u4e2d\u95f4\u503c\uff0c\u4f17\u6570\u662f\u6570\u636e\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5747\u503c<\/strong><\/h2>\n<p>mean_A = df[&#39;A&#39;].mean()<\/p>\n<p>mean_B = df[&#39;B&#39;].mean()<\/p>\n<h2><strong>\u8ba1\u7b97\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>median_A = df[&#39;A&#39;].median()<\/p>\n<p>median_B = df[&#39;B&#39;].median()<\/p>\n<h2><strong>\u8ba1\u7b97\u4f17\u6570<\/strong><\/h2>\n<p>mode_A = df[&#39;A&#39;].mode()[0]<\/p>\n<p>mode_B = df[&#39;B&#39;].mode()[0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u65b9\u5dee\u548c\u6807\u51c6\u5dee<\/h4>\n<\/p>\n<p><p>\u65b9\u5dee\u662f\u6570\u636e\u7684\u79bb\u6563\u7a0b\u5ea6\uff0c\u6807\u51c6\u5dee\u662f\u65b9\u5dee\u7684\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u65b9\u5dee<\/p>\n<p>var_A = df[&#39;A&#39;].var()<\/p>\n<p>var_B = df[&#39;B&#39;].var()<\/p>\n<h2><strong>\u8ba1\u7b97\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_A = df[&#39;A&#39;].std()<\/p>\n<p>std_B = df[&#39;B&#39;].std()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u53ef\u89c6\u5316\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u53ef\u89c6\u5316\u662f\u6570\u636e\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u76f4\u89c2\u5730\u4e86\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\u3002Python \u6709\u8bb8\u591a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\uff0c\u4f8b\u5982 matplotlib\u3001seaborn\u3001plotly \u7b49\u3002<\/p>\n<\/p>\n<p><h4>4.1 Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521b\u5efa\u5404\u79cd\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(df[&#39;A&#39;], label=&#39;A&#39;)<\/p>\n<p>plt.plot(df[&#39;B&#39;], label=&#39;B&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 Seaborn<\/h4>\n<\/p>\n<p><p>Seaborn \u662f\u4e00\u4e2a\u57fa\u4e8e Matplotlib \u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u548c\u7f8e\u89c2\u7684\u7ed8\u56fe\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>sns.boxplot(data=df)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 Plotly<\/h4>\n<\/p>\n<p><p>Plotly \u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>fig = px.scatter(df, x=&#39;A&#39;, y=&#39;B&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u63a8\u65ad\u6027\u7edf\u8ba1\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u63a8\u65ad\u6027\u7edf\u8ba1\u5206\u6790\u662f\u901a\u8fc7\u6837\u672c\u6570\u636e\u63a8\u65ad\u603b\u4f53\u7279\u5f81\u7684\u8fc7\u7a0b\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u5047\u8bbe\u68c0\u9a8c\u3001\u56de\u5f52\u5206\u6790\u7b49\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5047\u8bbe\u68c0\u9a8c<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u68c0\u9a8c\u662f\u901a\u8fc7\u6837\u672c\u6570\u636e\u68c0\u9a8c\u5047\u8bbe\u662f\u5426\u6210\u7acb\u7684\u8fc7\u7a0b\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec t \u68c0\u9a8c\u3001\u5361\u65b9\u68c0\u9a8c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import ttest_ind, chi2_contingency<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data1 = [1, 2, 3, 4, 5]<\/p>\n<p>data2 = [2, 3, 4, 5, 6]<\/p>\n<h2><strong>t \u68c0\u9a8c<\/strong><\/h2>\n<p>t_stat, p_value = ttest_ind(data1, data2)<\/p>\n<h2><strong>\u5361\u65b9\u68c0\u9a8c<\/strong><\/h2>\n<p>data = [[10, 20], [20, 30]]<\/p>\n<p>chi2_stat, p_value, dof, expected = chi2_contingency(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u56de\u5f52\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u56de\u5f52\u5206\u6790\u662f\u7814\u7a76\u56e0\u53d8\u91cf\u548c\u81ea\u53d8\u91cf\u4e4b\u95f4\u5173\u7cfb\u7684\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u7ebf\u6027\u56de\u5f52\u3001\u903b\u8f91\u56de\u5f52\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statsmodels.api as sm<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ebf\u6027\u56de\u5f52<\/strong><\/h2>\n<p>X = df[&#39;A&#39;]<\/p>\n<p>y = df[&#39;B&#39;]<\/p>\n<p>X = sm.add_constant(X)<\/p>\n<p>model = sm.OLS(y, X).fit()<\/p>\n<p>results = model.summary()<\/p>\n<p>print(results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u89e3\u91ca\u548c\u62a5\u544a\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u89e3\u91ca\u548c\u62a5\u544a\u7ed3\u679c\u662f\u6570\u636e\u5206\u6790\u7684\u6700\u540e\u4e00\u6b65\uff0c\u901a\u8fc7\u89e3\u91ca\u5206\u6790\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u7ed3\u8bba\uff0c\u5e76\u63d0\u51fa\u76f8\u5e94\u7684\u5efa\u8bae\u3002\u62a5\u544a\u7ed3\u679c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u8868\u548c\u8868\u683c\u6765\u5c55\u793a\u5206\u6790\u7ed3\u679c\uff0c\u4ee5\u4fbf\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u89e3\u91ca\u5206\u6790\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u89e3\u91ca\u5206\u6790\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u7ed3\u5408\u5b9e\u9645\u60c5\u51b5\uff0c\u5bf9\u5206\u6790\u7ed3\u679c\u8fdb\u884c\u5408\u7406\u7684\u89e3\u91ca\u3002\u4f8b\u5982\uff0c\u56de\u5f52\u5206\u6790\u7684\u7ed3\u679c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u81ea\u53d8\u91cf\u548c\u56e0\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4ece\u800c\u63d0\u51fa\u76f8\u5e94\u7684\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u62a5\u544a\u5206\u6790\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u62a5\u544a\u5206\u6790\u7ed3\u679c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u8868\u548c\u8868\u683c\u6765\u5c55\u793a\u5206\u6790\u7ed3\u679c\uff0c\u4ee5\u4fbf\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u56fe\u8868\u6765\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\u3002\u53ef\u4ee5\u4f7f\u7528\u8868\u683c\u6765\u5c55\u793a\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf\u3001\u56de\u5f52\u5206\u6790\u7ed3\u679c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(df[&#39;A&#39;], label=&#39;A&#39;)<\/p>\n<p>plt.plot(df[&#39;B&#39;], label=&#39;B&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u8868\u683c<\/strong><\/h2>\n<p>summary_table = pd.DataFrame({&#39;Statistic&#39;: [&#39;Mean&#39;, &#39;Median&#39;, &#39;Mode&#39;, &#39;Variance&#39;, &#39;Standard Deviation&#39;],<\/p>\n<p>                              &#39;A&#39;: [df[&#39;A&#39;].mean(), df[&#39;A&#39;].median(), df[&#39;A&#39;].mode()[0], df[&#39;A&#39;].var(), df[&#39;A&#39;].std()],<\/p>\n<p>                              &#39;B&#39;: [df[&#39;B&#39;].mean(), df[&#39;B&#39;].median(), df[&#39;B&#39;].mode()[0], df[&#39;B&#39;].var(), df[&#39;B&#39;].std()]})<\/p>\n<p>print(summary_table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython \u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u7edf\u8ba1\u5206\u6790\u4e2d\u3002\u901a\u8fc7\u5bfc\u5165\u76f8\u5173\u5e93\u3001\u51c6\u5907\u548c\u6e05\u6d17\u6570\u636e\u3001\u8fdb\u884c\u63cf\u8ff0\u6027\u7edf\u8ba1\u5206\u6790\u3001\u53ef\u89c6\u5316\u6570\u636e\u3001\u8fdb\u884c\u63a8\u65ad\u6027\u7edf\u8ba1\u5206\u6790\u3001\u89e3\u91ca\u548c\u62a5\u544a\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Python \u8fdb\u884c\u9ad8\u6548\u7684\u7edf\u8ba1\u5206\u6790\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528 Python \u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4f7f\u7528Python\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u65f6\uff0c\u9996\u5148\u9700\u8981\u660e\u786e\u5206\u6790\u7684\u76ee\u6807\u548c\u6570\u636e\u6765\u6e90\u3002\u901a\u5e38\uff0c\u6570\u636e\u53ef\u4ee5\u6765\u81eaCSV\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216API\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528Python\u7684Pandas\u5e93\u52a0\u8f7d\u548c\u5904\u7406\u6570\u636e\uff0c\u901a\u8fc7\u6570\u636e\u6e05\u6d17\u3001\u7b5b\u9009\u548c\u8f6c\u6362\u7b49\u6b65\u9aa4\uff0c\u4e3a\u540e\u7eed\u5206\u6790\u505a\u597d\u51c6\u5907\u3002\u4e4b\u540e\uff0c\u53ef\u4ee5\u5229\u7528NumPy\u548cSciPy\u5e93\u8fdb\u884c\u63cf\u8ff0\u6027\u7edf\u8ba1\u5206\u6790\uff0c\u4f8b\u5982\u8ba1\u7b97\u5747\u503c\u3001\u65b9\u5dee\u7b49\u6307\u6807\u3002\u53ef\u89c6\u5316\u5de5\u5177\u5982Matplotlib\u548cSeaborn\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u5206\u5e03\u548c\u8d8b\u52bf\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u7edf\u8ba1\u5206\u6790\u5e93\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u7684\u7edf\u8ba1\u5206\u6790\u5e93\u3002Pandas\u662f\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u57fa\u7840\u5e93\uff0cNumPy\u5219\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002SciPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u79d1\u5b66\u8ba1\u7b97\u548c\u7edf\u8ba1\u5206\u5e03\u7684\u529f\u80fd\uff0cStatsmodels\u5219\u4e13\u6ce8\u4e8e\u7edf\u8ba1\u5efa\u6a21\u548c\u8ba1\u91cf\u7ecf\u6d4e\u5b66\u5206\u6790\u3002\u6b64\u5916\uff0cMatplotlib\u548cSeaborn\u5728\u6570\u636e\u53ef\u89c6\u5316\u65b9\u9762\u8868\u73b0\u4f18\u5f02\uff0c\u80fd\u591f\u521b\u5efa\u5404\u79cd\u56fe\u8868\u6765\u5c55\u793a\u5206\u6790\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u7edf\u8ba1\u5206\u6790\u65b9\u6cd5\uff1f<\/strong><br \/>\u9009\u62e9\u7edf\u8ba1\u5206\u6790\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u7c7b\u578b\u548c\u7814\u7a76\u95ee\u9898\u7684\u6027\u8d28\u3002\u5982\u679c\u6570\u636e\u662f\u8fde\u7eed\u578b\u7684\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528t\u68c0\u9a8c\u6216\u65b9\u5dee\u5206\u6790\u7b49\u65b9\u6cd5\uff1b\u5982\u679c\u6570\u636e\u662f\u5206\u7c7b\u7684\uff0c\u5361\u65b9\u68c0\u9a8c\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002\u6b64\u5916\uff0c\u8fd8\u8981\u8003\u8651\u6837\u672c\u7684\u5927\u5c0f\u548c\u5206\u5e03\u7279\u5f81\u3002\u901a\u5e38\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fdb\u884c\u63a2\u7d22\u6027\u6570\u636e\u5206\u6790\uff08EDA\uff09\u6765\u5e2e\u52a9\u9009\u62e9\u5408\u9002\u7684\u7edf\u8ba1\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u5206\u6790\u7ed3\u679c\u7684\u6709\u6548\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u505a\u7edf\u8ba1\u5206\u6790 Python \u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u7edf\u8ba1\u5206\u6790\u4e2d\u3002\u9996\u5148\u9700 [&hellip;]","protected":false},"author":3,"featured_media":1089025,"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\/1089016"}],"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=1089016"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089016\/revisions"}],"predecessor-version":[{"id":1089026,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089016\/revisions\/1089026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089025"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}