{"id":1153797,"date":"2025-01-13T17:43:08","date_gmt":"2025-01-13T09:43:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153797.html"},"modified":"2025-01-13T17:43:16","modified_gmt":"2025-01-13T09:43:16","slug":"%e6%95%b0%e5%ad%a6%e5%bb%ba%e6%a8%a1%e5%a6%82%e4%bd%95%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153797.html","title":{"rendered":"\u6570\u5b66\u5efa\u6a21\u5982\u4f55\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183511\/3dcbb5d3-9aea-4b6e-a6f6-d71dc384017c.webp\" alt=\"\u6570\u5b66\u5efa\u6a21\u5982\u4f55\u7528python\" \/><\/p>\n<p><p> \u6570\u5b66\u5efa\u6a21\u5982\u4f55\u7528Python\uff1f<strong>\u6570\u5b66\u5efa\u6a21\u7528Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\uff1a\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3001\u4f7f\u7528SciPy\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3001\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u4f7f\u7528Scikit-Learn\u8fdb\u884c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/strong>\u3002\u5176\u4e2d\uff0cNumPy\u662fPython\u4e2d\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u8fd0\u7b97\u3002\u5b83\u4e0d\u4ec5\u652f\u6301\u5e38\u89c1\u7684\u6570\u5b66\u51fd\u6570\u3001\u7ebf\u6027\u4ee3\u6570\u548c\u968f\u673a\u6570\u751f\u6210\uff0c\u8fd8\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u8868\u73b0\u51fa\u8272\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001NumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u6700\u91cd\u8981\u7684\u6570\u503c\u8ba1\u7b97\u5e93\u4e4b\u4e00\u3002\u5b83\u7684\u6838\u5fc3\u662f\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff08ndarray\uff09\uff0c\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u9ad8\u6548\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u901a\u8fc7NumPy\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u77e9\u9635\u8fd0\u7b97\u3001\u5085\u91cc\u53f6\u53d8\u6362\u3001\u968f\u673a\u6570\u751f\u6210\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u548c\u64cd\u4f5c\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u7684\u4e3b\u8981\u6570\u636e\u7ed3\u6784\u662fndarray\uff0c\u5b83\u662f\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\u3002\u521b\u5efa\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6bd4\u5982\u901a\u8fc7\u5217\u8868\u521b\u5efa\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u521b\u5efa\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4ece\u5217\u8868\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>a = np.array([1, 2, 3, 4])<\/p>\n<p>print(a)<\/p>\n<h2><strong>\u521b\u5efa\u5168\u96f6\u6570\u7ec4<\/strong><\/h2>\n<p>b = np.zeros((2, 3))<\/p>\n<p>print(b)<\/p>\n<h2><strong>\u521b\u5efa\u5168\u4e00\u6570\u7ec4<\/strong><\/h2>\n<p>c = np.ones((2, 3))<\/p>\n<p>print(c)<\/p>\n<h2><strong>\u521b\u5efa\u5355\u4f4d\u77e9\u9635<\/strong><\/h2>\n<p>d = np.eye(3)<\/p>\n<p>print(d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6570\u7ec4\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u9664\u6cd5\u3001\u77e9\u9635\u4e58\u6cd5\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([4, 5, 6])<\/p>\n<h2><strong>\u6570\u7ec4\u52a0\u6cd5<\/strong><\/h2>\n<p>c = a + b<\/p>\n<p>print(c)<\/p>\n<h2><strong>\u6570\u7ec4\u51cf\u6cd5<\/strong><\/h2>\n<p>d = a - b<\/p>\n<p>print(d)<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>e = a * b<\/p>\n<p>print(e)<\/p>\n<h2><strong>\u6570\u7ec4\u9664\u6cd5<\/strong><\/h2>\n<p>f = a \/ b<\/p>\n<p>print(f)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>g = np.dot(a, b)<\/p>\n<p>print(g)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u7ebf\u6027\u4ee3\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u8fd8\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ebf\u6027\u4ee3\u6570\u529f\u80fd\uff0c\u5305\u62ec\u77e9\u9635\u4e58\u6cd5\u3001\u6c42\u9006\u3001\u7279\u5f81\u503c\u5206\u89e3\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u77e9\u9635<\/strong><\/h2>\n<p>A = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>B = np.dot(A, A)<\/p>\n<p>print(B)<\/p>\n<h2><strong>\u77e9\u9635\u6c42\u9006<\/strong><\/h2>\n<p>C = np.linalg.inv(A)<\/p>\n<p>print(C)<\/p>\n<h2><strong>\u77e9\u9635\u7279\u5f81\u503c\u5206\u89e3<\/strong><\/h2>\n<p>D, E = np.linalg.eig(A)<\/p>\n<p>print(D)<\/p>\n<p>print(E)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001SciPy\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p>SciPy\u662f\u5efa\u7acb\u5728NumPy\u57fa\u7840\u4e0a\u7684\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u3001\u79d1\u5b66\u548c\u5de5\u7a0b\u529f\u80fd\u3002SciPy\u5305\u542b\u4e86\u8bb8\u591a\u5b50\u6a21\u5757\uff0c\u5982\u4f18\u5316\u3001\u63d2\u503c\u3001\u79ef\u5206\u3001\u7ebf\u6027\u4ee3\u6570\u3001\u7edf\u8ba1\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f18\u5316<\/h3>\n<\/p>\n<p><p>SciPy\u7684\u4f18\u5316\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u4f18\u5316\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u51fd\u6570\u6700\u5c0f\u5316\u3001\u66f2\u7ebf\u62df\u5408\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f18\u5316\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.optimize import minimize<\/p>\n<h2><strong>\u5b9a\u4e49\u76ee\u6807\u51fd\u6570<\/strong><\/h2>\n<p>def objective(x):<\/p>\n<p>    return x2 + 2*x + 1<\/p>\n<h2><strong>\u521d\u59cb\u731c\u6d4b\u503c<\/strong><\/h2>\n<p>x0 = 0<\/p>\n<h2><strong>\u6267\u884c\u4f18\u5316<\/strong><\/h2>\n<p>result = minimize(objective, x0)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u63d2\u503c<\/h3>\n<\/p>\n<p><p>\u63d2\u503c\u662f\u6307\u901a\u8fc7\u5df2\u77e5\u7684\u6570\u636e\u70b9\u6765\u4f30\u8ba1\u672a\u77e5\u7684\u6570\u636e\u70b9\u3002SciPy\u7684\u63d2\u503c\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u63d2\u503c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u63d2\u503c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.interpolate import interp1d<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u5df2\u77e5\u7684\u6570\u636e\u70b9<\/strong><\/h2>\n<p>x = np.array([0, 1, 2, 3, 4])<\/p>\n<p>y = np.array([0, 1, 4, 9, 16])<\/p>\n<h2><strong>\u521b\u5efa\u63d2\u503c\u51fd\u6570<\/strong><\/h2>\n<p>f = interp1d(x, y, kind=&#39;cubic&#39;)<\/p>\n<h2><strong>\u4f30\u8ba1\u672a\u77e5\u7684\u6570\u636e\u70b9<\/strong><\/h2>\n<p>x_new = np.linspace(0, 4, 100)<\/p>\n<p>y_new = f(x_new)<\/p>\n<p>print(y_new)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u79ef\u5206<\/h3>\n<\/p>\n<p><p>SciPy\u7684\u79ef\u5206\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u503c\u79ef\u5206\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5b9a\u79ef\u5206\u3001\u4e0d\u5b9a\u79ef\u5206\u3001\u591a\u91cd\u79ef\u5206\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u79ef\u5206\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.integrate import quad<\/p>\n<h2><strong>\u5b9a\u4e49\u88ab\u79ef\u51fd\u6570<\/strong><\/h2>\n<p>def integrand(x):<\/p>\n<p>    return x2<\/p>\n<h2><strong>\u8ba1\u7b97\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>result, error = quad(integrand, 0, 1)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/h2>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u751f\u6210\u591a\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u3001\u76f4\u65b9\u56fe\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u6298\u7ebf\u56fe<\/h3>\n<\/p>\n<p><p>\u6298\u7ebf\u56fe\u662f\u6700\u5e38\u89c1\u7684\u56fe\u8868\u7c7b\u578b\u4e4b\u4e00\uff0c\u7528\u4e8e\u663e\u793a\u6570\u636e\u7684\u53d8\u5316\u8d8b\u52bf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<p>plt.title(&#39;\u6298\u7ebf\u56fe\u793a\u4f8b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p>\u67f1\u72b6\u56fe\u7528\u4e8e\u663e\u793a\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u636e\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u67f1\u72b6\u56fe\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>values = [10, 20, 15, 30]<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>plt.bar(categories, values)<\/p>\n<p>plt.xlabel(&#39;\u7c7b\u522b&#39;)<\/p>\n<p>plt.ylabel(&#39;\u503c&#39;)<\/p>\n<p>plt.title(&#39;\u67f1\u72b6\u56fe\u793a\u4f8b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p>\u6563\u70b9\u56fe\u7528\u4e8e\u663e\u793a\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6563\u70b9\u56fe\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<p>plt.title(&#39;\u6563\u70b9\u56fe\u793a\u4f8b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406<\/h2>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u5904\u7406\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002Pandas\u7684\u4e3b\u8981\u6570\u636e\u7ed3\u6784\u662fDataFrame\uff0c\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u8868\u683c\uff0c\u5305\u542b\u884c\u548c\u5217\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u548c\u64cd\u4f5cDataFrame<\/h3>\n<\/p>\n<p><p>DataFrame\u53ef\u4ee5\u4ece\u591a\u79cd\u6570\u636e\u6e90\u521b\u5efa\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u3001NumPy\u6570\u7ec4\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4ece\u5b57\u5178\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4ece\u5217\u8868\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = [[1, 4], [2, 5], [3, 6]]<\/p>\n<p>df = pd.DataFrame(data, columns=[&#39;A&#39;, &#39;B&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6570\u636e\u7b5b\u9009\u548c\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u7b5b\u9009\u548c\u8fc7\u6ee4\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/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;A&#39;: [1, 2, 3, 4], &#39;B&#39;: [5, 6, 7, 8]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7b5b\u9009\u6ee1\u8db3\u6761\u4ef6\u7684\u884c<\/strong><\/h2>\n<p>filtered_df = df[df[&#39;A&#39;] &gt; 2]<\/p>\n<p>print(filtered_df)<\/p>\n<h2><strong>\u7b5b\u9009\u6307\u5b9a\u7684\u5217<\/strong><\/h2>\n<p>selected_columns = df[[&#39;A&#39;]]<\/p>\n<p>print(selected_columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6570\u636e\u7edf\u8ba1\u548c\u805a\u5408<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u7edf\u8ba1\u548c\u805a\u5408\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u7684\u6c47\u603b\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/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;A&#39;: [1, 2, 3, 4], &#39;B&#39;: [5, 6, 7, 8]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u503c<\/strong><\/h2>\n<p>mean_A = df[&#39;A&#39;].mean()<\/p>\n<p>print(mean_A)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>median_B = df[&#39;B&#39;].median()<\/p>\n<p>print(median_B)<\/p>\n<h2><strong>\u6309\u5217\u8fdb\u884c\u805a\u5408<\/strong><\/h2>\n<p>grouped = df.groupby(&#39;A&#39;).sum()<\/p>\n<p>print(grouped)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001Scikit-Learn\u8fdb\u884c\u673a\u5668\u5b66\u4e60<\/h2>\n<\/p>\n<p><p>Scikit-Learn\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u673a\u5668\u5b66\u4e60\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u548c\u5de5\u5177\u3002\u53ef\u4ee5\u7528\u4e8e\u5206\u7c7b\u3001\u56de\u5f52\u3001\u805a\u7c7b\u3001\u964d\u7ef4\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>1. \u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u673a\u5668\u5b66\u4e60\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002Scikit-Learn\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u9884\u5904\u7406\u65b9\u6cd5\uff0c\u5982\u6807\u51c6\u5316\u3001\u5f52\u4e00\u5316\u3001\u7f3a\u5931\u503c\u586b\u8865\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([[1, 2], [3, 4], [5, 6]])<\/p>\n<h2><strong>\u6807\u51c6\u5316<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>scaled_data = scaler.fit_transform(data)<\/p>\n<p>print(scaled_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bad\u7ec3\u6a21\u578b<\/h3>\n<\/p>\n<p><p>Scikit-Learn\u63d0\u4f9b\u4e86\u591a\u79cd\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bad\u7ec3\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[1], [2], [3], [4], [5]])<\/p>\n<p>y = np.array([2, 3, 5, 7, 11])<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>predictions = model.predict(X)<\/p>\n<p>print(predictions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6a21\u578b\u8bc4\u4f30<\/h3>\n<\/p>\n<p><p>\u5728\u8bad\u7ec3\u5b8c\u6a21\u578b\u540e\uff0c\u9700\u8981\u5bf9\u6a21\u578b\u8fdb\u884c\u8bc4\u4f30\u3002Scikit-Learn\u63d0\u4f9b\u4e86\u591a\u79cd\u8bc4\u4f30\u65b9\u6cd5\uff0c\u5982\u51c6\u786e\u7387\u3001\u5747\u65b9\u8bef\u5dee\u3001R^2\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\u8bc4\u4f30\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import mean_squared_error, r2_score<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[1], [2], [3], [4], [5]])<\/p>\n<p>y = np.array([2, 3, 5, 7, 11])<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>predictions = model.predict(X)<\/p>\n<h2><strong>\u8ba1\u7b97\u5747\u65b9\u8bef\u5dee<\/strong><\/h2>\n<p>mse = mean_squared_error(y, predictions)<\/p>\n<p>print(mse)<\/p>\n<h2><strong>\u8ba1\u7b97R^2<\/strong><\/h2>\n<p>r2 = r2_score(y, predictions)<\/p>\n<p>print(r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c<strong>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u6570\u5b66\u5efa\u6a21\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u6570\u503c\u8ba1\u7b97\u3001\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u53ef\u89c6\u5316\u3001\u6570\u636e\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60<\/strong>\u3002\u901a\u8fc7\u5408\u7406\u5229\u7528\u8fd9\u4e9b\u5e93\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u5404\u79cd\u6570\u5b66\u5efa\u6a21\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u6570\u5b66\u5efa\u6a21\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u6570\u5b66\u5efa\u6a21\u901a\u5e38\u5305\u62ec\u95ee\u9898\u7684\u7406\u89e3\u4e0e\u63cf\u8ff0\u3001\u5efa\u7acb\u6570\u5b66\u6a21\u578b\u3001\u6c42\u89e3\u6a21\u578b\u4ee5\u53ca\u9a8c\u8bc1\u4e0e\u5206\u6790\u7ed3\u679c\u3002\u7406\u89e3\u95ee\u9898\u662f\u5173\u952e\uff0c\u63a5\u7740\u901a\u8fc7\u6570\u5b66\u65b9\u6cd5\u5c06\u5b9e\u9645\u95ee\u9898\u8f6c\u5316\u4e3a\u6570\u5b66\u8868\u8fbe\u5f0f\u3002\u5229\u7528Python\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u5e93\uff08\u5982NumPy\u3001SciPy\u548cPandas\uff09\u6765\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\uff0c\u4ee5\u6c42\u89e3\u6a21\u578b\u5e76\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<p><strong>\u54ea\u4e9bPython\u5e93\u9002\u5408\u7528\u4e8e\u6570\u5b66\u5efa\u6a21\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u7684\u5e93\u6765\u652f\u6301\u6570\u5b66\u5efa\u6a21\u5de5\u4f5c\u3002NumPy\u7528\u4e8e\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\uff0cPandas\u9002\u5408\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790\uff0cSciPy\u5305\u542b\u4e86\u8bb8\u591a\u6570\u5b66\u7b97\u6cd5\u548c\u4f18\u5316\u5de5\u5177\uff0cMatplotlib\u548cSeaborn\u5219\u7528\u4e8e\u53ef\u89c6\u5316\u7ed3\u679c\u3002\u8fd9\u4e9b\u5e93\u7684\u7ed3\u5408\u4f7f\u7528\u80fd\u591f\u5927\u5927\u7b80\u5316\u5efa\u6a21\u8fc7\u7a0b\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6570\u5b66\u6a21\u578b\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u6570\u5b66\u6a21\u578b\u9700\u8981\u6839\u636e\u5177\u4f53\u95ee\u9898\u7684\u6027\u8d28\u548c\u6570\u636e\u7279\u5f81\u6765\u51b3\u5b9a\u3002\u9996\u5148\uff0c\u5206\u6790\u95ee\u9898\u7684\u80cc\u666f\u548c\u9700\u6c42\uff0c\u8003\u8651\u4f7f\u7528\u7ebf\u6027\u6a21\u578b\u3001\u975e\u7ebf\u6027\u6a21\u578b\u3001\u52a8\u6001\u6a21\u578b\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u6a21\u578b\u3002\u53ef\u4ee5\u901a\u8fc7\u5bf9\u5df2\u6709\u6570\u636e\u8fdb\u884c\u63a2\u7d22\u6027\u5206\u6790\uff0c\u4e86\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\uff0c\u4ece\u800c\u9009\u62e9\u6700\u5408\u9002\u7684\u5efa\u6a21\u65b9\u6cd5\u3002\u4f7f\u7528Python\u7684\u53ef\u89c6\u5316\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u5e76\u505a\u51fa\u51b3\u7b56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6570\u5b66\u5efa\u6a21\u5982\u4f55\u7528Python\uff1f\u6570\u5b66\u5efa\u6a21\u7528Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\uff1a\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3001\u4f7f\u7528SciPy [&hellip;]","protected":false},"author":3,"featured_media":1153804,"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\/1153797"}],"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=1153797"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153797\/revisions"}],"predecessor-version":[{"id":1153806,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153797\/revisions\/1153806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153804"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}