{"id":1154547,"date":"2025-01-13T17:52:34","date_gmt":"2025-01-13T09:52:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154547.html"},"modified":"2025-01-13T17:52:36","modified_gmt":"2025-01-13T09:52:36","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%b7%b2%e7%9f%a5xy","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154547.html","title":{"rendered":"\u5982\u4f55\u7528python\u5df2\u77e5xy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184130\/f77ab3ec-5244-4271-802a-89410aca36a3.webp\" alt=\"\u5982\u4f55\u7528python\u5df2\u77e5xy\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u4f7f\u7528\u5df2\u77e5\u7684x\u548cy\u6570\u636e\u8fdb\u884c\u5206\u6790\u548c\u5efa\u6a21\u662f\u6570\u636e\u79d1\u5b66\u4e2d\u5e38\u89c1\u7684\u4efb\u52a1\u3002<strong>\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u56fe\u8868\u3001\u62df\u5408\u66f2\u7ebf\u3001\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u7b49\u65b9\u5f0f\u8fdb\u884c\u5904\u7406<\/strong>\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\uff1a\u4f7f\u7528 <code>matplotlib<\/code> \u548c <code>numpy<\/code> \u5e93\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u548c\u66f2\u7ebf\u62df\u5408\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MATPLOTLIB\u7ed8\u5236\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p>1\u3001<strong>\u5bfc\u5165\u6570\u636e\u5e76\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u5e76\u51c6\u5907\u597d\u6211\u4eec\u7684x\u548cy\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528 <code>matplotlib<\/code> \u6765\u7ed8\u5236\u6563\u70b9\u56fe\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>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;X-Y\u6563\u70b9\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001<strong>\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0<\/strong><br \/>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u7ed8\u5236\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6563\u70b9\u56fe\uff0cx\u8f74\u548cy\u8f74\u5206\u522b\u8868\u793a\u6211\u4eec\u6570\u636e\u7684\u4e24\u4e2a\u7ef4\u5ea6\u3002 <code>plt.scatter<\/code> \u51fd\u6570\u7528\u6765\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u4e14\u53ef\u4ee5\u8bbe\u7f6e\u6807\u7b7e\u548c\u6807\u9898\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001NUMPY\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><p>1\u3001<strong>\u4f7f\u7528numpy\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/strong><br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>numpy<\/code> \u5e93\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\uff0c\u6765\u627e\u5230\u4e00\u6761\u6700\u9002\u5408\u6211\u4eec\u7684\u6570\u636e\u7684\u76f4\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528numpy\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/strong><\/h2>\n<p>coefficients = np.polyfit(x, y, 1)  # 1\u8868\u793a\u7ebf\u6027<\/p>\n<p>poly = np.poly1d(coefficients)<\/p>\n<h2><strong>\u751f\u6210\u62df\u5408\u76f4\u7ebf\u7684y\u503c<\/strong><\/h2>\n<p>y_fit = poly(x)<\/p>\n<h2><strong>\u7ed8\u5236\u62df\u5408\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.scatter(x, y, label=&#39;\u6570\u636e\u70b9&#39;)<\/p>\n<p>plt.plot(x, y_fit, color=&#39;red&#39;, label=&#39;\u62df\u5408\u76f4\u7ebf&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u7ebf\u6027\u56de\u5f52&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001<strong>\u8be6\u7ec6\u63cf\u8ff0\u7ebf\u6027\u56de\u5f52\u8fc7\u7a0b<\/strong><br \/>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>np.polyfit<\/code> \u51fd\u6570\u6765\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\uff0c\u5f97\u5230\u7ebf\u6027\u56de\u5f52\u7684\u7cfb\u6570\u3002 <code>np.poly1d<\/code> \u51fd\u6570\u5219\u751f\u6210\u4e86\u4e00\u4e2a\u591a\u9879\u5f0f\u5bf9\u8c61\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u62df\u5408\u76f4\u7ebf\u4e0a\u7684y\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>plt.plot<\/code> \u7ed8\u5236\u51fa\u62df\u5408\u76f4\u7ebf\uff0c\u5e76\u4e0e\u539f\u59cb\u6570\u636e\u70b9\u4e00\u8d77\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u636e\u7684\u7edf\u8ba1\u5206\u6790<\/h3>\n<\/p>\n<p><p>1\u3001<strong>\u8ba1\u7b97\u6570\u636e\u7684\u57fa\u672c\u7edf\u8ba1\u91cf<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\uff0c\u8ba1\u7b97\u6570\u636e\u7684\u57fa\u672c\u7edf\u8ba1\u91cf\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>numpy<\/code> \u548c <code>scipy<\/code> \u5e93\u6765\u8ba1\u7b97\u5e73\u5747\u503c\u3001\u65b9\u5dee\u3001\u6807\u51c6\u5dee\u7b49\u7edf\u8ba1\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import stats<\/p>\n<h2><strong>\u8ba1\u7b97\u57fa\u672c\u7edf\u8ba1\u91cf<\/strong><\/h2>\n<p>mean_x = np.mean(x)<\/p>\n<p>mean_y = np.mean(y)<\/p>\n<p>variance_x = np.var(x)<\/p>\n<p>variance_y = np.var(y)<\/p>\n<p>std_dev_x = np.std(x)<\/p>\n<p>std_dev_y = np.std(y)<\/p>\n<p>correlation, _ = stats.pearsonr(x, y)<\/p>\n<p>print(f&#39;X\u7684\u5e73\u5747\u503c: {mean_x}&#39;)<\/p>\n<p>print(f&#39;Y\u7684\u5e73\u5747\u503c: {mean_y}&#39;)<\/p>\n<p>print(f&#39;X\u7684\u65b9\u5dee: {variance_x}&#39;)<\/p>\n<p>print(f&#39;Y\u7684\u65b9\u5dee: {variance_y}&#39;)<\/p>\n<p>print(f&#39;X\u7684\u6807\u51c6\u5dee: {std_dev_x}&#39;)<\/p>\n<p>print(f&#39;Y\u7684\u6807\u51c6\u5dee: {std_dev_y}&#39;)<\/p>\n<p>print(f&#39;X\u548cY\u7684\u76f8\u5173\u7cfb\u6570: {correlation}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001<strong>\u8be6\u7ec6\u63cf\u8ff0\u7edf\u8ba1\u5206\u6790\u8fc7\u7a0b<\/strong><br \/>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u8ba1\u7b97\u4e86x\u548cy\u6570\u636e\u7684\u5e73\u5747\u503c\u3001\u65b9\u5dee\u3001\u6807\u51c6\u5dee\u548c\u76f8\u5173\u7cfb\u6570\u3002<strong>\u8fd9\u4e9b\u7edf\u8ba1\u91cf\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u7279\u6027\u3002<\/strong>\u4f8b\u5982\uff0c\u76f8\u5173\u7cfb\u6570\u53cd\u6620\u4e86x\u548cy\u4e4b\u95f4\u7684\u7ebf\u6027\u76f8\u5173\u6027\uff0c\u65b9\u5dee\u548c\u6807\u51c6\u5dee\u5219\u53cd\u6620\u4e86\u6570\u636e\u7684\u79bb\u6563\u7a0b\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001PANDAS\u8fdb\u884c\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>1\u3001<strong>\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/strong><br \/>\u9664\u4e86 <code>numpy<\/code> \u548c <code>scipy<\/code>\uff0c <code>pandas<\/code> \u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u6765\u52a0\u8f7d\u3001\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;X&#39;: x, &#39;Y&#39;: y}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u663e\u793a\u6570\u636e\u6846\u67b6<\/strong><\/h2>\n<p>print(df)<\/p>\n<h2><strong>\u8ba1\u7b97\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf<\/strong><\/h2>\n<p>print(df.describe())<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;scatter&#39;, x=&#39;X&#39;, y=&#39;Y&#39;, title=&#39;X-Y\u6563\u70b9\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001<strong>\u8be6\u7ec6\u63cf\u8ff0Pandas\u6570\u636e\u5904\u7406\u8fc7\u7a0b<\/strong><br \/>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a <code>DataFrame<\/code> \uff0c\u8fd9\u662f\u4e00\u79cd\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u7684\u6570\u636e\u7ed3\u6784\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>describe<\/code> \u65b9\u6cd5\u8ba1\u7b97\u4e86\u6570\u636e\u7684\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf\uff0c\u5e76\u4f7f\u7528 <code>plot<\/code> \u65b9\u6cd5\u7ed8\u5236\u4e86\u6563\u70b9\u56fe\u3002<strong><code>pandas<\/code> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u662f\u6570\u636e\u79d1\u5b66\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001SCIKIT-LEARN\u8fdb\u884c\u9ad8\u7ea7\u5efa\u6a21<\/h3>\n<\/p>\n<p><p>1\u3001<strong>\u4f7f\u7528Scikit-learn\u8fdb\u884c\u56de\u5f52\u5206\u6790<\/strong><br \/><code>scikit-learn<\/code> \u662f\u4e00\u4e2a\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7684\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u3001\u5efa\u6a21\u548c\u8bc4\u4f30\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>scikit-learn<\/code> \u8fdb\u884c\u591a\u79cd\u56de\u5f52\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u8f6c\u6362\u6570\u636e\u683c\u5f0f<\/strong><\/h2>\n<p>x_reshape = np.array(x).reshape(-1, 1)<\/p>\n<p>y_reshape = np.array(y)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(x_reshape, y_reshape)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = model.predict(x_reshape)<\/p>\n<h2><strong>\u7ed8\u5236\u62df\u5408\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.scatter(x, y, label=&#39;\u6570\u636e\u70b9&#39;)<\/p>\n<p>plt.plot(x, y_pred, color=&#39;red&#39;, label=&#39;\u62df\u5408\u76f4\u7ebf&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u7ebf\u6027\u56de\u5f52&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001<strong>\u8be6\u7ec6\u63cf\u8ff0Scikit-learn\u5efa\u6a21\u8fc7\u7a0b<\/strong><br \/>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u636e\u8f6c\u6362\u6210\u9002\u5408 <code>scikit-learn<\/code> \u7684\u683c\u5f0f\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u5e76\u4f7f\u7528 <code>fit<\/code> \u65b9\u6cd5\u8fdb\u884c\u8bad\u7ec3\u3002\u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>predict<\/code> \u65b9\u6cd5\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u7ed8\u5236\u4e86\u62df\u5408\u76f4\u7ebf\u3002<strong><code>scikit-learn<\/code> \u63d0\u4f9b\u4e86\u591a\u79cd\u56de\u5f52\u6a21\u578b\uff0c\u9664\u4e86\u7ebf\u6027\u56de\u5f52\uff0c\u8fd8\u5305\u62ec\u5cad\u56de\u5f52\u3001lasso\u56de\u5f52\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001SEABORN\u8fdb\u884c\u9ad8\u7ea7\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>1\u3001<strong>\u4f7f\u7528Seaborn\u8fdb\u884c\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316<\/strong><br \/><code>seaborn<\/code> \u662f\u4e00\u4e2a\u57fa\u4e8e <code>matplotlib<\/code> \u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u548c\u6613\u7528\u7684\u63a5\u53e3\u6765\u521b\u5efa\u7f8e\u89c2\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;X&#39;: x, &#39;Y&#39;: y}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe\u548c\u56de\u5f52\u76f4\u7ebf<\/strong><\/h2>\n<p>sns.lmplot(x=&#39;X&#39;, y=&#39;Y&#39;, data=df)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;X-Y\u6563\u70b9\u56fe\u548c\u56de\u5f52\u76f4\u7ebf&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001<strong>\u8be6\u7ec6\u63cf\u8ff0Seaborn\u53ef\u89c6\u5316\u8fc7\u7a0b<\/strong><br \/>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>seaborn<\/code> \u7684 <code>lmplot<\/code> \u51fd\u6570\u7ed8\u5236\u4e86\u6563\u70b9\u56fe\u548c\u56de\u5f52\u76f4\u7ebf\u3002<strong><code>seaborn<\/code> \u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u63a5\u53e3\u6765\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u5e76\u4e14\u751f\u6210\u7684\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u3002<\/strong>\u4f8b\u5982\uff0c<code>lmplot<\/code> \u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u8fd8\u53ef\u4ee5\u81ea\u52a8\u6dfb\u52a0\u56de\u5f52\u76f4\u7ebf\u548c\u7f6e\u4fe1\u533a\u95f4\uff0c\u975e\u5e38\u9002\u5408\u8fdb\u884c\u6570\u636e\u63a2\u7d22\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001PYTORCH\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u5efa\u6a21<\/h3>\n<\/p>\n<p><p>1\u3001<strong>\u4f7f\u7528PyTorch\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u5efa\u6a21<\/strong><br \/>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5efa\u6a21\u4efb\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6 <code>PyTorch<\/code>\u3002 <code>PyTorch<\/code> \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u6784\u5efa\u548c\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>import torch.nn as nn<\/p>\n<p>import torch.optim as optim<\/p>\n<h2><strong>\u8f6c\u6362\u6570\u636e\u683c\u5f0f<\/strong><\/h2>\n<p>x_tensor = torch.tensor(x, dtype=torch.float32).reshape(-1, 1)<\/p>\n<p>y_tensor = torch.tensor(y, dtype=torch.float32).reshape(-1, 1)<\/p>\n<h2><strong>\u5b9a\u4e49\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/strong><\/h2>\n<p>class LinearRegressionModel(nn.Module):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super(LinearRegressionModel, self).__init__()<\/p>\n<p>        self.linear = nn.Linear(1, 1)<\/p>\n<p>    def forward(self, x):<\/p>\n<p>        return self.linear(x)<\/p>\n<h2><strong>\u521b\u5efa\u6a21\u578b\u5b9e\u4f8b<\/strong><\/h2>\n<p>model = LinearRegressionModel()<\/p>\n<h2><strong>\u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<\/strong><\/h2>\n<p>criterion = nn.MSELoss()<\/p>\n<p>optimizer = optim.SGD(model.parameters(), lr=0.01)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>for epoch in range(1000):<\/p>\n<p>    model.tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n()<\/p>\n<p>    optimizer.zero_grad()<\/p>\n<p>    outputs = model(x_tensor)<\/p>\n<p>    loss = criterion(outputs, y_tensor)<\/p>\n<p>    loss.backward()<\/p>\n<p>    optimizer.step()<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>model.eval()<\/p>\n<p>with torch.no_grad():<\/p>\n<p>    y_pred = model(x_tensor)<\/p>\n<h2><strong>\u7ed8\u5236\u62df\u5408\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.scatter(x, y, label=&#39;\u6570\u636e\u70b9&#39;)<\/p>\n<p>plt.plot(x, y_pred.numpy(), color=&#39;red&#39;, label=&#39;\u62df\u5408\u76f4\u7ebf&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6df1\u5ea6\u5b66\u4e60\u7ebf\u6027\u56de\u5f52&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001<strong>\u8be6\u7ec6\u63cf\u8ff0PyTorch\u6df1\u5ea6\u5b66\u4e60\u5efa\u6a21\u8fc7\u7a0b<\/strong><br \/>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u636e\u8f6c\u6362\u6210\u9002\u5408 <code>PyTorch<\/code> \u7684\u5f20\u91cf\u683c\u5f0f\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002\u6211\u4eec\u4f7f\u7528\u5747\u65b9\u8bef\u5dee\u635f\u5931\u51fd\u6570\u548c\u968f\u673a\u68af\u5ea6\u4e0b\u964d\u4f18\u5316\u5668\u6765\u8bad\u7ec3\u6a21\u578b\u3002\u7ecf\u8fc71000\u4e2aepoch\u7684\u8bad\u7ec3\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u7ed8\u5236\u4e86\u62df\u5408\u76f4\u7ebf\u3002<strong><code>PyTorch<\/code> \u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u63a5\u53e3\u6765\u6784\u5efa\u548c\u8bad\u7ec3\u5404\u79cd\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u90e8\u5206\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u5730\u8bb2\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u5904\u7406\u5df2\u77e5\u7684xy\u6570\u636e\u3002<strong>\u4ece\u57fa\u672c\u7684\u53ef\u89c6\u5316\u5230\u9ad8\u7ea7\u7684\u5efa\u6a21\u548c\u5206\u6790\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u9700\u6c42\u3002<\/strong>\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6570\u636e\u63a2\u7d22\uff0c\u8fd8\u662f\u590d\u6742\u7684\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\uff0cPython\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5b8c\u6210\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\uff0c\u5e76\u5e94\u7528\u5230\u5b9e\u9645\u5de5\u4f5c\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u6839\u636e\u5df2\u77e5\u7684x\u548cy\u503c\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>scikit-learn<\/code>\u5e93\u6765\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u3002\u9996\u5148\uff0c\u9700\u8981\u5c06\u5df2\u77e5\u7684x\u548cy\u503c\u6574\u7406\u6210\u5408\u9002\u7684\u683c\u5f0f\u3002\u63a5\u7740\uff0c\u521b\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u5e76\u8fdb\u884c\u8bad\u7ec3\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>fit()<\/code>\u65b9\u6cd5\u6765\u8bad\u7ec3\u6a21\u578b\uff0c\u4e4b\u540e\u4f7f\u7528<code>predict()<\/code>\u65b9\u6cd5\u6765\u9884\u6d4b\u65b0\u7684y\u503c\u3002\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\u5305\u62ec\u6570\u636e\u5bfc\u5165\u3001\u6a21\u578b\u521b\u5efa\u3001\u8bad\u7ec3\u548c\u9884\u6d4b\u6b65\u9aa4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u7ed8\u5236\u5df2\u77e5x\u548cy\u503c\u7684\u6563\u70b9\u56fe\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u3002\u9996\u5148\uff0c\u5bfc\u5165\u8be5\u5e93\u5e76\u51c6\u5907\u597dx\u548cy\u6570\u636e\u3002\u4f7f\u7528<code>plt.scatter()<\/code>\u51fd\u6570\u53ef\u4ee5\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u968f\u540e\u4f7f\u7528<code>plt.show()<\/code>\u663e\u793a\u56fe\u5f62\u3002\u901a\u8fc7\u8bbe\u7f6e\u56fe\u6807\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u56fe\u5f62\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u542b\u6709\u7f3a\u5931x\u6216y\u503c\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406\u7f3a\u5931\u6570\u636e\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u91cd\u8981\u73af\u8282\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>dropna()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u7f3a\u5931\u503c\uff0c\u6216\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u586b\u8865\u7f3a\u5931\u503c\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u6027\u8d28\u548c\u540e\u7eed\u5206\u6790\u7684\u9700\u6c42\u3002\u5728\u8fdb\u884c\u4efb\u4f55\u5206\u6790\u4e4b\u524d\uff0c\u786e\u4fdd\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u7406\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5df2\u77e5\u7684x\u548cy\u6570\u636e\u8fdb\u884c\u5206\u6790\u548c\u5efa\u6a21\u662f\u6570\u636e\u79d1\u5b66\u4e2d\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u56fe\u8868\u3001\u62df\u5408\u66f2\u7ebf\u3001\u8fdb\u884c\u7edf [&hellip;]","protected":false},"author":3,"featured_media":1154555,"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\/1154547"}],"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=1154547"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154547\/revisions"}],"predecessor-version":[{"id":1154557,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154547\/revisions\/1154557"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154555"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}