{"id":1141346,"date":"2025-01-08T22:30:04","date_gmt":"2025-01-08T14:30:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141346.html"},"modified":"2025-01-08T22:30:06","modified_gmt":"2025-01-08T14:30:06","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%86%99%e4%b8%80%e4%b8%aa%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141346.html","title":{"rendered":"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104057\/6ef3105c-ac8c-40c5-be3c-82e75b7330a1.webp\" alt=\"\u5982\u4f55\u7528python\u5199\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u7528Python\u5199\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u5d4c\u5957\u5217\u8868\u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u800cNumPy\u548cPandas\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u521b\u5efa\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><h4>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u3002\u901a\u8fc7\u5c06\u5217\u8868\u5d4c\u5957\u5728\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u7d22\u5f15\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = array[0][1]  # \u7ed3\u679c\u662f2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fee\u6539\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array[0][1] = 10  # \u4fee\u6539\u540e\u6570\u7ec4\u53d8\u4e3a[[1, 10, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in array:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element, end=&#39; &#39;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u5728\u6027\u80fd\u4e0a\u6bd4\u5d4c\u5957\u5217\u8868\u66f4\u4f18\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u521b\u5efa\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684<code>array<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4e0e\u5d4c\u5957\u5217\u8868\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539NumPy\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = array[0, 1]  # \u7ed3\u679c\u662f2<\/p>\n<p>array[0, 1] = 10  # \u4fee\u6539\u540e\u6570\u7ec4\u53d8\u4e3a[[1, 10, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6570\u5b66\u8fd0\u7b97\u548c\u77e9\u9635\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u6570\u7ec4\u64cd\u4f5c\u7684\u51fd\u6570\uff0c\u5982\u77e9\u9635\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\u3002\u4f8b\u5982\uff0c\u77e9\u9635\u52a0\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array1 = np.array([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>])<\/p>\n<p>array2 = np.array([<\/p>\n<p>    [5, 6],<\/p>\n<p>    [7, 8]<\/p>\n<p>])<\/p>\n<p>result = array1 + array2  # \u7ed3\u679c\u662f[[6, 8], [10, 12]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u7528\u4e8e\u6570\u636e\u5904\u7406\u7684\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\uff0c\u5982DataFrame\u3002DataFrame\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u79cd\u4e8c\u7ef4\u6570\u7ec4\u7684\u6269\u5c55\uff0c\u5e26\u6709\u884c\u548c\u5217\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5Pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\uff08DataFrame\uff09<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684<code>DataFrame<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684DataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u8bbf\u95ee\u548c\u4fee\u6539DataFrame\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = df.at[0, &#39;B&#39;]  # \u7ed3\u679c\u662f4<\/p>\n<p>df.at[0, &#39;B&#39;] = 10  # \u4fee\u6539\u540eDataFrame\u53d8\u4e3a<\/p>\n<h2><strong>   A   B  C<\/strong><\/h2>\n<h2><strong>0  1  10  7<\/strong><\/h2>\n<h2><strong>1  2   5  8<\/strong><\/h2>\n<h2><strong>2  3   6  9<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u51fd\u6570\uff0c\u5982\u8fc7\u6ee4\u3001\u5206\u7ec4\u3001\u805a\u5408\u7b49\u3002\u4f8b\u5982\uff0c\u8fc7\u6ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filtered_df = df[df[&#39;A&#39;] &gt; 1]  # \u7ed3\u679c\u662f<\/p>\n<h2><strong>   A  B  C<\/strong><\/h2>\n<h2><strong>1  2  5  8<\/strong><\/h2>\n<h2><strong>2  3  6  9<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e8c\u7ef4\u6570\u7ec4\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4e8c\u7ef4\u6570\u7ec4\u5728\u8bb8\u591a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5982\u6570\u636e\u5206\u6790\u3001\u56fe\u50cf\u5904\u7406\u3001\u79d1\u5b66\u8ba1\u7b97\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u548c\u5904\u7406\u6570\u636e\u96c6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Pandas\u7684DataFrame\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u5904\u7406CSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\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>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u8fdb\u884c\u6570\u636e\u5206\u6790<\/strong><\/h2>\n<p>summary = df.describe()<\/p>\n<p>print(summary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u7070\u5ea6\u56fe\u50cf\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u8868\u793a\u4e00\u4e2a\u50cf\u7d20\u7684\u7070\u5ea6\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u8bfb\u53d6\u548c\u5904\u7406\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>image = Image.open(&#39;image.jpg&#39;).convert(&#39;L&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array(image)<\/p>\n<h2><strong>\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff08\u5982\u8fb9\u7f18\u68c0\u6d4b\uff09<\/strong><\/h2>\n<p>edges = np.where(array &gt; 128, 255, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u77e9\u9635\uff0c\u5e76\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>])<\/p>\n<p>matrix2 = np.array([<\/p>\n<p>    [5, 6],<\/p>\n<p>    [7, 8]<\/p>\n<p>])<\/p>\n<h2><strong>\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>result = np.dot(matrix1, matrix2)  # \u7ed3\u679c\u662f[[19, 22], [43, 50]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u4e8c\u7ef4\u6570\u7ec4\u64cd\u4f5c\u7684\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4e8c\u7ef4\u6570\u7ec4\u64cd\u4f5c\u7684\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528NumPy\u66ff\u4ee3\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><p>NumPy\u662f\u4e13\u4e3a\u6570\u7ec4\u64cd\u4f5c\u8bbe\u8ba1\u7684\uff0c\u5b83\u5728\u6027\u80fd\u4e0a\u6bd4\u5d4c\u5957\u5217\u8868\u66f4\u4f18\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u66ff\u4ee3\u5d4c\u5957\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u907f\u514d\u9891\u7e41\u7684\u6570\u7ec4\u590d\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u6570\u7ec4\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u9891\u7e41\u7684\u6570\u7ec4\u590d\u5236\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u7684\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u907f\u514d\u590d\u5236\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<h2><strong>\u83b7\u53d6\u5b50\u6570\u7ec4\uff08\u4e0d\u8fdb\u884c\u590d\u5236\uff09<\/strong><\/h2>\n<p>sub_array = array[0:2, 0:2]  # \u7ed3\u679c\u662f[[1, 2], [4, 5]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u663e\u5f0f\u5faa\u73af\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6570\u7ec4\u8fd0\u7b97\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u65b9\uff08\u5411\u91cf\u5316\u64cd\u4f5c\uff09<\/strong><\/h2>\n<p>squared_array = array  2  # \u7ed3\u679c\u662f[[1, 4, 9], [16, 25, 36], [49, 64, 81]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5e76\u884c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u7684<code>vectorize<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6807\u91cf\u51fd\u6570\u8f6c\u6362\u4e3a\u5411\u91cf\u51fd\u6570\uff0c\u5e76\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u6807\u91cf\u51fd\u6570<\/strong><\/h2>\n<p>def scalar_function(x):<\/p>\n<p>    return x  2<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5411\u91cf\u51fd\u6570<\/strong><\/h2>\n<p>vectorized_function = np.vectorize(scalar_function)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<h2><strong>\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97<\/strong><\/h2>\n<p>result = vectorized_function(array)  # \u7ed3\u679c\u662f[[1, 4, 9], [16, 25, 36], [49, 64, 81]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4f7f\u7528Python\u521b\u5efa\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u5d4c\u5957\u5217\u8868\u3001NumPy\u548cPandas\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u63d0\u9ad8\u6548\u7387\u548c\u6027\u80fd\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u662f\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\u5185\u5305\u542b\u5176\u4ed6\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u884c\u4e09\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u66f4\u9ad8\u6548\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u65b9\u6cd5\u662f\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u3002<\/p>\n<p><strong>\u6211\u5e94\u8be5\u9009\u62e9\u4f7f\u7528\u5217\u8868\u8fd8\u662fNumPy\u6570\u7ec4\u6765\u5b9e\u73b0\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u4f7f\u7528\u5217\u8868\u9002\u7528\u4e8e\u7b80\u5355\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u53ea\u9700\u8981\u57fa\u672c\u7684\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\u65f6\u3002\u82e5\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u6216\u5904\u7406\u5927\u91cf\u6570\u636e\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5177\u6709\u66f4\u5feb\u7684\u6027\u80fd\u548c\u4e30\u5bcc\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002NumPy\u8fd8\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u4f7f\u5176\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u66f4\u4e3a\u5f3a\u5927\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>array[0][1]<\/code> \u5c06\u8fd4\u56de\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\u3002\u5982\u679c\u8981\u4fee\u6539\u8be5\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff0c\u5982<code>array[0][1] = 10<\/code>\u3002\u5728\u4f7f\u7528NumPy\u65f6\uff0c\u8bbf\u95ee\u548c\u4fee\u6539\u7684\u65b9\u5f0f\u76f8\u4f3c\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u4f8b\u5982<code>array[0, 1]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5199\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u3002 \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u5d4c\u5957\u5217\u8868\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1141352,"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\/1141346"}],"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=1141346"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141346\/revisions"}],"predecessor-version":[{"id":1141357,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141346\/revisions\/1141357"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141352"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}