{"id":963129,"date":"2024-12-27T04:14:46","date_gmt":"2024-12-26T20:14:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/963129.html"},"modified":"2024-12-27T04:14:49","modified_gmt":"2024-12-26T20:14:49","slug":"python%e7%9f%a9%e9%98%b5%e5%be%88%e5%a4%a7%e5%a6%82%e4%bd%95%e8%b0%83%e8%af%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/963129.html","title":{"rendered":"python\u77e9\u9635\u5f88\u5927\u5982\u4f55\u8c03\u8bd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180544\/0d3eb15b-046f-4c2b-bc26-254b7cda2e5b.webp\" alt=\"python\u77e9\u9635\u5f88\u5927\u5982\u4f55\u8c03\u8bd5\" \/><\/p>\n<p><p> <strong>\u8c03\u8bd5\u5927\u578bPython\u77e9\u9635\u7684\u6709\u6548\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u5b58\u4f18\u5316\u5e93\u3001\u5206\u5757\u5904\u7406\u77e9\u9635\u3001\u53ef\u89c6\u5316\u5de5\u5177\u3001\u7b80\u5316\u548c\u62bd\u8c61\u4ee3\u7801\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u5b58\u4f18\u5316\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u5f71\u54cd\u6027\u80fd\u7684\u60c5\u51b5\u4e0b\u5904\u7406\u5927\u89c4\u6a21\u77e9\u9635\u3002\u6bd4\u5982\u4f7f\u7528NumPy\u548cPandas\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u77e9\u9635\u7684\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5185\u5b58\u4f18\u5316\u5e93\u662f\u5904\u7406\u5927\u578b\u77e9\u9635\u7684\u4e00\u4e2a\u6781\u5176\u91cd\u8981\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u6765\u6709\u6548\u5730\u5b58\u50a8\u548c\u8ba1\u7b97\u5927\u89c4\u6a21\u6570\u636e\u3002NumPy\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86n\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u6267\u884c\u6570\u5b66\u64cd\u4f5c\u3002NumPy\u6570\u7ec4\u6bd4Python\u7684\u5185\u7f6e\u5217\u8868\u66f4\u8282\u7701\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u56fa\u5b9a\u5927\u5c0f\u7684\uff0c\u5e76\u4e14\u6240\u6709\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u76f8\u540c\u3002\u6b64\u5916\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u6570\u7ec4\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u662f\u4ee5\u5e95\u5c42C\u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u56e0\u6b64\u901f\u5ea6\u975e\u5e38\u5feb\u3002\u5bf9\u4e8e\u8d85\u5927\u578b\u6570\u636e\u96c6\uff0cDask\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u5b58\u4f18\u5316\u5e93<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5185\u5b58\u4f18\u5316\u5e93\u662f\u5904\u7406\u5927\u578b\u77e9\u9635\u7684\u6700\u6709\u6548\u65b9\u6cd5\u4e4b\u4e00\u3002Python\u7684\u79d1\u5b66\u8ba1\u7b97\u751f\u6001\u7cfb\u7edf\u4e2d\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>NumPy<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u6700\u57fa\u7840\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u5176\u6838\u5fc3\u662f\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff08ndarray\uff09\u3002NumPy\u6570\u7ec4\u901a\u5e38\u6bd4Python\u5217\u8868\u66f4\u8282\u7701\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u56fa\u5b9a\u5927\u5c0f\u7684\uff0c\u5e76\u4e14\u6240\u6709\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5927\u578b\u77e9\u9635\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u6d88\u8017\u3002<\/p>\n<\/p>\n<p><p>NumPy\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u77e9\u9635\u64cd\u4f5c\u7684\u51fd\u6570\uff0c\u6bd4\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\u3002\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u662f\u4ee5\u5e95\u5c42C\u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u56e0\u6b64\u901f\u5ea6\u975e\u5e38\u5feb\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u5bf9\u4e00\u4e2a\u5927\u578b\u77e9\u9635\u6267\u884c\u591a\u6b21\u76f8\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\uff0c\u4f7f\u7528NumPy\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(10000, 10000)<\/p>\n<h2><strong>\u6267\u884c\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>result = np.dot(large_matrix, large_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>SciPy<\/strong><\/p>\n<\/p>\n<p><p>SciPy\u662f\u57fa\u4e8eNumPy\u6784\u5efa\u7684\uff0c\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u6570\u5b66\u51fd\u6570\u548c\u7b97\u6cd5\uff0c\u7279\u522b\u9002\u5408\u4e8e\u66f4\u590d\u6742\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u5982\u7a00\u758f\u77e9\u9635\u8fd0\u7b97\u3001\u4fe1\u53f7\u5904\u7406\u3001\u7ebf\u6027\u4ee3\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u7a00\u758f\u77e9\u9635\uff0c\u4f7f\u7528SciPy\u7684\u7a00\u758f\u77e9\u9635\u6a21\u5757\u975e\u5e38\u6709\u5e2e\u52a9\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u6781\u5927\u5730\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u5e76\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.sparse import csr_matrix<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a00\u758f\u77e9\u9635<\/strong><\/h2>\n<p>sparse_matrix = csr_matrix((3, 4), dtype=np.int8)<\/p>\n<h2><strong>\u6267\u884c\u7a00\u758f\u77e9\u9635\u7684\u4e58\u6cd5<\/strong><\/h2>\n<p>result = sparse_matrix.dot(sparse_matrix.T)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Pandas<\/strong><\/p>\n<\/p>\n<p><p>Pandas\u662f\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u77e9\u9635\u6570\u636e\u3002\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0cPandas\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u7b80\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>Pandas\u7684DataFrame\u5bf9\u8c61\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u4e8c\u7ef4\u7684\u5927\u5c0f\u53ef\u53d8\u3001\u5e26\u6807\u7b7e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u6570\u636e\u5e93\u8868\u3002\u901a\u8fc7\u4f7f\u7528Pandas\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u5927\u578b\u77e9\u9635\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u7b5b\u9009\u3001\u6c47\u603b\u3001\u5206\u7ec4\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u6570\u636e\u6846<\/strong><\/h2>\n<p>large_df = pd.DataFrame(np.random.rand(10000, 10000))<\/p>\n<h2><strong>\u6267\u884c\u6570\u636e\u6846\u7684\u64cd\u4f5c<\/strong><\/h2>\n<p>result = large_df.sum()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Dask<\/strong><\/p>\n<\/p>\n<p><p>Dask\u662f\u4e00\u4e2a\u5e76\u884c\u8ba1\u7b97\u5e93\uff0c\u4e13\u4e3a\u5904\u7406\u5927\u6570\u636e\u96c6\u800c\u8bbe\u8ba1\u3002Dask\u5141\u8bb8\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u5b83\u4eec\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u5bf9\u4e8e\u8d85\u5927\u578b\u77e9\u9635\uff0cDask\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f88\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><p>Dask\u7684\u6570\u7ec4\u548c\u6570\u636e\u6846\u4e0eNumPy\u548cPandas\u7684\u63a5\u53e3\u76f8\u4f3c\uff0c\u56e0\u6b64\u5f88\u5bb9\u6613\u4e0a\u624b\u3002\u901a\u8fc7\u4f7f\u7528Dask\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u5355\u673a\u6216\u96c6\u7fa4\u4e0a\u5904\u7406\u5927\u578b\u77e9\u9635\uff0c\u5e76\u5229\u7528\u591a\u6838CPU\u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.array as da<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578bDask\u6570\u7ec4<\/strong><\/h2>\n<p>large_dask_array = da.random.random((10000, 10000), chunks=(1000, 1000))<\/p>\n<h2><strong>\u6267\u884cDask\u6570\u7ec4\u7684\u64cd\u4f5c<\/strong><\/h2>\n<p>result = large_dask_array.sum().compute()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5206\u5757\u5904\u7406\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u578b\u77e9\u9635\uff0c\u5206\u5757\u5904\u7406\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002\u5206\u5757\u5904\u7406\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u5927\u578b\u77e9\u9635\u5206\u5272\u6210\u8f83\u5c0f\u7684\u5757\uff0c\u7136\u540e\u9010\u5757\u5904\u7406\uff0c\u8fd9\u6837\u53ef\u4ee5\u964d\u4f4e\u5185\u5b58\u5360\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5206\u5757\u5904\u7406\u7684\u6982\u5ff5<\/strong><\/p>\n<\/p>\n<p><p>\u5206\u5757\u5904\u7406\u662f\u4e00\u79cd\u5c06\u5927\u4efb\u52a1\u5206\u89e3\u4e3a\u5c0f\u4efb\u52a1\u7684\u65b9\u6cd5\u3002\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u53ef\u4ee5\u5c06\u77e9\u9635\u5206\u5272\u4e3a\u8f83\u5c0f\u7684\u5b50\u77e9\u9635\uff08\u5757\uff09\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u5b50\u77e9\u9635\u5355\u72ec\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u77e9\u9635\u5230\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u51cf\u5c0f\u5185\u5b58\u538b\u529b\u3002<\/p>\n<\/p>\n<p><p>\u5206\u5757\u5904\u7406\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u53ef\u4ee5\u5229\u7528\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u7f13\u5b58\u673a\u5236\u6765\u63d0\u9ad8\u6548\u7387\u3002\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u5c42\u6b21\u7ed3\u6784\u662f\u5206\u5c42\u7684\uff0c\u8bbf\u95ee\u7f13\u5b58\u7684\u901f\u5ea6\u8fdc\u5feb\u4e8e\u8bbf\u95ee\u4e3b\u5185\u5b58\u3002\u901a\u8fc7\u5206\u5757\u5904\u7406\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u7f13\u5b58\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5982\u4f55\u5b9e\u73b0\u5206\u5757\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5b9e\u73b0\u5206\u5757\u5904\u7406\u7684\u57fa\u672c\u601d\u8def\u662f\u5c06\u5927\u578b\u77e9\u9635\u5206\u5272\u4e3a\u591a\u4e2a\u8f83\u5c0f\u7684\u5b50\u77e9\u9635\uff0c\u7136\u540e\u9010\u5757\u8fdb\u884c\u8ba1\u7b97\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u8fed\u4ee3\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u501f\u52a9\u4e00\u4e9b\u5e93\u6765\u7b80\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u5b9e\u73b0\u5206\u5757\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6765\u5904\u7406\u77e9\u9635\u7684\u6bcf\u4e00\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(10000, 10000)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5757\u7684\u5927\u5c0f<\/strong><\/h2>\n<p>block_size = 1000<\/p>\n<h2><strong>\u5bf9\u77e9\u9635\u8fdb\u884c\u5206\u5757\u5904\u7406<\/strong><\/h2>\n<p>for i in range(0, large_matrix.shape[0], block_size):<\/p>\n<p>    for j in range(0, large_matrix.shape[1], block_size):<\/p>\n<p>        # \u63d0\u53d6\u5b50\u77e9\u9635<\/p>\n<p>        sub_matrix = large_matrix[i:i+block_size, j:j+block_size]<\/p>\n<p>        # \u5bf9\u5b50\u77e9\u9635\u8fdb\u884c\u64cd\u4f5c<\/p>\n<p>        # \u4f8b\u5982\uff0c\u8ba1\u7b97\u5b50\u77e9\u9635\u7684\u548c<\/p>\n<p>        sub_sum = np.sum(sub_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Dask\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5206\u5757\u5904\u7406\u3002Dask\u7684\u6570\u7ec4\u5bf9\u8c61\u672c\u8eab\u5c31\u662f\u5206\u5757\u7684\uff0c\u56e0\u6b64\u5728\u4f7f\u7528Dask\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5206\u5757\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.array as da<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578bDask\u6570\u7ec4<\/strong><\/h2>\n<p>large_dask_array = da.random.random((10000, 10000), chunks=(1000, 1000))<\/p>\n<h2><strong>\u5bf9Dask\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c<\/strong><\/h2>\n<h2><strong>\u4f8b\u5982\uff0c\u8ba1\u7b97\u6570\u7ec4\u7684\u548c<\/strong><\/h2>\n<p>result = large_dask_array.map_blocks(np.sum, dtype=float).compute()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u53ef\u89c6\u5316\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u76f4\u89c2\u5730\u7406\u89e3\u5927\u578b\u77e9\u9635\u7684\u6570\u636e\u7ed3\u6784\u548c\u5185\u5bb9\u3002\u901a\u8fc7\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u5feb\u901f\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u6a21\u5f0f\u3001\u5f02\u5e38\u503c\u548c\u5176\u4ed6\u91cd\u8981\u7279\u5f81\uff0c\u4ece\u800c\u66f4\u6709\u6548\u5730\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Matplotlib<\/strong><\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u53ef\u89c6\u5316\u5927\u578b\u77e9\u9635\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>imshow<\/code>\u51fd\u6570\u6765\u663e\u793a\u77e9\u9635\u7684\u56fe\u50cf\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\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(100, 100)<\/p>\n<h2><strong>\u53ef\u89c6\u5316\u77e9\u9635<\/strong><\/h2>\n<p>plt.imshow(large_matrix, cmap=&#39;viridis&#39;, aspect=&#39;auto&#39;)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Matplotlib\u7684<code>imshow<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u4e8c\u7ef4\u6570\u636e\u7684\u56fe\u50cf\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u6620\u5c04\uff08cmap\uff09\uff0c\u53ef\u4ee5\u7a81\u51fa\u663e\u793a\u6570\u636e\u4e2d\u7684\u6a21\u5f0f\u548c\u7279\u5f81\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>aspect<\/code>\u53c2\u6570\u6765\u63a7\u5236\u56fe\u50cf\u7684\u957f\u5bbd\u6bd4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Seaborn<\/strong><\/p>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u6784\u5efa\u7684\u9ad8\u7ea7\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u548c\u66f4\u7f8e\u89c2\u7684\u56fe\u8868\u3002\u5bf9\u4e8e\u77e9\u9635\u6570\u636e\uff0cSeaborn\u63d0\u4f9b\u4e86<code>heatmap<\/code>\u51fd\u6570\u6765\u751f\u6210\u70ed\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(100, 100)<\/p>\n<h2><strong>\u53ef\u89c6\u5316\u77e9\u9635<\/strong><\/h2>\n<p>sns.heatmap(large_matrix, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Seaborn\u7684<code>heatmap<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u8bb8\u591a\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u53ef\u4ee5\u8f7b\u677e\u8c03\u6574\u56fe\u8868\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>cmap<\/code>\u53c2\u6570\u6765\u66f4\u6539\u989c\u8272\u6620\u5c04\uff0c\u901a\u8fc7\u8bbe\u7f6e<code>annot=True<\/code>\u6765\u663e\u793a\u6bcf\u4e2a\u5355\u5143\u683c\u7684\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Plotly<\/strong><\/p>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u5408\u4e8e\u9700\u8981\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u573a\u5408\u3002\u5bf9\u4e8e\u5927\u578b\u77e9\u9635\u6570\u636e\uff0cPlotly\u7684<code>heatmap<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4ea4\u4e92\u5f0f\u70ed\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(100, 100)<\/p>\n<h2><strong>\u53ef\u89c6\u5316\u77e9\u9635<\/strong><\/h2>\n<p>fig = px.imshow(large_matrix, color_continuous_scale=&#39;viridis&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Plotly\u7684<code>imshow<\/code>\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u653e\u5927\u3001\u7f29\u5c0f\u3001\u5e73\u79fb\u56fe\u8868\u3002\u8fd9\u5bf9\u4e8e\u63a2\u7d22\u5927\u578b\u77e9\u9635\u6570\u636e\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u67e5\u770b\u6570\u636e\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u7b80\u5316\u548c\u62bd\u8c61\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u7b80\u5316\u548c\u62bd\u8c61\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51fd\u6570\u5316\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u91cd\u590d\u7684\u4ee3\u7801\u63d0\u53d6\u4e3a\u51fd\u6570\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u5c06\u5e38\u89c1\u7684\u64cd\u4f5c\u5c01\u88c5\u4e3a\u51fd\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u7684\u91cd\u590d\u6027\uff0c\u5e76\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u77e9\u9635\u7684\u548c<\/strong><\/h2>\n<p>def compute_matrix_sum(matrix):<\/p>\n<p>    return np.sum(matrix)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(10000, 10000)<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570\u8ba1\u7b97\u77e9\u9635\u7684\u548c<\/strong><\/h2>\n<p>matrix_sum = compute_matrix_sum(large_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u5e38\u7528\u7684\u64cd\u4f5c\u5c01\u88c5\u4e3a\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u591a\u6b21\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u590d\u7f16\u5199\u76f8\u540c\u7684\u4ee3\u7801\u3002\u8fd9\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u51cf\u5c11\u4e86\u9519\u8bef\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u5757\u5316\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u6a21\u5757\u5316\u4ee3\u7801\u662f\u6307\u5c06\u4ee3\u7801\u5212\u5206\u4e3a\u591a\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u6bcf\u4e2a\u6a21\u5757\u8d1f\u8d23\u5b8c\u6210\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u6a21\u5757\u5316\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u52a0\u8f7d\u3001\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u53ef\u89c6\u5316\u7b49\u4e0d\u540c\u7684\u4efb\u52a1\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># data_loader.py<\/p>\n<p>import numpy as np<\/p>\n<p>def load_data():<\/p>\n<p>    return np.random.rand(10000, 10000)<\/p>\n<h2><strong>data_processor.py<\/strong><\/h2>\n<p>import numpy as np<\/p>\n<p>def process_data(matrix):<\/p>\n<p>    return np.sum(matrix)<\/p>\n<h2><strong>data_visualizer.py<\/strong><\/h2>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>def visualize_data(matrix):<\/p>\n<p>    plt.imshow(matrix, cmap=&#39;viridis&#39;, aspect=&#39;auto&#39;)<\/p>\n<p>    plt.colorbar()<\/p>\n<p>    plt.show()<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>from data_loader import load_data<\/p>\n<p>from data_processor import process_data<\/p>\n<p>from data_visualizer import visualize_data<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>data = load_data()<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>result = process_data(data)<\/p>\n<h2><strong>\u53ef\u89c6\u5316\u6570\u636e<\/strong><\/h2>\n<p>visualize_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6a21\u5757\u5316\u4ee3\u7801\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\u7ed3\u6784\uff0c\u4f7f\u6bcf\u4e2a\u6a21\u5757\u90fd\u80fd\u72ec\u7acb\u5de5\u4f5c\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u8c03\u8bd5\uff0c\u56e0\u4e3a\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u5b9a\u4f4d\u548c\u4fee\u590d\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u62bd\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u62bd\u8c61\u662f\u6307\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282\uff0c\u53ea\u66b4\u9732\u5fc5\u8981\u7684\u63a5\u53e3\u3002\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u4f7f\u7528\u62bd\u8c61\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e13\u6ce8\u4e8e\u9ad8\u5c42\u6b21\u7684\u903b\u8f91\uff0c\u800c\u4e0d\u5fc5\u5173\u5fc3\u5e95\u5c42\u5b9e\u73b0\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u77e9\u9635\u7c7b\uff0c\u5c06\u77e9\u9635\u7684\u64cd\u4f5c\u5c01\u88c5\u5230\u7c7b\u7684\u65b9\u6cd5\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>class Matrix:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def sum(self):<\/p>\n<p>        return np.sum(self.data)<\/p>\n<p>    def visualize(self):<\/p>\n<p>        import matplotlib.pyplot as plt<\/p>\n<p>        plt.imshow(self.data, cmap=&#39;viridis&#39;, aspect=&#39;auto&#39;)<\/p>\n<p>        plt.colorbar()<\/p>\n<p>        plt.show()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u5bf9\u8c61<\/strong><\/h2>\n<p>matrix = Matrix(np.random.rand(10000, 10000))<\/p>\n<h2><strong>\u4f7f\u7528\u77e9\u9635\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>matrix_sum = matrix.sum()<\/p>\n<p>matrix.visualize()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u62bd\u8c61\uff0c\u53ef\u4ee5\u5c06\u590d\u6742\u7684\u64cd\u4f5c\u9690\u85cf\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u53ea\u66b4\u9732\u7b80\u5355\u7684\u63a5\u53e3\u7ed9\u7528\u6237\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u4f7f\u7528\u548c\u7406\u89e3\uff0c\u56e0\u4e3a\u7528\u6237\u53ea\u9700\u8981\u5173\u5fc3\u63a5\u53e3\u7684\u4f7f\u7528\uff0c\u800c\u4e0d\u5fc5\u4e86\u89e3\u5b9e\u73b0\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6027\u80fd\u4f18\u5316\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u901a\u8fc7\u4e00\u4e9b\u4f18\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u63d0\u9ad8\u77e9\u9635\u64cd\u4f5c\u7684\u901f\u5ea6\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u548c\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u77e9\u9635\u7684\u5143\u7d20\u662f\u6574\u6570\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528<code>int8<\/code>\u6216<code>int16<\/code>\u7b49\u8f83\u5c0f\u7684\u6570\u636e\u7c7b\u578b\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684<code>int64<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528\u8f83\u5c0f\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>matrix = np.random.randint(0, 100, size=(10000, 10000), dtype=np.int8)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u5e76\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\uff0c\u56e0\u4e3a\u8f83\u5c0f\u7684\u6570\u636e\u7c7b\u578b\u5728\u5185\u5b58\u4e2d\u5360\u7528\u7684\u7a7a\u95f4\u66f4\u5c11\uff0c\u8ba1\u7b97\u65f6\u7684\u7f13\u5b58\u547d\u4e2d\u7387\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u548c\u63d0\u9ad8\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u5728\u51fd\u6570\u4e2d\u4f20\u9012\u77e9\u9635\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u5f15\u7528\u800c\u4e0d\u662f\u590d\u5236\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def process_matrix(matrix):<\/p>\n<p>    # \u5728\u51fd\u6570\u4e2d\u76f4\u63a5\u4f7f\u7528\u5f15\u7528\uff0c\u800c\u4e0d\u662f\u590d\u5236\u77e9\u9635<\/p>\n<p>    matrix[0, 0] = 0<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(10000, 10000)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>process_matrix(large_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d\uff0c\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u5e76\u63d0\u9ad8\u901f\u5ea6\uff0c\u56e0\u4e3a\u590d\u5236\u77e9\u9635\u9700\u8981\u5206\u914d\u989d\u5916\u7684\u5185\u5b58\uff0c\u5e76\u8fdb\u884c\u6570\u636e\u7684\u590d\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5229\u7528\u5e76\u884c\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5e76\u884c\u8ba1\u7b97\u662f\u63d0\u9ad8\u5904\u7406\u5927\u578b\u77e9\u9635\u901f\u5ea6\u7684\u6709\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u5229\u7528\u591a\u6838CPU\u6216\u5206\u5e03\u5f0f\u8ba1\u7b97\u8d44\u6e90\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u4e00\u4e9b\u5e93\uff0c\u5982NumPy\u3001SciPy\u548cDask\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e76\u884c\u8ba1\u7b97\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0cNumPy\u7684<code>einsum<\/code>\u51fd\u6570\u53ef\u4ee5\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>matrix_a = np.random.rand(10000, 10000)<\/p>\n<p>matrix_b = np.random.rand(10000, 10000)<\/p>\n<h2><strong>\u4f7f\u7528einsum\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>result = np.einsum(&#39;ij,jk-&gt;ik&#39;, matrix_a, matrix_b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>einsum<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u63d0\u9ad8\u77e9\u9635\u4e58\u6cd5\u7684\u901f\u5ea6\u3002<code>einsum<\/code>\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u51b3\u5b9a\u4f7f\u7528\u4f55\u79cd\u4f18\u5316\u7b56\u7565\uff0c\u4ee5\u83b7\u5f97\u6700\u4f73\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u8c03\u8bd5\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u8c03\u8bd5\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u73af\u8282\u3002\u901a\u8fc7\u4e00\u4e9b\u8c03\u8bd5\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u5feb\u5730\u5b9a\u4f4d\u548c\u4fee\u590d\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u65ad\u70b9<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u65ad\u70b9\u53ef\u4ee5\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u4ee5\u4fbf\u68c0\u67e5\u5f53\u524d\u7684\u72b6\u6001\u3002\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u53ef\u4ee5\u5728\u5173\u952e\u4f4d\u7f6e\u8bbe\u7f6e\u65ad\u70b9\uff0c\u4ee5\u68c0\u67e5\u77e9\u9635\u7684\u5185\u5bb9\u548c\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u578b\u77e9\u9635<\/strong><\/h2>\n<p>large_matrix = np.random.rand(10000, 10000)<\/p>\n<h2><strong>\u8bbe\u7f6e\u65ad\u70b9<\/strong><\/h2>\n<p>import pdb; pdb.set_trace()<\/p>\n<h2><strong>\u6267\u884c\u64cd\u4f5c<\/strong><\/h2>\n<p>result = np.sum(large_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u65ad\u70b9\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6682\u505c\u6267\u884c\uff0c\u5e76\u8fdb\u5165\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u73af\u5883\u3002\u5728\u8c03\u8bd5\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u6267\u884c\u8bed\u53e5\uff0c\u751a\u81f3\u4fee\u6539\u4ee3\u7801\u3002\u8fd9\u5bf9\u4e8e\u5b9a\u4f4d\u548c\u4fee\u590d\u95ee\u9898\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/p>\n<\/p>\n<p><p>\u65e5\u5fd7\u8bb0\u5f55\u662f\u8c03\u8bd5\u5927\u578b\u77e9\u9635\u64cd\u4f5c\u7684\u6709\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u8bb0\u5f55\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u66f4\u597d\u5730<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u8c03\u8bd5\u5927\u578b\u77e9\u9635\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u8c03\u8bd5\u7684\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u4f8b\u5982<code>np.shape<\/code>\u548c<code>np.sum<\/code>\uff0c\u6765\u5feb\u901f\u68c0\u67e5\u77e9\u9635\u7684\u7ef4\u5ea6\u548c\u57fa\u672c\u7edf\u8ba1\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u5229\u7528Python\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5982pdb\uff0c\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u4ee3\u7801\u5e76\u68c0\u67e5\u77e9\u9635\u7684\u72b6\u6001\uff0c\u5e2e\u52a9\u8bc6\u522b\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<p><strong>\u8c03\u8bd5\u5927\u578b\u77e9\u9635\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u548c\u89e3\u51b3\u65b9\u6848\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6570\u7ec4\u7ef4\u5ea6\u4e0d\u5339\u914d\u3001\u6570\u636e\u7c7b\u578b\u4e0d\u4e00\u81f4\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u77e9\u9635\u7684\u7ef4\u5ea6\u548c\u6570\u636e\u7c7b\u578b\u6765\u63d0\u524d\u53d1\u73b0\u8fd9\u4e9b\u95ee\u9898\u3002\u4f7f\u7528<code>assert<\/code>\u8bed\u53e5\u786e\u4fdd\u77e9\u9635\u5728\u8fdb\u884c\u8fd0\u7b97\u524d\u7b26\u5408\u9884\u671f\u7684\u6761\u4ef6\u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65b9\u6cd5\u3002\u5982\u679c\u53d1\u73b0\u9519\u8bef\uff0c\u9010\u6b65\u7f29\u5c0f\u95ee\u9898\u8303\u56f4\uff0c\u4f7f\u7528\u5c0f\u578b\u793a\u4f8b\u8fdb\u884c\u6d4b\u8bd5\uff0c\u80fd\u591f\u66f4\u5feb\u5b9a\u4f4d\u95ee\u9898\u6e90\u5934\u3002<\/p>\n<p><strong>\u5728\u8c03\u8bd5\u5927\u578b\u77e9\u9635\u65f6\uff0c\u5982\u4f55\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u5c06\u590d\u6742\u7684\u77e9\u9635\u64cd\u4f5c\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u51fd\u6570\u6dfb\u52a0\u8be6\u7ec6\u7684\u6587\u6863\u8bf4\u660e\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6e05\u6670\u7684\u53d8\u91cf\u547d\u540d\u548c\u5206\u6b65\u6ce8\u91ca\u4e5f\u80fd\u5e2e\u52a9\u7406\u89e3\u4ee3\u7801\u903b\u8f91\u3002\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u91c7\u7528\u7248\u672c\u63a7\u5236\u5de5\u5177\u5982Git\u6765\u8ddf\u8e2a\u4ee3\u7801\u53d8\u5316\uff0c\u4e5f\u80fd\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u5f88\u5927\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8c03\u8bd5\u5927\u578bPython\u77e9\u9635\u7684\u6709\u6548\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u5b58\u4f18\u5316\u5e93\u3001\u5206\u5757\u5904\u7406\u77e9\u9635\u3001\u53ef\u89c6\u5316\u5de5\u5177\u3001\u7b80\u5316\u548c\u62bd\u8c61\u4ee3\u7801\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5185 [&hellip;]","protected":false},"author":3,"featured_media":963132,"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\/963129"}],"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=963129"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963129\/revisions"}],"predecessor-version":[{"id":963133,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963129\/revisions\/963133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/963132"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=963129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=963129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=963129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}