{"id":1177460,"date":"2025-01-15T17:58:21","date_gmt":"2025-01-15T09:58:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177460.html"},"modified":"2025-01-15T17:58:24","modified_gmt":"2025-01-15T09:58:24","slug":"%e5%a6%82%e4%bd%95python%e5%81%9a%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1177460.html","title":{"rendered":"\u5982\u4f55python\u505a\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\/25112342\/8e6ca5cb-ab85-457a-884f-f92b18b66147.webp\" alt=\"\u5982\u4f55python\u505a\u6570\u636e\u5206\u6790\" \/><\/p>\n<p><p> <strong>\u8981\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u3001NumPy\u3001Matplotlib\u3001Seaborn\u3001Scikit-learn\u7b49\u5e93\u6765\u5b8c\u6210\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u53ef\u89c6\u5316\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4efb\u52a1\u3002<\/strong> \u5176\u4e2d\uff0cPandas\u548cNumPy\u662f\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u7684\u57fa\u672c\u5de5\u5177\uff0cMatplotlib\u548cSeaborn\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0cScikit-learn\u5219\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u548c\u5de5\u5177\u3002<strong>\u6570\u636e\u6e05\u6d17<\/strong>\u662f\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u6570\u636e\u5f80\u5f80\u662f\u810f\u7684\uff0c\u6709\u7f3a\u5931\u503c\u3001\u91cd\u590d\u503c\u6216\u8005\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u6570\u636e\u3002\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684\u5404\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528dropna()\u65b9\u6cd5\u5220\u9664\u7f3a\u5931\u503c\uff0c\u4f7f\u7528fillna()\u65b9\u6cd5\u586b\u5145\u7f3a\u5931\u503c\uff0c\u4f7f\u7528duplicated()\u65b9\u6cd5\u67e5\u627e\u91cd\u590d\u503c\u5e76\u4f7f\u7528drop_duplicates()\u65b9\u6cd5\u5220\u9664\u91cd\u590d\u503c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528str.replace()\u65b9\u6cd5\u6765\u5904\u7406\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u83b7\u53d6\u4e0e\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u83b7\u53d6\u548c\u5bfc\u5165\u6570\u636e\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u5305\u62ec\u8bfb\u53d6CSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001\u6570\u636e\u5e93\u4ee5\u53ca\u4ece\u7f51\u7edc\u4e0a\u6293\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\u4e4b\u4e00\u3002Pandas\u5e93\u63d0\u4f9b\u4e86read_csv\u51fd\u6570\u6765\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d\u4e94\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9664\u4e86CSV\u6587\u4ef6\uff0cExcel\u6587\u4ef6\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\u3002Pandas\u5e93\u4e5f\u63d0\u4f9b\u4e86read_excel\u51fd\u6570\u6765\u8bfb\u53d6Excel\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d\u4e94\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>Python\u53ef\u4ee5\u901a\u8fc7\u5404\u79cd\u5e93\uff08\u5982SQLAlchemy\u3001Psycopg2\u3001PyMySQL\u7b49\uff09\u8fde\u63a5\u5230\u4e0d\u540c\u7684\u6570\u636e\u5e93\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\u6765\u83b7\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528SQLAlchemy\u4eceMySQL\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>engine = create_engine(&#39;mysql+pymysql:\/\/username:password@host:port\/database&#39;)<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2\u5e76\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_sql(&#39;SELECT * FROM table_name&#39;, engine)<\/p>\n<h2><strong>\u663e\u793a\u524d\u4e94\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u6e05\u6d17\u4e0e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u4e0e\u9884\u5904\u7406\u662f\u6570\u636e\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u5b83\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u91cd\u590d\u503c\u3001\u5f02\u5e38\u503c\u4ee5\u53ca\u683c\u5f0f\u4e0d\u6b63\u786e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u7f3a\u5931\u503c\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u3002Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\uff0c\u5305\u62ec\u5220\u9664\u7f3a\u5931\u503c\u548c\u586b\u5145\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/strong><\/h2>\n<p>data = data.dropna()<\/p>\n<h2><strong>\u7528\u6307\u5b9a\u503c\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data = data.fillna(value=0)<\/p>\n<h2><strong>\u7528\u5217\u7684\u5747\u503c\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data = data.fillna(data.mean())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u91cd\u590d\u503c<\/h4>\n<\/p>\n<p><p>\u91cd\u590d\u503c\u4e5f\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u3002Pandas\u63d0\u4f9b\u4e86duplicated\u548cdrop_duplicates\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u5220\u9664\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u91cd\u590d\u503c<\/strong><\/h2>\n<p>duplicates = data.duplicated()<\/p>\n<h2><strong>\u5220\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>data = data.drop_duplicates()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u5f02\u5e38\u503c<\/h4>\n<\/p>\n<p><p>\u5f02\u5e38\u503c\u662f\u6307\u660e\u663e\u504f\u79bb\u5176\u4ed6\u89c2\u6d4b\u503c\u7684\u6570\u636e\u70b9\u3002\u53ef\u4ee5\u4f7f\u7528\u7edf\u8ba1\u65b9\u6cd5\uff08\u5982\u6807\u51c6\u5dee\uff09\u6765\u68c0\u6d4b\u548c\u5904\u7406\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u5217\u7684\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_devs = data.std()<\/p>\n<h2><strong>\u627e\u5230\u5f02\u5e38\u503c\uff08\u8d85\u8fc73\u4e2a\u6807\u51c6\u5dee\u7684\u503c\uff09<\/strong><\/h2>\n<p>outliers = data[(data - data.mean()).abs() &gt; 3 * std_devs]<\/p>\n<h2><strong>\u5220\u9664\u5f02\u5e38\u503c<\/strong><\/h2>\n<p>data = data[(data - data.mean()).abs() &lt;= 3 * std_devs]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u636e\u63a2\u7d22\u4e0e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u63a2\u7d22\u4e0e\u53ef\u89c6\u5316\u662f\u6570\u636e\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u3001\u8d8b\u52bf\u548c\u5173\u7cfb\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u5305\u62ecMatplotlib\u3001Seaborn\u548cPlotly\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u56fe\u8868\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>data[&#39;column_name&#39;].value_counts().plot(kind=&#39;bar&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>data[&#39;column_name&#39;].plot(kind=&#39;line&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>data.plot(kind=&#39;scatter&#39;, x=&#39;column_x&#39;, y=&#39;column_y&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Seaborn\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u56fe\u8868\u548c\u66f4\u7b80\u6d01\u7684\u7ed8\u56fe\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u56fe\u8868\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>sns.histplot(data[&#39;column_name&#39;], kde=True)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u7bb1\u7ebf\u56fe<\/strong><\/h2>\n<p>sns.boxplot(x=&#39;column_x&#39;, y=&#39;column_y&#39;, data=data)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>sns.heatmap(data.corr(), annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6570\u636e\u5efa\u6a21\u4e0e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5efa\u6a21\u4e0e\u5206\u6790\u662f\u6570\u636e\u5206\u6790\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u5b83\u5305\u62ec\u7279\u5f81\u5de5\u7a0b\u3001\u6a21\u578b\u8bad\u7ec3\u4e0e\u8bc4\u4f30\u3001\u6a21\u578b\u8c03\u4f18\u7b49\u3002Scikit-learn\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u673a\u5668\u5b66\u4e60\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7b97\u6cd5\u548c\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7279\u5f81\u5de5\u7a0b<\/h4>\n<\/p>\n<p><p>\u7279\u5f81\u5de5\u7a0b\u662f\u6307\u901a\u8fc7\u5bf9\u539f\u59cb\u6570\u636e\u8fdb\u884c\u8f6c\u6362\u3001\u7ec4\u5408\u6216\u751f\u6210\u65b0\u7684\u7279\u5f81\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u8868\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7279\u5f81\u5de5\u7a0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from sklearn.preprocessing import StandardScaler, OneHotEncoder<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u6807\u51c6\u5316\u6570\u503c\u7279\u5f81<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>data[[&#39;numerical_feature&#39;]] = scaler.fit_transform(data[[&#39;numerical_feature&#39;]])<\/p>\n<h2><strong>\u72ec\u70ed\u7f16\u7801\u5206\u7c7b\u7279\u5f81<\/strong><\/h2>\n<p>encoder = OneHotEncoder()<\/p>\n<p>encoded_features = encoder.fit_transform(data[[&#39;categorical_feature&#39;]])<\/p>\n<p>encoded_df = pd.DataFrame(encoded_features.toarray(), columns=encoder.get_feature_names([&#39;categorical_feature&#39;]))<\/p>\n<p>data = pd.concat([data, encoded_df], axis=1).drop(&#39;categorical_feature&#39;, axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6a21\u578b\u8bad\u7ec3\u4e0e\u8bc4\u4f30<\/h4>\n<\/p>\n<p><p>\u6a21\u578b\u8bad\u7ec3\u4e0e\u8bc4\u4f30\u662f\u6570\u636e\u5efa\u6a21\u7684\u6838\u5fc3\u6b65\u9aa4\u3002Scikit-learn\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7b97\u6cd5\u548c\u8bc4\u4f30\u6307\u6807\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\u4e0e\u8bc4\u4f30\uff1a<\/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>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5206\u5272\u6570\u636e\u96c6\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X = data.drop(&#39;target&#39;, axis=1)<\/p>\n<p>y = data[&#39;target&#39;]<\/p>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\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\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>y_pred = model.predict(X_test)<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b\u8868\u73b0<\/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><h4>3\u3001\u6a21\u578b\u8c03\u4f18<\/h4>\n<\/p>\n<p><p>\u6a21\u578b\u8c03\u4f18\u662f\u6307\u901a\u8fc7\u8c03\u6574\u6a21\u578b\u7684\u8d85\u53c2\u6570\u6216\u7279\u5f81\u9009\u62e9\u6765\u63d0\u9ad8\u6a21\u578b\u7684\u8868\u73b0\u3002Scikit-learn\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8fdb\u884c\u6a21\u578b\u8c03\u4f18\uff0c\u5305\u62ec\u7f51\u683c\u641c\u7d22\u548c\u968f\u673a\u641c\u7d22\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u8fdb\u884c\u6a21\u578b\u8c03\u4f18\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import GridSearchCV<\/p>\n<p>from sklearn.ensemble import RandomForestRegressor<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5206\u5272\u6570\u636e\u96c6\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X = data.drop(&#39;target&#39;, axis=1)<\/p>\n<p>y = data[&#39;target&#39;]<\/p>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u5b9a\u4e49\u6a21\u578b\u548c\u53c2\u6570\u7f51\u683c<\/strong><\/h2>\n<p>model = RandomForestRegressor()<\/p>\n<p>param_grid = {<\/p>\n<p>    &#39;n_estimators&#39;: [50, 100, 200],<\/p>\n<p>    &#39;max_depth&#39;: [None, 10, 20, 30],<\/p>\n<p>    &#39;min_samples_split&#39;: [2, 5, 10]<\/p>\n<p>}<\/p>\n<h2><strong>\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u8fdb\u884c\u8d85\u53c2\u6570\u8c03\u4f18<\/strong><\/h2>\n<p>grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, scoring=&#39;neg_mean_squared_error&#39;)<\/p>\n<p>grid_search.fit(X_train, y_train)<\/p>\n<h2><strong>\u6253\u5370\u6700\u4f73\u53c2\u6570\u548c\u6700\u4f73\u5f97\u5206<\/strong><\/h2>\n<p>print(f&#39;Best Parameters: {grid_search.best_params_}&#39;)<\/p>\n<p>print(f&#39;Best Score: {grid_search.best_score_}&#39;)<\/p>\n<h2><strong>\u4f7f\u7528\u6700\u4f73\u6a21\u578b\u8fdb\u884c\u9884\u6d4b<\/strong><\/h2>\n<p>best_model = grid_search.best_estimator_<\/p>\n<p>y_pred = best_model.predict(X_test)<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b\u8868\u73b0<\/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><h3>\u4e94\u3001\u6570\u636e\u5206\u6790\u62a5\u544a\u4e0e\u5c55\u793a<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5206\u6790\u62a5\u544a\u4e0e\u5c55\u793a\u662f\u6570\u636e\u5206\u6790\u7684\u6700\u540e\u4e00\u6b65\u3002\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u56fe\u8868\u3001\u8868\u683c\u548c\u6587\u5b57\u8bf4\u660e\u6765\u5c55\u793a\u5206\u6790\u7ed3\u679c\uff0c\u5e76\u751f\u6210PDF\u62a5\u544a\u6216\u5728Jupyter Notebook\u4e2d\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u56fe\u8868\u548c\u8868\u683c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Matplotlib\u3001Seaborn\u548cPandas\u751f\u6210\u56fe\u8868\u548c\u8868\u683c\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u7247\u6216\u76f4\u63a5\u5d4c\u5165\u62a5\u544a\u4e2d\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<p>import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u751f\u6210\u67f1\u72b6\u56fe\u5e76\u4fdd\u5b58\u4e3a\u56fe\u7247<\/strong><\/h2>\n<p>data[&#39;column_name&#39;].value_counts().plot(kind=&#39;bar&#39;)<\/p>\n<p>plt.savefig(&#39;bar_chart.png&#39;)<\/p>\n<h2><strong>\u751f\u6210\u70ed\u529b\u56fe\u5e76\u4fdd\u5b58\u4e3a\u56fe\u7247<\/strong><\/h2>\n<p>sns.heatmap(data.corr(), annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<p>plt.savefig(&#39;heatmap.png&#39;)<\/p>\n<h2><strong>\u751f\u6210\u8868\u683c\u5e76\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>summary = data.describe()<\/p>\n<p>summary.to_csv(&#39;summary.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u751f\u6210PDF\u62a5\u544a<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528ReportLab\u5e93\u751f\u6210PDF\u62a5\u544a\uff0c\u5e76\u5c06\u56fe\u8868\u548c\u8868\u683c\u5d4c\u5165\u5176\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.lib.pagesizes import letter<\/p>\n<p>from reportlab.pdfgen import canvas<\/p>\n<h2><strong>\u521b\u5efaPDF\u6587\u6863<\/strong><\/h2>\n<p>c = canvas.Canvas(&#39;report.pdf&#39;, pagesize=letter)<\/p>\n<p>width, height = letter<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>c.setFont(&#39;Helvetica-Bold&#39;, 16)<\/p>\n<p>c.drawString(100, height - 50, &#39;Data Analysis Report&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u5b57\u8bf4\u660e<\/strong><\/h2>\n<p>c.setFont(&#39;Helvetica&#39;, 12)<\/p>\n<p>c.drawString(100, height - 100, &#39;This is a data analysis report generated using Python.&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u8868<\/strong><\/h2>\n<p>c.drawImage(&#39;bar_chart.png&#39;, 100, height - 400, width=400, height=300)<\/p>\n<p>c.drawImage(&#39;heatmap.png&#39;, 100, height - 800, width=400, height=300)<\/p>\n<h2><strong>\u4fdd\u5b58PDF\u6587\u6863<\/strong><\/h2>\n<p>c.save()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5728Jupyter Notebook\u4e2d\u5c55\u793a<\/h4>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u6570\u636e\u5206\u6790\u548c\u5c55\u793a\u7684\u5e38\u7528\u5de5\u5177\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728Notebook\u4e2d\u5c55\u793a\u56fe\u8868\u3001\u8868\u683c\u548c\u6587\u5b57\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u8868\u683c<\/strong><\/h2>\n<p>display(data.head())<\/p>\n<h2><strong>\u751f\u6210\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>data[&#39;column_name&#39;].value_counts().plot(kind=&#39;bar&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u751f\u6210\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>sns.heatmap(data.corr(), annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u6dfb\u52a0\u6587\u5b57\u8bf4\u660e<\/strong><\/h2>\n<p>from IPython.display import display, Markdown<\/p>\n<p>display(Markdown(&#39;### Data Analysis Report&#39;))<\/p>\n<p>display(Markdown(&#39;This is a data analysis report generated using Python.&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3\u4e0e\u5c55\u671b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u5305\u62ec\u6570\u636e\u83b7\u53d6\u4e0e\u5bfc\u5165\u3001\u6570\u636e\u6e05\u6d17\u4e0e\u9884\u5904\u7406\u3001\u6570\u636e\u63a2\u7d22\u4e0e\u53ef\u89c6\u5316\u3001\u6570\u636e\u5efa\u6a21\u4e0e\u5206\u6790\u4ee5\u53ca\u6570\u636e\u5206\u6790\u62a5\u544a\u4e0e\u5c55\u793a\u3002\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u6709\u5176\u91cd\u8981\u6027\u548c\u590d\u6742\u6027\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5206\u6790\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><p>\u968f\u7740\u6570\u636e\u91cf\u7684\u589e\u52a0\u548c\u5206\u6790\u9700\u6c42\u7684\u590d\u6742\u5316\uff0c\u6570\u636e\u5206\u6790\u5de5\u5177\u548c\u65b9\u6cd5\u4e5f\u5728\u4e0d\u65ad\u53d1\u5c55\u3002\u672a\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u671f\u5f85\u66f4\u591a\u9ad8\u6548\u3001\u667a\u80fd\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\u548c\u65b9\u6cd5\u7684\u51fa\u73b0\uff0c\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5229\u7528\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>Python\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u5df2\u7ecf\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u9886\u57df\uff0c\u5305\u62ec\u91d1\u878d\u3001\u533b\u7597\u3001\u5e02\u573a\u8425\u9500\u7b49\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u638c\u63e1Python\u7684\u6570\u636e\u5206\u6790\u6280\u80fd\uff0c\u5e76\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u5e94\u7528\u8fd9\u4e9b\u6280\u80fd\uff0c\u89e3\u51b3\u5404\u79cd\u6570\u636e\u5206\u6790\u95ee\u9898\uff0c\u4e3a\u51b3\u7b56\u63d0\u4f9b\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\uff1f<\/strong><br \/>\u8981\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u60a8\u9700\u8981\u5b89\u88c5Python\u53ca\u5176\u76f8\u5173\u5e93\uff0c\u5982Pandas\u3001NumPy\u548cMatplotlib\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u5de5\u5177\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7Anaconda\u6216\u76f4\u63a5\u4f7f\u7528pip\u5b89\u88c5\u6240\u9700\u5e93\u3002\u5b66\u4e60\u5982\u4f55\u5bfc\u5165\u6570\u636e\u3001\u6e05\u7406\u6570\u636e\u548c\u6267\u884c\u57fa\u672c\u7684\u7edf\u8ba1\u5206\u6790\u662f\u5165\u95e8\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\uff1f<\/strong><br \/>\u5728Python\u7684\u6570\u636e\u5206\u6790\u4e2d\uff0c\u51e0\u4e2a\u5e38\u7528\u7684\u5e93\u5305\u62ecPandas\u3001NumPy\u3001Matplotlib\u548cSeaborn\u3002Pandas\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\uff0cNumPy\u63d0\u4f9b\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\uff0cMatplotlib\u548cSeaborn\u5219\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u8fd9\u4e9b\u5e93\u7684\u7ed3\u5408\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4ece\u6570\u636e\u4e2d\u63d0\u53d6\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\u53d6\u51b3\u4e8e\u60a8\u8981\u8868\u8fbe\u7684\u4fe1\u606f\u548c\u6570\u636e\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u56fe\u8868\uff0cMatplotlib\u53ef\u80fd\u8db3\u591f\u7528\uff0c\u800c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u56fe\u5f62\uff0cSeaborn\u63d0\u4f9b\u4e86\u66f4\u53cb\u597d\u7684\u63a5\u53e3\u548c\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002\u5982\u679c\u60a8\u5e0c\u671b\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\uff0cPlotly\u548cBokeh\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002\u4e86\u89e3\u6bcf\u4e2a\u5de5\u5177\u7684\u7279\u70b9\u6709\u52a9\u4e8e\u60a8\u505a\u51fa\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u3001NumPy\u3001Matplotlib\u3001Seaborn\u3001Sci [&hellip;]","protected":false},"author":3,"featured_media":1177468,"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\/1177460"}],"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=1177460"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177460\/revisions"}],"predecessor-version":[{"id":1177469,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177460\/revisions\/1177469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1177468"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1177460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1177460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1177460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}