{"id":1124544,"date":"2025-01-08T19:42:37","date_gmt":"2025-01-08T11:42:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1124544.html"},"modified":"2025-01-08T19:42:40","modified_gmt":"2025-01-08T11:42:40","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e4%b8%80%e4%b8%aa%e5%a4%8d%e6%9d%82%e7%9a%84%e5%85%ac%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1124544.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u516c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085538\/06c4e142-2cef-4def-bf59-dca4fb093ea3.webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u516c\u5f0f\" \/><\/p>\n<p><p> <strong>\u5f00\u5934\u6bb5\u843d:<\/strong><\/p>\n<\/p>\n<p><p><strong>\u7528Python\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u516c\u5f0f\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u6570\u636e\u3001\u4f18\u5316\u8ba1\u7b97\u6027\u80fd\u3002<\/strong> \u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3aPython\u62e5\u6709\u4e30\u5bcc\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5982NumPy\u3001SciPy\u548cSymPy\u7b49\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u5b66\u8ba1\u7b97\u529f\u80fd\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u590d\u6742\u516c\u5f0f\u7684\u5b9e\u73b0\u8fc7\u7a0b\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u5982\u4f55\u7528Python\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u516c\u5f0f<\/strong><\/h2>\n<p><p>\u8ba1\u7b97\u590d\u6742\u516c\u5f0f\u662f\u6570\u636e\u79d1\u5b66\u3001\u5de5\u7a0b\u548c\u7ecf\u6d4e\u5b66\u7b49\u9886\u57df\u5e38\u89c1\u7684\u4efb\u52a1\u3002Python\u56e0\u5176\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u548c\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u6210\u4e3a\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u7684\u7406\u60f3\u9009\u62e9\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u7528Python\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u516c\u5f0f\uff0c\u6db5\u76d6\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u6570\u636e\u4ee5\u53ca\u4f18\u5316\u8ba1\u7b97\u6027\u80fd\u7b49\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h2>\n<\/p>\n<p><p>Python\u62e5\u6709\u591a\u4e2a\u4e13\u95e8\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u9002\u7528\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u6210\u529f\u8ba1\u7b97\u590d\u6742\u516c\u5f0f\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h3>1.1 NumPy<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u7684\u4e00\u4e2a\u57fa\u7840\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u5b83\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u6267\u884c\u5404\u79cd\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u516c\u5f0f<\/strong><\/h2>\n<p>a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([4, 5, 6])<\/p>\n<p>result = np.dot(a, b) + np.sin(a)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 SciPy<\/h3>\n<\/p>\n<p><p>SciPy\u662f\u57fa\u4e8eNumPy\u6784\u5efa\u7684\u4e00\u4e2a\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u8ba1\u7b97\u529f\u80fd\uff0c\u5305\u62ec\u79ef\u5206\u3001\u4f18\u5316\u3001\u7ebf\u6027\u4ee3\u6570\u548c\u7edf\u8ba1\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import integrate<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u8ba1\u7b97\u4e00\u4e2a\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>result = integrate.quad(lambda x: x2, 0, 1)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3 SymPy<\/h3>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2a\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7b26\u53f7\u8fd0\u7b97\u7684\u573a\u666f\u3002\u5b83\u53ef\u4ee5\u8fdb\u884c\u4ee3\u6570\u7b80\u5316\u3001\u5fae\u79ef\u5206\u3001\u65b9\u7a0b\u6c42\u89e3\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u7b26\u53f7\u8ba1\u7b97<\/strong><\/h2>\n<p>x = sp.symbols(&#39;x&#39;)<\/p>\n<p>expr = x2 + 2*x + 1<\/p>\n<p>simplified_expr = sp.simplify(expr)<\/p>\n<p>print(simplified_expr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5b9a\u4e49\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u662f\u8ba1\u7b97\u590d\u6742\u516c\u5f0f\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u516c\u5f0f\u5c01\u88c5\u8d77\u6765\uff0c\u4fbf\u4e8e\u590d\u7528\u548c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u4f7f\u7528NumPy\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u6765\u5b9a\u4e49\u590d\u6742\u7684\u6570\u5b66\u516c\u5f0f\uff0c\u5e76\u901a\u8fc7\u4f20\u9012\u6570\u7ec4\u6765\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def complex_formula(x):<\/p>\n<p>    return np.sin(x) + np.log(x) + x2<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u8ba1\u7b97\u590d\u6742\u516c\u5f0f<\/strong><\/h2>\n<p>x = np.array([1, 2, 3])<\/p>\n<p>result = complex_formula(x)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u4f7f\u7528SciPy\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>SciPy\u53ef\u4ee5\u4e0eNumPy\u7ed3\u5408\u4f7f\u7528\uff0c\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u8ba1\u7b97\uff0c\u5982\u6c42\u89e3\u5fae\u5206\u65b9\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.integrate import odeint<\/p>\n<p>def model(y, t):<\/p>\n<p>    dydt = -y + t<\/p>\n<p>    return dydt<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u6c42\u89e3\u5fae\u5206\u65b9\u7a0b<\/strong><\/h2>\n<p>y0 = 0<\/p>\n<p>t = np.linspace(0, 5, 100)<\/p>\n<p>solution = odeint(model, y0, t)<\/p>\n<p>print(solution)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3 \u4f7f\u7528SymPy\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>SymPy\u5141\u8bb8\u5b9a\u4e49\u7b26\u53f7\u51fd\u6570\uff0c\u5e76\u8fdb\u884c\u7b26\u53f7\u8fd0\u7b97\uff0c\u5982\u5fae\u5206\u548c\u79ef\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<p>x = sp.symbols(&#39;x&#39;)<\/p>\n<p>expr = sp.sin(x) + sp.log(x) + x2<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u6c42\u5bfc\u6570<\/strong><\/h2>\n<p>derivative = sp.diff(expr, x)<\/p>\n<p>print(derivative)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5904\u7406\u8f93\u5165\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5904\u7406\u8f93\u5165\u6570\u636e\u662f\u8ba1\u7b97\u590d\u6742\u516c\u5f0f\u7684\u524d\u63d0\u3002\u8f93\u5165\u6570\u636e\u7684\u8d28\u91cf\u548c\u683c\u5f0f\u76f4\u63a5\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u590d\u6742\u516c\u5f0f\u8ba1\u7b97\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u5982\u53bb\u9664\u5f02\u5e38\u503c\u3001\u5f52\u4e00\u5316\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u6570\u636e\u9884\u5904\u7406<\/strong><\/h2>\n<p>data = np.array([1, 2, 3, 4, 5, -999])<\/p>\n<p>data = data[data &gt; 0]  # \u53bb\u9664\u5f02\u5e38\u503c<\/p>\n<p>data = (data - np.mean(data)) \/ np.std(data)  # \u5f52\u4e00\u5316<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u6570\u636e\u683c\u5f0f\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u8f93\u5165\u6570\u636e\u7684\u683c\u5f0f\u4e5f\u9700\u8981\u6839\u636e\u8ba1\u7b97\u7684\u9700\u6c42\u8fdb\u884c\u8f6c\u6362\uff0c\u5982\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\u6216\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u6570\u636e\u683c\u5f0f\u8f6c\u6362<\/strong><\/h2>\n<p>list_data = [1, 2, 3, 4, 5]<\/p>\n<p>array_data = np.array(list_data)<\/p>\n<p>print(array_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f18\u5316\u8ba1\u7b97\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u8ba1\u7b97\u590d\u6742\u516c\u5f0f\u901a\u5e38\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u56e0\u6b64\u4f18\u5316\u8ba1\u7b97\u6027\u80fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u7528\u7684\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u5411\u91cf\u5316\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u5411\u91cf\u5316\u8fd0\u7b97\u662f\u5229\u7528NumPy\u6570\u7ec4\u7684\u7279\u6027\uff0c\u901a\u8fc7\u4e00\u6b21\u6027\u5904\u7406\u6574\u4e2a\u6570\u7ec4\u6765\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u5411\u91cf\u5316\u8fd0\u7b97<\/strong><\/h2>\n<p>x = np.array([1, 2, 3, 4, 5])<\/p>\n<p>result = np.sin(x) + np.log(x) + x2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u5e76\u884c\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8ba1\u7b97\u91cf\u975e\u5e38\u5927\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u63d0\u9ad8\u6548\u7387\u3002Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u5982multiprocessing\u548cjoblib\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>def complex_calculation(x):<\/p>\n<p>    return x2 + 2*x + 1<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u5e76\u884c\u8ba1\u7b97<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>with Pool(4) as p:<\/p>\n<p>    result = p.map(complex_calculation, data)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.3 \u4f7f\u7528Cython<\/h3>\n<\/p>\n<p><p>Cython\u662f\u4e00\u4e2a\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\u4ee3\u7801\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528Cython\u52a0\u901f\u8ba1\u7b97<\/p>\n<h2><strong>\u9700\u8981\u5728\u72ec\u7acb\u7684 .pyx \u6587\u4ef6\u4e2d\u7f16\u5199\u4ee3\u7801\uff0c\u7136\u540e\u7f16\u8bd1<\/strong><\/h2>\n<h2><strong>complex_calculation.pyx<\/strong><\/h2>\n<p>def complex_calculation(x):<\/p>\n<p>    return x2 + 2*x + 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u9645\u6848\u4f8b\u5206\u6790<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7528Python\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u6848\u4f8b\u80cc\u666f<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u7269\u7406\u516c\u5f0f\uff0c\u8be5\u516c\u5f0f\u6d89\u53ca\u591a\u4e2a\u53d8\u91cf\u548c\u5e38\u6570\uff0c\u5e76\u4e14\u9700\u8981\u8fdb\u884c\u6570\u503c\u79ef\u5206\u3002<\/p>\n<\/p>\n<p><h3>5.2 \u9009\u62e9\u5408\u9002\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9009\u62e9NumPy\u548cSciPy\u6765\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u548c\u9ad8\u6548\u7684\u8ba1\u7b97\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy import integrate<\/p>\n<h2><strong>\u5b9a\u4e49\u590d\u6742\u516c\u5f0f<\/strong><\/h2>\n<p>def complex_formula(x, a, b, c):<\/p>\n<p>    return a * np.sin(b * x) + c * np.log(x)<\/p>\n<h2><strong>\u5b9a\u4e49\u79ef\u5206\u8303\u56f4\u548c\u5e38\u6570<\/strong><\/h2>\n<p>a, b, c = 1, 2, 3<\/p>\n<p>x_min, x_max = 0.1, 10<\/p>\n<h2><strong>\u8ba1\u7b97\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>result, error = integrate.quad(complex_formula, x_min, x_max, args=(a, b, c))<\/p>\n<p>print(f&quot;Integral result: {result}, Error: {error}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u590d\u6742\u516c\u5f0f\u5c01\u88c5\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u4fbf\u4e8e\u590d\u7528\u548c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy import integrate<\/p>\n<p>def complex_formula(x, a, b, c):<\/p>\n<p>    return a * np.sin(b * x) + c * np.log(x)<\/p>\n<p>def calculate_integral(a, b, c, x_min, x_max):<\/p>\n<p>    result, error = integrate.quad(complex_formula, x_min, x_max, args=(a, b, c))<\/p>\n<p>    return result, error<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u8ba1\u7b97\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>a, b, c = 1, 2, 3<\/p>\n<p>x_min, x_max = 0.1, 10<\/p>\n<p>result, error = calculate_integral(a, b, c, x_min, x_max)<\/p>\n<p>print(f&quot;Integral result: {result}, Error: {error}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.4 \u5904\u7406\u8f93\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u548c\u683c\u5f0f\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u6570\u636e\u9884\u5904\u7406<\/strong><\/h2>\n<p>data = np.array([1, 2, 3, 4, 5, -999])<\/p>\n<p>data = data[data &gt; 0]  # \u53bb\u9664\u5f02\u5e38\u503c<\/p>\n<p>data = (data - np.mean(data)) \/ np.std(data)  # \u5f52\u4e00\u5316<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.5 \u4f18\u5316\u8ba1\u7b97\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5411\u91cf\u5316\u8fd0\u7b97\u548c\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u5411\u91cf\u5316\u8fd0\u7b97<\/strong><\/h2>\n<p>x = np.array([1, 2, 3, 4, 5])<\/p>\n<p>result = np.sin(x) + np.log(x) + x2<\/p>\n<p>print(result)<\/p>\n<p>from multiprocessing import Pool<\/p>\n<p>def complex_calculation(x):<\/p>\n<p>    return x2 + 2*x + 1<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u5e76\u884c\u8ba1\u7b97<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5]<\/p>\n<p>with Pool(4) as p:<\/p>\n<p>    result = p.map(complex_calculation, data)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.6 \u6848\u4f8b\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u7528Python\u8ba1\u7b97\u4e86\u4e00\u4e2a\u590d\u6742\u7684\u7269\u7406\u516c\u5f0f\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u6570\u636e\u548c\u4f18\u5316\u8ba1\u7b97\u6027\u80fd\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u7528Python\u8ba1\u7b97\u590d\u6742\u516c\u5f0f\u5e76\u4e0d\u4ec5\u9650\u4e8e\u4ee5\u4e0a\u5185\u5bb9\u3002Python\u7684\u751f\u6001\u7cfb\u7edf\u4e2d\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u5f3a\u5927\u7684\u5e93\u548c\u5de5\u5177\uff0c\u5982Pandas\u3001Matplotlib\u3001TensorFlow\u7b49\uff0c\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u516c\u5f0f\u7684\u8ba1\u7b97\u548c\u5206\u6790\u3002\u5173\u952e\u5728\u4e8e\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u5e76\u4e0d\u65ad\u4f18\u5316\u548c\u6539\u8fdb\u8ba1\u7b97\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4f7f\u7528Python\u8ba1\u7b97\u590d\u6742\u516c\u5f0f\u65f6\uff0c\u5e94\u8be5\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8ba1\u7b97\u590d\u6742\u7684\u6570\u5b66\u516c\u5f0f\u3002\u6700\u5e38\u7528\u7684\u5305\u62ecNumPy\u548cSymPy\u3002NumPy\u4e13\u6ce8\u4e8e\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u9002\u5408\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u800cSymPy\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u9002\u5408\u5904\u7406\u4ee3\u6570\u8868\u8fbe\u5f0f\u548c\u7b26\u53f7\u65b9\u7a0b\u3002\u5982\u679c\u60a8\u9700\u8981\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\uff0cSymPy\u662f\u7406\u60f3\u7684\u9009\u62e9\uff1b\u5982\u679c\u662f\u6570\u503c\u8ba1\u7b97\uff0cNumPy\u5219\u66f4\u4e3a\u9002\u5408\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u516c\u5f0f\u4e2d\u7684\u53d8\u91cf\u548c\u5e38\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u53d8\u91cf\u6765\u5904\u7406\u516c\u5f0f\u4e2d\u7684\u5e38\u91cf\u548c\u53d8\u91cf\u3002\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\uff0c\u4f8b\u5982<code>a = 5<\/code>\uff0c<code>b = 10<\/code>\u3002\u5728\u590d\u6742\u7684\u516c\u5f0f\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5c06\u53d8\u91cf\u76f4\u63a5\u5d4c\u5165\u516c\u5f0f\u4e2d\uff0c\u6bd4\u5982<code>result = a * b + (a ** 2) \/ b<\/code>\u3002\u4e3a\u4e86\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5efa\u8bae\u5c06\u516c\u5f0f\u5206\u89e3\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u9010\u6b65\u8ba1\u7b97\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u7406\u89e3\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u63a8\u8350\u7684\u5728\u7ebf\u5de5\u5177\u6216IDE\u6765\u5e2e\u52a9\u7f16\u5199\u548c\u6d4b\u8bd5\u590d\u6742\u516c\u5f0f\uff1f<\/strong><br \/>\u8bb8\u591a\u5728\u7ebf\u5e73\u53f0\u548c\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7f16\u5199\u548c\u6d4b\u8bd5Python\u4ee3\u7801\uff0c\u5305\u62ecJupyter Notebook\u3001Google Colab\u548cPyCharm\u3002Jupyter Notebook\u5141\u8bb8\u60a8\u4ee5\u4ea4\u4e92\u65b9\u5f0f\u8fd0\u884c\u4ee3\u7801\uff0c\u5e76\u4e14\u53ef\u4ee5\u76f4\u89c2\u5730\u5c55\u793a\u8ba1\u7b97\u7ed3\u679c\uff0c\u9002\u5408\u8fdb\u884c\u63a2\u7d22\u6027\u7f16\u7a0b\u3002Google Colab\u5219\u662f\u4e00\u4e2a\u514d\u8d39\u7684\u5728\u7ebf\u73af\u5883\uff0c\u652f\u6301Python\u5e76\u63d0\u4f9b\u4e91\u7aef\u8ba1\u7b97\u80fd\u529b\u3002\u800cPyCharm\u5219\u662f\u529f\u80fd\u5f3a\u5927\u7684\u672c\u5730IDE\uff0c\u9002\u5408\u9700\u8981\u8f83\u9ad8\u5f00\u53d1\u6548\u7387\u7684\u7528\u6237\u3002\u9009\u62e9\u9002\u5408\u60a8\u7684\u5de5\u5177\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7f16\u7a0b\u4f53\u9a8c\u548c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: \u7528Python\u8ba1\u7b97\u4e00\u4e2a\u590d\u6742\u7684\u516c\u5f0f\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u6570\u636e\u3001\u4f18\u5316\u8ba1\u7b97\u6027\u80fd [&hellip;]","protected":false},"author":3,"featured_media":1124552,"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\/1124544"}],"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=1124544"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124544\/revisions"}],"predecessor-version":[{"id":1124554,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124544\/revisions\/1124554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1124552"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1124544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1124544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1124544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}