{"id":1071853,"date":"2025-01-08T11:11:00","date_gmt":"2025-01-08T03:11:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071853.html"},"modified":"2025-01-08T11:11:03","modified_gmt":"2025-01-08T03:11:03","slug":"python%e5%a6%82%e4%bd%95%e5%ba%94%e7%94%a8%e4%ba%8e%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071853.html","title":{"rendered":"python\u5982\u4f55\u5e94\u7528\u4e8e\u6570\u636e\u5206\u6790"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102217\/04075bfa-147d-44b6-9952-b6cbcaf2fe2a.webp\" alt=\"python\u5982\u4f55\u5e94\u7528\u4e8e\u6570\u636e\u5206\u6790\" \/><\/p>\n<p><p> <strong>Python\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528\u4e3b\u8981\u4f53\u73b0\u5728\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u7edf\u8ba1\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u65b9\u9762\u3002\u7279\u522b\u662fPython\u7684\u5e93\u5982Pandas\u3001NumPy\u3001Matplotlib\u3001Scikit-learn\u7b49\uff0c\u4f7f\u5f97\u6570\u636e\u5206\u6790\u53d8\u5f97\u66f4\u4e3a\u4fbf\u6377\u9ad8\u6548\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u5728\u5404\u4e2a\u6570\u636e\u5206\u6790\u73af\u8282\u7684\u5b9e\u9645\u5e94\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6570\u636e\u6e05\u6d17<\/p>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u5f88\u591a\u6570\u636e\u96c6\u5f80\u5f80\u5305\u542b\u7f3a\u5931\u503c\u3001\u91cd\u590d\u503c\u3001\u5f02\u5e38\u503c\u7b49\u95ee\u9898\u3002Python\u7684Pandas\u5e93\u5728\u6570\u636e\u6e05\u6d17\u4e2d\u53d1\u6325\u4e86\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u636e\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3002\u6bd4\u5982\uff0c\u5904\u7406\u7f3a\u5931\u503c\u53ef\u4ee5\u4f7f\u7528<code>dropna()<\/code>\u51fd\u6570\u5220\u9664\u7f3a\u5931\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528<code>fillna()<\/code>\u51fd\u6570\u586b\u5145\u7f3a\u5931\u6570\u636e\u3002\u5bf9\u4e8e\u91cd\u590d\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>drop_duplicates()<\/code>\u51fd\u6570\u5220\u9664\u91cd\u590d\u6570\u636e\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;Name&#39;: [&#39;Tom&#39;, &#39;Nick&#39;, &#39;Chris&#39;, &#39;Tom&#39;],<\/p>\n<p>        &#39;Age&#39;: [20, 21, None, 20]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5220\u9664\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df_cleaned = df.dropna()<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df_filled = df.fillna(0)<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df_unique = df.drop_duplicates()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5904\u7406\u7f3a\u5931\u503c\u662f\u6570\u636e\u6e05\u6d17\u7684\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\uff0c\u7f3a\u5931\u503c\u53ef\u80fd\u4f1a\u5bf9\u5206\u6790\u7ed3\u679c\u4ea7\u751f\u8bef\u5bfc\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>dropna()<\/code>\u51fd\u6570\u76f4\u63a5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\uff0c\u6216\u8005\u4f7f\u7528<code>fillna()<\/code>\u51fd\u6570\u5bf9\u7f3a\u5931\u503c\u8fdb\u884c\u586b\u5145\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6570\u636e\u53ef\u89c6\u5316<\/p>\n<\/p>\n<p><p>\u6570\u636e\u53ef\u89c6\u5316\u662f\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u73af\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u3002Python\u7684Matplotlib\u548cSeaborn\u5e93\u662f\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u51e0\u4e4e\u53ef\u4ee5\u7ed8\u5236\u6240\u6709\u7c7b\u578b\u7684\u56fe\u8868\u3002Seaborn\u662f\u5728Matplotlib\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u9ad8\u7ea7\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u7684API\u548c\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Nick&#39;, &#39;Chris&#39;, &#39;Tom&#39;],<\/p>\n<p>        &#39;Age&#39;: [20, 21, 19, 20]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>Matplotlib \u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>plt.bar(df[&#39;Name&#39;], df[&#39;Age&#39;])<\/p>\n<p>plt.xlabel(&#39;Name&#39;)<\/p>\n<p>plt.ylabel(&#39;Age&#39;)<\/p>\n<p>plt.title(&#39;Age of individuals&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>Seaborn \u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>sns.boxplot(x=df[&#39;Age&#39;])<\/p>\n<p>plt.title(&#39;Age Distribution&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u6570\u636e\u53ef\u89c6\u5316\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\uff0c\u8fd8\u80fd\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\u548c\u6f5c\u5728\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Matplotlib\u7ed8\u5236\u67f1\u72b6\u56fe\u53ef\u4ee5\u76f4\u89c2\u5730\u6bd4\u8f83\u4e0d\u540c\u7c7b\u522b\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u800c\u4f7f\u7528Seaborn\u7ed8\u5236\u7bb1\u7ebf\u56fe\u5219\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u53ca\u5f02\u5e38\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7edf\u8ba1\u5206\u6790<\/p>\n<\/p>\n<p><p>\u7edf\u8ba1\u5206\u6790\u662f\u6570\u636e\u5206\u6790\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0cPython\u7684SciPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7edf\u8ba1\u5206\u6790\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>SciPy\u5e93\u4e2d\u5305\u542b\u4e86\u5927\u91cf\u7684\u7edf\u8ba1\u51fd\u6570\uff0c\u6bd4\u5982\u63cf\u8ff0\u7edf\u8ba1\u3001\u5047\u8bbe\u68c0\u9a8c\u3001\u76f8\u5173\u5206\u6790\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u6df1\u5165\u7684\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import stats<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [20, 21, 19, 20, 21, 22, 20]<\/p>\n<h2><strong>\u63cf\u8ff0\u7edf\u8ba1<\/strong><\/h2>\n<p>mean = stats.tmean(data)<\/p>\n<p>median = stats.scoreatpercentile(data, 50)<\/p>\n<p>std_dev = stats.tstd(data)<\/p>\n<h2><strong>\u5047\u8bbe\u68c0\u9a8c<\/strong><\/h2>\n<p>t_stat, p_value = stats.ttest_1samp(data, 20)<\/p>\n<p>print(f&#39;Mean: {mean}, Median: {median}, Standard Deviation: {std_dev}&#39;)<\/p>\n<p>print(f&#39;T-statistic: {t_stat}, P-value: {p_value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u7edf\u8ba1\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6570\u636e\u7684\u7279\u5f81\u548c\u5206\u5e03\u60c5\u51b5\uff0c\u8fdb\u884c\u5047\u8bbe\u68c0\u9a8c\u548c\u76f8\u5173\u5206\u6790\u3002\u4f8b\u5982\uff0c\u901a\u8fc7SciPy\u5e93\u7684\u63cf\u8ff0\u7edf\u8ba1\u51fd\u6570\u53ef\u4ee5\u8ba1\u7b97\u6570\u636e\u7684\u5747\u503c\u3001\u4e2d\u4f4d\u6570\u548c\u6807\u51c6\u5dee\uff0c\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6570\u636e\u7684\u96c6\u4e2d\u8d8b\u52bf\u548c\u79bb\u6563\u7a0b\u5ea6\u3002\u800c\u901a\u8fc7\u5047\u8bbe\u68c0\u9a8c\u51fd\u6570\u53ef\u4ee5\u68c0\u9a8c\u6570\u636e\u662f\u5426\u7b26\u5408\u67d0\u4e00\u5047\u8bbe\uff0c\u8fdb\u800c\u6307\u5bfc\u6211\u4eec\u7684\u51b3\u7b56\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u673a\u5668\u5b66\u4e60<\/p>\n<\/p>\n<p><p>\u673a\u5668\u5b66\u4e60\u662f\u6570\u636e\u5206\u6790\u7684\u9ad8\u7ea7\u9636\u6bb5\uff0c\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u5efa\u6a21\uff0c\u53ef\u4ee5\u9884\u6d4b\u672a\u6765\u6570\u636e\u6216\u8005\u5206\u7c7b\u65b0\u6570\u636e\u3002Python\u7684Scikit-learn\u5e93\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u673a\u5668\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><p>Scikit-learn\u5e93\u5305\u542b\u4e86\u5e38\u89c1\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u6bd4\u5982\u7ebf\u6027\u56de\u5f52\u3001\u51b3\u7b56\u6811\u3001\u652f\u6301\u5411\u91cf\u673a\u3001\u968f\u673a\u68ee\u6797\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7c7b\u3001\u56de\u5f52\u3001\u805a\u7c7b\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.metrics import mean_squared_error<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Age&#39;: [20, 21, 19, 20, 21, 22, 20],<\/p>\n<p>        &#39;Salary&#39;: [2000, 2100, 1900, 2000, 2100, 2200, 2000]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf<\/strong><\/h2>\n<p>X = df[[&#39;Age&#39;]]<\/p>\n<p>y = df[&#39;Salary&#39;]<\/p>\n<h2><strong>\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)<\/p>\n<h2><strong>\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = model.predict(X_test)<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b<\/strong><\/h2>\n<p>mse = mean_squared_error(y_test, y_pred)<\/p>\n<p>print(f&#39;Mean Squared Error: {mse}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u673a\u5668\u5b66\u4e60\u901a\u8fc7\u5bf9\u5386\u53f2\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\uff0c\u53ef\u4ee5\u9884\u6d4b\u672a\u6765\u6570\u636e\u6216\u8005\u5206\u7c7b\u65b0\u6570\u636e\u3002\u4ee5\u7ebf\u6027\u56de\u5f52\u4e3a\u4f8b\uff0c\u901a\u8fc7Scikit-learn\u5e93\u7684\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u8f93\u5165\u7684\u7279\u5f81\u53d8\u91cf\u9884\u6d4b\u76ee\u6807\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u5747\u65b9\u8bef\u5dee\u7b49\u6307\u6807\u8bc4\u4f30\u6a21\u578b\u7684\u9884\u6d4b\u6548\u679c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790<\/p>\n<\/p>\n<p><p>\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u662f\u6570\u636e\u5206\u6790\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u9886\u57df\uff0c\u7279\u522b\u662f\u5728\u91d1\u878d\u6570\u636e\u548c\u4f20\u611f\u5668\u6570\u636e\u4e2d\u3002Python\u7684Statsmodels\u548cFacebook\u7684Prophet\u5e93\u662f\u8fdb\u884c\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>Statsmodels\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u51fd\u6570\uff0c\u6bd4\u5982\u81ea\u56de\u5f52\u6a21\u578b\uff08AR\uff09\u3001\u5dee\u5206\u79fb\u52a8\u5e73\u5747\u6a21\u578b\uff08MA\uff09\u3001\u81ea\u56de\u5f52\u79ef\u5206\u79fb\u52a8\u5e73\u5747\u6a21\u578b\uff08ARIMA\uff09\u7b49\u3002Facebook\u7684Prophet\u5e93\u5219\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5177\u6709\u660e\u663e\u5b63\u8282\u6027\u548c\u5047\u65e5\u6548\u5e94\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statsmodels.api as sm<\/p>\n<p>from fbprophet import Prophet<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Date&#39;: pd.date_range(start=&#39;1\/1\/2020&#39;, periods=7),<\/p>\n<p>        &#39;Value&#39;: [100, 110, 105, 115, 120, 125, 130]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.set_index(&#39;Date&#39;, inplace=True)<\/p>\n<h2><strong>ARIMA \u6a21\u578b<\/strong><\/h2>\n<p>model = sm.tsa.ARIMA(df[&#39;Value&#39;], order=(1, 1, 1))<\/p>\n<p>result = model.fit()<\/p>\n<p>print(result.summary())<\/p>\n<h2><strong>Prophet \u6a21\u578b<\/strong><\/h2>\n<p>df.reset_index(inplace=True)<\/p>\n<p>df.columns = [&#39;ds&#39;, &#39;y&#39;]<\/p>\n<p>model = Prophet()<\/p>\n<p>model.fit(df)<\/p>\n<p>future = model.make_future_dataframe(periods=3)<\/p>\n<p>forecast = model.predict(future)<\/p>\n<p>print(forecast[[&#39;ds&#39;, &#39;yhat&#39;, &#39;yhat_lower&#39;, &#39;yhat_upper&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6570\u636e\u7684\u65f6\u95f4\u4f9d\u8d56\u5173\u7cfb\uff0c\u5e76\u8fdb\u884c\u672a\u6765\u6570\u636e\u7684\u9884\u6d4b\u3002\u4f8b\u5982\uff0c\u901a\u8fc7Statsmodels\u5e93\u7684ARIMA\u6a21\u578b\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u81ea\u56de\u5f52\u548c\u79fb\u52a8\u5e73\u5747\u5efa\u6a21\uff0c\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u8d8b\u52bf\u548c\u5b63\u8282\u6027\u3002\u800c\u901a\u8fc7Prophet\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5177\u6709\u5b63\u8282\u6027\u548c\u5047\u65e5\u6548\u5e94\u7684\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001\u6587\u672c\u5206\u6790<\/p>\n<\/p>\n<p><p>\u6587\u672c\u5206\u6790\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u9886\u57df\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u975e\u7ed3\u6784\u5316\u7684\u6587\u672c\u6570\u636e\u3002Python\u7684NLTK\u548cSpaCy\u5e93\u662f\u8fdb\u884c\u6587\u672c\u5206\u6790\u7684\u4e3b\u8981\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>NLTK\uff08Natural Language Toolkit\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6587\u672c\u5904\u7406\u51fd\u6570\uff0c\u6bd4\u5982\u5206\u8bcd\u3001\u8bcd\u6027\u6807\u6ce8\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u7b49\u3002SpaCy\u5219\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6587\u672c\u6570\u636e\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.corpus import stopwords<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<p>import spacy<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Python is an amazing programming language for data analysis.&quot;<\/p>\n<h2><strong>NLTK \u5206\u8bcd\u548c\u53bb\u9664\u505c\u7528\u8bcd<\/strong><\/h2>\n<p>stop_words = set(stopwords.words(&#39;english&#39;))<\/p>\n<p>word_tokens = word_tokenize(text)<\/p>\n<p>filtered_text = [w for w in word_tokens if not w.lower() in stop_words]<\/p>\n<h2><strong>SpaCy \u547d\u540d\u5b9e\u4f53\u8bc6\u522b<\/strong><\/h2>\n<p>nlp = spacy.load(&quot;en_core_web_sm&quot;)<\/p>\n<p>doc = nlp(text)<\/p>\n<p>entities = [(entity.text, entity.label_) for entity in doc.ents]<\/p>\n<p>print(f&#39;Filtered Text: {filtered_text}&#39;)<\/p>\n<p>print(f&#39;Entities: {entities}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u6587\u672c\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u975e\u7ed3\u6784\u5316\u7684\u6587\u672c\u6570\u636e\u4e2d\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u901a\u8fc7NLTK\u5e93\u53ef\u4ee5\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\u548c\u53bb\u9664\u505c\u7528\u8bcd\uff0c\u6e05\u6d17\u6587\u672c\u6570\u636e\u3002\u800c\u901a\u8fc7SpaCy\u5e93\u53ef\u4ee5\u8fdb\u884c\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\uff0c\u4ece\u6587\u672c\u4e2d\u8bc6\u522b\u51fa\u4eba\u540d\u3001\u5730\u540d\u3001\u7ec4\u7ec7\u540d\u7b49\u5b9e\u4f53\u4fe1\u606f\uff0c\u4e3a\u540e\u7eed\u5206\u6790\u63d0\u4f9b\u652f\u6301\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5927\u6570\u636e\u5904\u7406<\/p>\n<\/p>\n<p><p>\u968f\u7740\u6570\u636e\u91cf\u7684\u4e0d\u65ad\u589e\u5927\uff0c\u4f20\u7edf\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\u5df2\u7ecf\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u3002Python\u7684Dask\u548cPySpark\u5e93\u662f\u8fdb\u884c\u5927\u6570\u636e\u5904\u7406\u7684\u4e3b\u8981\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>Dask\u662f\u4e00\u4e2a\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u5e76\u884c\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002PySpark\u662fApache Spark\u7684Python\u63a5\u53e3\uff0c\u53ef\u4ee5\u5728\u5206\u5e03\u5f0f\u8ba1\u7b97\u73af\u5883\u4e2d\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.dataframe as dd<\/p>\n<p>from pyspark.sql import SparkSession<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Nick&#39;, &#39;Chris&#39;, &#39;Tom&#39;],<\/p>\n<p>        &#39;Age&#39;: [20, 21, 19, 20]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>Dask \u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>ddf = dd.from_pandas(df, npartitions=2)<\/p>\n<p>result = ddf.groupby(&#39;Name&#39;).mean().compute()<\/p>\n<h2><strong>PySpark \u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>spark = SparkSession.builder.appName(&quot;example&quot;).getOrCreate()<\/p>\n<p>sdf = spark.createDataFrame(df)<\/p>\n<p>result = sdf.groupBy(&#39;Name&#39;).mean().collect()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5927\u6570\u636e\u5904\u7406\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u548c\u5206\u6790\u5927\u89c4\u6a21\u6570\u636e\uff0c\u63d0\u5347\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u901a\u8fc7Dask\u5e93\u53ef\u4ee5\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u5e76\u884c\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u800c\u901a\u8fc7PySpark\u5e93\u53ef\u4ee5\u5728\u5206\u5e03\u5f0f\u8ba1\u7b97\u73af\u5883\u4e2d\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u5145\u5206\u5229\u7528\u96c6\u7fa4\u8d44\u6e90\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516b\u3001\u5730\u7406\u6570\u636e\u5206\u6790<\/p>\n<\/p>\n<p><p>\u5730\u7406\u6570\u636e\u5206\u6790\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u9886\u57df\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u5177\u6709\u5730\u7406\u4f4d\u7f6e\u7684\u6570\u636e\u3002Python\u7684Geopandas\u548cFolium\u5e93\u662f\u8fdb\u884c\u5730\u7406\u6570\u636e\u5206\u6790\u7684\u4e3b\u8981\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>Geopandas\u662f\u4e00\u4e2a\u5730\u7406\u6570\u636e\u5904\u7406\u5e93\uff0c\u57fa\u4e8ePandas\u548cShapely\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5730\u7406\u6570\u636e\u7684\u8bfb\u53d6\u3001\u64cd\u4f5c\u548c\u5206\u6790\u3002Folium\u662f\u4e00\u4e2a\u5730\u56fe\u53ef\u89c6\u5316\u5e93\uff0c\u53ef\u4ee5\u5728Jupyter Notebook\u4e2d\u663e\u793a\u4ea4\u4e92\u5f0f\u5730\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import geopandas as gpd<\/p>\n<p>import folium<\/p>\n<h2><strong>\u793a\u4f8b\u5730\u7406\u6570\u636e<\/strong><\/h2>\n<p>world = gpd.read_file(gpd.datasets.get_path(&#39;naturalearth_lowres&#39;))<\/p>\n<h2><strong>Geopandas \u8bfb\u53d6\u548c\u64cd\u4f5c\u5730\u7406\u6570\u636e<\/strong><\/h2>\n<p>africa = world[world[&#39;continent&#39;] == &#39;Africa&#39;]<\/p>\n<h2><strong>Folium \u663e\u793a\u4ea4\u4e92\u5f0f\u5730\u56fe<\/strong><\/h2>\n<p>m = folium.Map(location=[0, 0], zoom_start=2)<\/p>\n<p>folium.Choropleth(<\/p>\n<p>    geo_data=africa,<\/p>\n<p>    name=&#39;choropleth&#39;,<\/p>\n<p>    data=africa,<\/p>\n<p>    columns=[&#39;iso_a3&#39;, &#39;gdp_md_est&#39;],<\/p>\n<p>    key_on=&#39;feature.id&#39;,<\/p>\n<p>    fill_color=&#39;YlGn&#39;,<\/p>\n<p>    fill_opacity=0.7,<\/p>\n<p>    line_opacity=0.2,<\/p>\n<p>    legend_name=&#39;GDP (Million USD)&#39;<\/p>\n<p>).add_to(m)<\/p>\n<p>folium.LayerControl().add_to(m)<\/p>\n<p>m<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u5730\u7406\u6570\u636e\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u548c\u5206\u6790\u5177\u6709\u5730\u7406\u4f4d\u7f6e\u7684\u6570\u636e\uff0c\u5c55\u793a\u6570\u636e\u7684\u5730\u7406\u5206\u5e03\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u901a\u8fc7Geopandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u64cd\u4f5c\u5730\u7406\u6570\u636e\uff0c\u8fdb\u884c\u5730\u7406\u5206\u6790\u3002\u800c\u901a\u8fc7Folium\u5e93\u53ef\u4ee5\u5728Jupyter Notebook\u4e2d\u663e\u793a\u4ea4\u4e92\u5f0f\u5730\u56fe\uff0c\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u7684\u5730\u7406\u5206\u5e03\u60c5\u51b5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u5728\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u7edf\u8ba1\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u3001\u6587\u672c\u5206\u6790\u3001\u5927\u6570\u636e\u5904\u7406\u548c\u5730\u7406\u6570\u636e\u5206\u6790\u4e2d\u7684\u5b9e\u9645\u5e94\u7528\u3002Python\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u51ed\u501f\u5176\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u6570\u636e\u5206\u6790\u7684\u6d41\u7a0b\uff0c\u63d0\u9ad8\u4e86\u6570\u636e\u5206\u6790\u7684\u6548\u7387\u548c\u6548\u679c\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u5728\u6570\u636e\u5206\u6790\u4e2d\u6709\u54ea\u4e9b\u4e3b\u8981\u5e93\uff1f<\/strong><br \/>Python\u62e5\u6709\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u80fd\u591f\u5e2e\u52a9\u6570\u636e\u5206\u6790\u5e08\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecPandas\uff0c\u5b83\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u6570\u636e\u6846\u67b6\u7684\u5904\u7406\uff1bNumPy\uff0c\u4e13\u6ce8\u4e8e\u6570\u503c\u8ba1\u7b97\u548c\u6570\u7ec4\u64cd\u4f5c\uff1bMatplotlib\u548cSeaborn\uff0c\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff1bSciPy\uff0c\u63d0\u4f9b\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\uff1b\u4ee5\u53caScikit-learn\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u7684\u5b9e\u73b0\u3002\u8fd9\u4e9b\u5e93\u7684\u7ec4\u5408\u4f7f\u5f97Python\u5728\u6570\u636e\u5206\u6790\u9886\u57df\u6781\u5177\u4f18\u52bf\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5904\u7406\u7f3a\u5931\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406\u7f3a\u5931\u6570\u636e\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u91cd\u8981\u73af\u8282\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u6570\u636e\uff0cPandas\u5e93\u4e2d\u7684<code>isnull()<\/code>\u548c<code>dropna()<\/code>\u51fd\u6570\u53ef\u7528\u4e8e\u8bc6\u522b\u548c\u5220\u9664\u7f3a\u5931\u6570\u636e\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>fillna()<\/code>\u51fd\u6570\u901a\u8fc7\u63d2\u8865\u3001\u5747\u503c\u586b\u5145\u6216\u5176\u4ed6\u65b9\u6cd5\u6765\u66ff\u4ee3\u7f3a\u5931\u503c\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u8fdb\u884c\u7f3a\u5931\u503c\u9884\u6d4b\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u7b56\u7565\u3002<\/p>\n<p><strong>Python\u5728\u6570\u636e\u53ef\u89c6\u5316\u4e2d\u80fd\u63d0\u4f9b\u54ea\u4e9b\u529f\u80fd\uff1f<\/strong><br \/>Python\u5177\u5907\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u80fd\u529b\uff0c\u80fd\u591f\u5e2e\u52a9\u5206\u6790\u5e08\u4ee5\u56fe\u5f62\u5316\u7684\u5f62\u5f0f\u5c55\u793a\u6570\u636e\u3002\u901a\u8fc7Matplotlib\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u548c\u6563\u70b9\u56fe\u3002Seaborn\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7f8e\u89c2\u7684\u7edf\u8ba1\u56fe\u8868\uff0c\u540c\u65f6\u652f\u6301\u590d\u6742\u7684\u53ef\u89c6\u5316\u3002\u4f7f\u7528Plotly\u5e93\u8fd8\u80fd\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u6df1\u5165\u5730\u7406\u89e3\u6570\u636e\u80cc\u540e\u7684\u4fe1\u606f\u3002\u8fd9\u4e9b\u5de5\u5177\u8ba9\u6570\u636e\u5206\u6790\u7ed3\u679c\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528\u4e3b\u8981\u4f53\u73b0\u5728\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u7edf\u8ba1\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u7b49\u65b9\u9762\u3002\u7279\u522b\u662fPython\u7684\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1071861,"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\/1071853"}],"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=1071853"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071853\/revisions"}],"predecessor-version":[{"id":1071864,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071853\/revisions\/1071864"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071861"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}