{"id":1017274,"date":"2024-12-27T12:25:00","date_gmt":"2024-12-27T04:25:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1017274.html"},"modified":"2024-12-27T12:25:06","modified_gmt":"2024-12-27T04:25:06","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1017274.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u6784\u5efa\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25160212\/a6bf5339-0fb7-4b1f-8010-2ff447fe8555.webp\" alt=\"python\u4e2d\u5982\u4f55\u6784\u5efa\u77e9\u9635\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6784\u5efa\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002<strong>\u4f7f\u7528<code>NumPy<\/code>\u5e93\u3001<code>list<\/code>\u5d4c\u5957\u3001<code>Pandas<\/code>\u5e93\u3001<code>SciPy<\/code>\u5e93\u662f\u6784\u5efa\u77e9\u9635\u7684\u5e38\u89c1\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c<code>NumPy<\/code>\u662f\u6700\u4e3a\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u6211\u4eec\u6765\u8be6\u7ec6\u8ba8\u8bba\u4e00\u4e0b\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528<code>NumPy<\/code>\u5e93\u6784\u5efa\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p><strong><code>NumPy<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002\u8981\u4f7f\u7528\u5b83\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5<code>NumPy<\/code>\u5e93\uff08\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install numpy<\/code>\u6765\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import numpy as np<\/code>\u6765\u5bfc\u5165\u5e93\u3002<code>NumPy<\/code>\u7684<code>array<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u77e9\u9635\uff0c\u4f8b\u5982\uff1a<code>matrix = np.array([[1, 2, 3], [4, 5, 6]])<\/code>\u3002\u8fd9\u884c\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\u3002<code>NumPy<\/code>\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u51fd\u6570\uff0c\u5982<code>zeros<\/code>\u3001<code>ones<\/code>\u3001<code>eye<\/code>\u7b49\uff0c\u5206\u522b\u7528\u4e8e\u521b\u5efa\u5168\u96f6\u77e9\u9635\u3001\u5168\u4e00\u77e9\u9635\u548c\u5355\u4f4d\u77e9\u9635<\/strong>\u3002\u6b64\u5916\uff0c<code>NumPy<\/code>\u652f\u6301\u591a\u79cd\u77e9\u9635\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\uff0c\u4f7f\u5176\u6210\u4e3a\u5904\u7406\u77e9\u9635\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528NUMPY\u5e93\u6784\u5efa\u77e9\u9635<\/p>\n<\/p>\n<p><p><code>NumPy<\/code>\u662fPython\u4e2d\u79d1\u5b66\u8ba1\u7b97\u7684\u6838\u5fc3\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u76f8\u5173\u64cd\u4f5c\u7684\u652f\u6301\u3002\u901a\u8fc7\u4f7f\u7528<code>NumPy<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u9ad8\u6548\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u57fa\u672c\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>np.array<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u77e9\u9635\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2a\u5d4c\u5957\u7684\u5217\u8868\u6765\u8868\u793a\u77e9\u9635\u7684\u884c\u548c\u5217\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u4e00\u4e2a2\u884c3\u5217\u7684\u77e9\u9635\u3002<code>NumPy<\/code>\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u662f\u53ef\u4ee5\u81ea\u52a8\u63a8\u65ad\u7684\uff0c\u4e5f\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u6307\u5b9a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u7279\u6b8a\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p><code>NumPy<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u7279\u6b8a\u77e9\u9635\u3002\u6700\u5e38\u89c1\u7684\u6709\u5168\u96f6\u77e9\u9635\u3001\u5168\u4e00\u77e9\u9635\u548c\u5355\u4f4d\u77e9\u9635\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u5168\u96f6\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528<code>np.zeros<\/code>\u51fd\u6570\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u5168\u96f6\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">zero_matrix = np.zeros((3, 3))<\/p>\n<p>print(zero_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5168\u4e00\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528<code>np.ones<\/code>\u51fd\u6570\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u5168\u4e00\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">one_matrix = np.ones((3, 3))<\/p>\n<p>print(one_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5355\u4f4d\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528<code>np.eye<\/code>\u51fd\u6570\u521b\u5efa\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u5355\u4f4d\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">identity_matrix = np.eye(3)<\/p>\n<p>print(identity_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u6784\u5efa\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>NumPy<\/code>\u662f\u5904\u7406\u77e9\u9635\u7684\u9996\u9009\u5de5\u5177\uff0c\u4f46Python\u672c\u8eab\u7684\u5217\u8868\u7ed3\u6784\u4e5f\u53ef\u4ee5\u7528\u6765\u6784\u5efa\u7b80\u5355\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u57fa\u672c\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684\u5217\u8868\u652f\u6301\u5d4c\u5957\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\u77e9\u9635\u7684\u884c\u548c\u5217\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a2&#215;3\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6]]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\uff0c\u4f46\u4e0d\u5982<code>NumPy<\/code>\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u6267\u884c\u77e9\u9635\u8fd0\u7b97\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u64cd\u4f5c\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6784\u5efa\u77e9\u9635\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528<code>matrix[0][1]<\/code>\uff0c\u4fee\u6539\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c<code>matrix[0][1] = 10<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528PANDAS\u6784\u5efa\u77e9\u9635<\/p>\n<\/p>\n<p><p><code>Pandas<\/code>\u662fPython\u4e2d\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u867d\u7136\u5b83\u4e0d\u662f\u4e13\u95e8\u4e3a\u77e9\u9635\u8fd0\u7b97\u8bbe\u8ba1\u7684\uff0c\u4f46\u5b83\u7684<code>DataFrame<\/code>\u7ed3\u6784\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u5e26\u6709\u6807\u7b7e\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa<code>DataFrame<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>Pandas<\/code>\u7684<code>DataFrame<\/code>\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u7c7b\u4f3c\u4e8e\u77e9\u9635\u7684\u7ed3\u6784\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa2&#215;3<code>DataFrame<\/code>\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Column1&#39;: [1, 4], &#39;Column2&#39;: [2, 5], &#39;Column3&#39;: [3, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u4e00\u4e2a2\u884c3\u5217\u7684<code>DataFrame<\/code>\uff0c\u5176\u4e2d\u5217\u6709\u6807\u7b7e<code>Column1<\/code>\u3001<code>Column2<\/code>\u548c<code>Column3<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u64cd\u4f5c<code>DataFrame<\/code><\/strong><\/p>\n<\/p>\n<p><p><code>Pandas<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9<code>DataFrame<\/code>\u8fdb\u884c\u6392\u5e8f\u3001\u8fc7\u6ee4\u3001\u805a\u5408\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7<code>df[&#39;Column1&#39;]<\/code>\u8bbf\u95ee\u67d0\u4e00\u5217\u7684\u6570\u636e\uff0c\u901a\u8fc7<code>df.iloc[0, 1]<\/code>\u8bbf\u95ee\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528SCIPY\u5e93\u6784\u5efa\u77e9\u9635<\/p>\n<\/p>\n<p><p><code>SciPy<\/code>\u662f\u4e00\u4e2a\u57fa\u4e8e<code>NumPy<\/code>\u6784\u5efa\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u6570\u5b66\u51fd\u6570\u548c\u9ad8\u7ea7\u8fd0\u7b97\u529f\u80fd\u3002\u5bf9\u4e8e\u6784\u5efa\u7a00\u758f\u77e9\u9635\uff0c<code>SciPy<\/code>\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u7a00\u758f\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bb8\u591a\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u77e9\u9635\u5927\u90e8\u5206\u5143\u7d20\u4e3a\u96f6\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u7a00\u758f\u77e9\u9635\u6765\u8282\u7701\u5185\u5b58\u3002<code>SciPy<\/code>\u7684<code>scipy.sparse<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u7a00\u758f\u77e9\u9635\u7c7b\u578b\uff0c\u5982<code>csr_matrix<\/code>\u3001<code>csc_matrix<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.sparse import csr_matrix<\/p>\n<p>data = [1, 2, 3]<\/p>\n<p>row_indices = [0, 1, 2]<\/p>\n<p>col_indices = [0, 1, 2]<\/p>\n<p>sparse_matrix = csr_matrix((data, (row_indices, col_indices)), shape=(3, 3))<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u5bf9\u89d2\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u64cd\u4f5c\u7a00\u758f\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u7a00\u758f\u77e9\u9635\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u77e9\u9635\u8fd0\u7b97\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\u3002\u53ef\u4ee5\u4f7f\u7528<code>toarray<\/code>\u65b9\u6cd5\u5c06\u7a00\u758f\u77e9\u9635\u8f6c\u6362\u4e3a\u5bc6\u96c6\u77e9\u9635\u4ee5\u4fbf\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dense_matrix = sparse_matrix.toarray()<\/p>\n<p>print(dense_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u77e9\u9635\u8fd0\u7b97\u4e0e\u5e94\u7528<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u77e9\u9635\u8fd0\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6784\u5efa\u77e9\u9635\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\uff0c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u3001\u9006\u77e9\u9635\u7b49\u3002<code>NumPy<\/code>\u63d0\u4f9b\u4e86\u4e00\u6574\u5957\u77e9\u9635\u8fd0\u7b97\u5de5\u5177\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u52a0\u6cd5\u548c\u51cf\u6cd5<\/strong>\uff1a\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>+<\/code>\u548c<code>-<\/code>\u8fd0\u7b97\u7b26\u8fdb\u884c\u5143\u7d20\u7ea7\u52a0\u51cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>sum_matrix = matrix1 + matrix2<\/p>\n<p>print(sum_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e58\u6cd5<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>*<\/code>\u8fdb\u884c\u5143\u7d20\u7ea7\u4e58\u6cd5\uff0c\u4f7f\u7528<code>np.dot<\/code>\u6216<code>@<\/code>\u8fd0\u7b97\u7b26\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">product_matrix = np.dot(matrix1, matrix2)<\/p>\n<p>print(product_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f6c\u7f6e<\/strong>\uff1a\u4f7f\u7528<code>matrix.T<\/code>\u53ef\u4ee5\u5f97\u5230\u77e9\u9635\u7684\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transpose_matrix = matrix1.T<\/p>\n<p>print(transpose_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u7684\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u77e9\u9635\u5728\u8bb8\u591a\u79d1\u5b66\u8ba1\u7b97\u3001\u5de5\u7a0b\u3001\u6570\u636e\u5206\u6790\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5728\u56fe\u50cf\u5904\u7406\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3001\u7269\u7406\u6a21\u62df\u7b49\u9886\u57df\uff0c\u77e9\u9635\u662f\u63cf\u8ff0\u548c\u8ba1\u7b97\u6570\u636e\u7684\u91cd\u8981\u5de5\u5177\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong>\uff1a\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u56fe\u50cf\u901a\u5e38\u88ab\u8868\u793a\u4e3a\u77e9\u9635\uff0c\u77e9\u9635\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u50cf\u7d20\u7684\u4eae\u5ea6\u6216\u989c\u8272\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u673a\u5668\u5b66\u4e60<\/strong>\uff1a\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u7279\u5f81\u548c\u6837\u672c\u6570\u636e\u901a\u5e38\u4ee5\u77e9\u9635\u5f62\u5f0f\u5b58\u50a8\uff0c\u8bb8\u591a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u90fd\u57fa\u4e8e\u77e9\u9635\u8fd0\u7b97\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7269\u7406\u6a21\u62df<\/strong>\uff1a\u5728\u7269\u7406\u6a21\u62df\u4e2d\uff0c\u77e9\u9635\u7528\u4e8e\u63cf\u8ff0\u7cfb\u7edf\u7684\u72b6\u6001\u548c\u53d8\u5316\uff0c\u5982\u529b\u5b66\u7cfb\u7edf\u4e2d\u7684\u521a\u5ea6\u77e9\u9635\u3001\u7535\u8def\u4e2d\u7684\u5bfc\u7eb3\u77e9\u9635\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u77e9\u9635\u7684\u4f18\u5316\u4e0e\u6027\u80fd<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f18\u5316\u77e9\u9635\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u91cd\u8981\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u63d0\u9ad8\u77e9\u9635\u64cd\u4f5c\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528\u7a00\u758f\u77e9\u9635<\/strong>\uff1a\u5982\u679c\u77e9\u9635\u4e2d\u5927\u591a\u6570\u5143\u7d20\u662f\u96f6\uff0c\u4f7f\u7528<code>SciPy<\/code>\u7684\u7a00\u758f\u77e9\u9635\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u6d88\u8017\u548c\u8ba1\u7b97\u65f6\u95f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u5757\u77e9\u9635\u8fd0\u7b97<\/strong>\uff1a\u5bf9\u4e8e\u8d85\u5927\u578b\u77e9\u9635\uff0c\u53ef\u4ee5\u5c06\u5176\u5206\u5272\u6210\u8f83\u5c0f\u7684\u5757\uff0c\u5e76\u884c\u5904\u7406\u4ee5\u63d0\u9ad8\u8fd0\u7b97\u901f\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u7b97\u6cd5<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u6216\u5e93\u6765\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3002\u6709\u4e9b\u5e93\u9488\u5bf9\u7279\u5b9a\u7c7b\u578b\u7684\u77e9\u9635\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u5206\u6790\u4e0e\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u65f6\uff0c\u6027\u80fd\u5206\u6790\u662f\u5f88\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>time<\/code>\u6a21\u5757\u6765\u6d4b\u91cf\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u66f4\u4e13\u4e1a\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\u5982<code>cProfile<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6267\u884c\u77e9\u9635\u64cd\u4f5c<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;Execution time:&quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u77e9\u9635\u5728\u673a\u5668\u5b66\u4e60\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7279\u5f81\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u8868\u793a\u4e3a\u4e00\u4e2a\u7279\u5f81\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u6837\u672c\uff0c\u6bcf\u4e00\u5217\u4ee3\u8868\u4e00\u4e2a\u7279\u5f81\u3002\u901a\u8fc7\u5bf9\u7279\u5f81\u77e9\u9635\u8fdb\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u5f52\u4e00\u5316\u3001\u6807\u51c6\u5316\u3001\u964d\u7ef4\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5f52\u4e00\u5316\u548c\u6807\u51c6\u5316<\/strong>\uff1a\u901a\u8fc7<code>scikit-learn<\/code>\u5e93\u7684<code>StandardScaler<\/code>\u548c<code>MinMaxScaler<\/code>\u53ef\u4ee5\u5bf9\u7279\u5f81\u77e9\u9635\u8fdb\u884c\u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler, MinMaxScaler<\/p>\n<p>scaler = StandardScaler()<\/p>\n<p>normalized_data = scaler.fit_transform(matrix)<\/p>\n<p>print(normalized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6a21\u578b\u8bad\u7ec3<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6a21\u578b\u8bad\u7ec3\u4e2d\uff0c\u77e9\u9635\u8fd0\u7b97\u88ab\u5e7f\u6cdb\u7528\u4e8e\u8ba1\u7b97\u68af\u5ea6\u3001\u635f\u5931\u51fd\u6570\u3001\u9884\u6d4b\u7ed3\u679c\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7ebf\u6027\u56de\u5f52<\/strong>\uff1a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u53ef\u4ee5\u8868\u793a\u4e3a\u77e9\u9635\u5f62\u5f0f\uff0c\u901a\u8fc7\u6700\u5c0f\u5316\u635f\u5931\u51fd\u6570\u7684\u77e9\u9635\u8fd0\u7b97\u5b9e\u73b0\u53c2\u6570\u4f18\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u795e\u7ecf\u7f51\u7edc<\/strong>\uff1a\u5728\u795e\u7ecf\u7f51\u7edc\u4e2d\uff0c\u524d\u5411\u4f20\u64ad\u548c\u53cd\u5411\u4f20\u64ad\u7b97\u6cd5\u90fd\u5927\u91cf\u4f7f\u7528\u77e9\u9635\u8fd0\u7b97\u6765\u8ba1\u7b97\u6fc0\u6d3b\u51fd\u6570\u3001\u68af\u5ea6\u548c\u6743\u91cd\u66f4\u65b0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u77e9\u9635\u7684\u5b58\u50a8\u4e0e\u8bfb\u53d6<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b58\u50a8\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u5c06\u5176\u5b58\u50a8\u5230\u6587\u4ef6\u4e2d\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<code>NumPy<\/code>\u63d0\u4f9b\u4e86<code>save<\/code>\u548c<code>savetxt<\/code>\u51fd\u6570\u6765\u4fdd\u5b58\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">np.save(&#39;matrix.npy&#39;, matrix)<\/p>\n<p>np.savetxt(&#39;matrix.txt&#39;, matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b58\u50a8\u4e3a<code>.npy<\/code>\u6587\u4ef6\u65f6\uff0c\u77e9\u9635\u4f1a\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u4fdd\u5b58\uff0c\u8bfb\u53d6\u901f\u5ea6\u8f83\u5feb\u3002\u800c<code>.txt<\/code>\u6587\u4ef6\u5219\u662f\u4ee5\u6587\u672c\u683c\u5f0f\u4fdd\u5b58\uff0c\u66f4\u6613\u4e8e\u67e5\u770b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u5b58\u50a8\u7684\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7<code>load<\/code>\u548c<code>loadtxt<\/code>\u51fd\u6570\u8bfb\u53d6\u56de\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">loaded_matrix = np.load(&#39;matrix.npy&#39;)<\/p>\n<p>loaded_txt_matrix = np.loadtxt(&#39;matrix.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bfb\u53d6\u540e\uff0c\u77e9\u9635\u53ef\u4ee5\u7ee7\u7eed\u7528\u4e8e\u8ba1\u7b97\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e5d\u3001\u603b\u7ed3\u4e0e\u5c55\u671b<\/p>\n<\/p>\n<p><p>\u6784\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u662f\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u57fa\u7840\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u5e93\u6765\u652f\u6301\u77e9\u9635\u7684\u6784\u5efa\u3001\u64cd\u4f5c\u548c\u4f18\u5316\uff0c\u5982<code>NumPy<\/code>\u3001<code>SciPy<\/code>\u3001<code>Pandas<\/code>\u7b49\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u9ad8\u6548\u89e3\u51b3\u5404\u79cd\u4e0e\u77e9\u9635\u76f8\u5173\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u672a\u6765\uff0c\u968f\u7740\u79d1\u5b66\u8ba1\u7b97\u7684\u6df1\u5165\u53d1\u5c55\uff0c\u77e9\u9635\u8fd0\u7b97\u7684\u6027\u80fd\u548c\u7b97\u6cd5\u4f18\u5316\u5c06\u6210\u4e3a\u7814\u7a76\u7684\u70ed\u70b9\u3002\u65b0\u5174\u7684\u786c\u4ef6\u6280\u672f\u5982GPU\u52a0\u901f\u548c\u91cf\u5b50\u8ba1\u7b97\u53ef\u80fd\u4f1a\u8fdb\u4e00\u6b65\u63d0\u5347\u77e9\u9635\u8fd0\u7b97\u7684\u80fd\u529b\uff0c\u4e3a\u79d1\u5b66\u7814\u7a76\u548c\u5de5\u7a0b\u5e94\u7528\u5e26\u6765\u65b0\u7684\u673a\u9047\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u4e8c\u7ef4\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6216NumPy\u5e93\u3002\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5c06\u591a\u4e2a\u5217\u8868\u5305\u542b\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c<code>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\u3002\u5982\u679c\u9009\u62e9\u4f7f\u7528NumPy\u5e93\uff0c\u53ea\u9700\u4f7f\u7528 <code>np.array()<\/code> \u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<code>import numpy as np; matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bf9\u77e9\u9635\u8fdb\u884c\u8fd0\u7b97\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u77e9\u9635\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u8f6c\u7f6e\u7b49\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e24\u4e2a\u77e9\u9635 <code>A<\/code> \u548c <code>B<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>C = A + B<\/code> \u8fdb\u884c\u77e9\u9635\u52a0\u6cd5\u3002\u5bf9\u4e8e\u77e9\u9635\u4e58\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>C = np.dot(A, B)<\/code> \u6216\u8005 <code>C = A @ B<\/code> \u8fdb\u884c\u8ba1\u7b97\u3002\u6b64\u5916\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86 <code>A.T<\/code> \u6765\u5b9e\u73b0\u77e9\u9635\u7684\u8f6c\u7f6e\u64cd\u4f5c\uff0c\u6781\u5927\u5730\u65b9\u4fbf\u4e86\u77e9\u9635\u7684\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ece\u77e9\u9635\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u884c\u6216\u5217\uff1f<\/strong><br \/>\u5728NumPy\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u884c\u6216\u5217\u975e\u5e38\u7b80\u5355\u3002\u5047\u8bbe <code>matrix<\/code> \u662f\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u63d0\u53d6\u7b2c\u4e00\u884c\u53ef\u4ee5\u4f7f\u7528 <code>row = matrix[0]<\/code>\uff0c\u800c\u63d0\u53d6\u7b2c\u4e8c\u5217\u5219\u53ef\u4ee5\u4f7f\u7528 <code>column = matrix[:, 1]<\/code>\u3002\u8fd9\u79cd\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u7075\u6d3b\u5730\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u5f62\u72b6\u7684\u77e9\u9635\uff0c\u5e2e\u52a9\u7528\u6237\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6784\u5efa\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f7f\u7528NumPy\u5e93\u3001list\u5d4c\u5957\u3001Pandas\u5e93\u3001SciPy\u5e93\u662f\u6784 [&hellip;]","protected":false},"author":3,"featured_media":1017279,"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\/1017274"}],"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=1017274"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017274\/revisions"}],"predecessor-version":[{"id":1017282,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017274\/revisions\/1017282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1017279"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1017274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1017274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1017274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}