{"id":1056512,"date":"2024-12-31T15:00:17","date_gmt":"2024-12-31T07:00:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056512.html"},"modified":"2024-12-31T15:00:19","modified_gmt":"2024-12-31T07:00:19","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1056512.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/aeb1d12f-7eba-4220-a0db-30d2e28a3e14.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u77e9\u9635<\/strong>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u7b49\u3002<strong>\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6<\/strong>\uff0c\u5176\u4e2d\u5d4c\u5957\u5217\u8868\u662f\u6700\u57fa\u672c\u7684\u65b9\u5f0f\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1bNumPy\u5e93\u662f\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u77e9\u9635\u8fd0\u7b97\u7684\u7528\u6237\uff1b\u4ece\u6587\u4ef6\u8bfb\u53d6\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u8f93\u5165\u6216\u8005\u5faa\u73af\u8f93\u5165\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1. \u624b\u52a8\u8f93\u5165\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u624b\u52a8\u8f93\u5165\u9002\u5408\u5c0f\u89c4\u6a21\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u4e00\u4e2a2&#215;3\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u5b9a\u4e49\u4e86\u4e00\u4e2a2\u884c3\u5217\u7684\u77e9\u9635\u3002\u6bcf\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\u4ee3\u8868\u77e9\u9635\u4e2d\u7684\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><h4>2. \u5faa\u73af\u8f93\u5165\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u77e9\u9635\uff0c\u624b\u52a8\u8f93\u5165\u663e\u5f97\u4e0d\u592a\u5b9e\u9645\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6765\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u4e00\u4e2a3&#215;3\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = 3<\/p>\n<p>cols = 3<\/p>\n<p>matrix = []<\/p>\n<p>for i in range(rows):<\/p>\n<p>    row = []<\/p>\n<p>    for j in range(cols):<\/p>\n<p>        element = int(input(f&quot;Enter element for row {i+1}, col {j+1}: &quot;))<\/p>\n<p>        row.append(element)<\/p>\n<p>    matrix.append(row)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e24\u4e2a\u5d4c\u5957\u7684for\u5faa\u73af\uff0c\u5206\u522b\u8f93\u5165\u77e9\u9635\u7684\u884c\u548c\u5217\uff0c\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u7684\u5f3a\u5927\u5de5\u5177\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>1. \u624b\u52a8\u8f93\u5165NumPy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>array()<\/code>\u51fd\u6570\u624b\u52a8\u8f93\u5165\u4e00\u4e2a\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u4e00\u4e2a2&#215;3\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u7684<code>array()<\/code>\u51fd\u6570\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>2. \u4ece\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>array()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>nested_list = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>]<\/p>\n<p>matrix = np.array(nested_list)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u7684<code>array()<\/code>\u51fd\u6570\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528NumPy\u7684\u51fd\u6570\u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u521b\u5efa\u7279\u6b8a\u7684\u77e9\u9635\uff0c\u4f8b\u5982\u5168\u96f6\u77e9\u9635\u3001\u5168\u4e00\u77e9\u9635\u3001\u5355\u4f4d\u77e9\u9635\u7b49\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\">import numpy as np<\/p>\n<p>matrix = np.zeros((3, 3))<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u7684<code>zeros()<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u5168\u96f6\u77e9\u9635\u3002\u7c7b\u4f3c\u5730\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ones()<\/code>\u51fd\u6570\u521b\u5efa\u5168\u4e00\u77e9\u9635\uff0c\u4f7f\u7528<code>eye()<\/code>\u51fd\u6570\u521b\u5efa\u5355\u4f4d\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u5c06\u77e9\u9635\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u901a\u8fc7Python\u8bfb\u53d6\u3002\u5e38\u89c1\u7684\u6587\u4ef6\u683c\u5f0f\u5305\u62ec\u6587\u672c\u6587\u4ef6\u548cCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u4ece\u6587\u672c\u6587\u4ef6\u8bfb\u53d6\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6587\u4ef6<code>matrix.txt<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>1 2 3<\/p>\n<p>4 5 6<\/p>\n<p>7 8 9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = []<\/p>\n<p>with open(&#39;matrix.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        row = list(map(int, line.split()))<\/p>\n<p>        matrix.append(row)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u5c06\u6bcf\u884c\u5206\u5272\u6210\u5143\u7d20\u5217\u8868\uff0c\u518d\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff0c\u6700\u540e\u5c06\u6bcf\u884c\u6dfb\u52a0\u5230\u77e9\u9635\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aCSV\u6587\u4ef6<code>matrix.csv<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>1,2,3<\/p>\n<p>4,5,6<\/p>\n<p>7,8,9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>csv<\/code>\u6a21\u5757\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>matrix = []<\/p>\n<p>with open(&#39;matrix.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        matrix.append(list(map(int, row)))<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>csv.reader<\/code>\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff0c\u6700\u540e\u5c06\u6bcf\u884c\u6dfb\u52a0\u5230\u77e9\u9635\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u7528\u6237\u8f93\u5165\u8bfb\u53d6\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u4ece\u7528\u6237\u8f93\u5165\u4e2d\u8bfb\u53d6\u77e9\u9635\uff0c\u4f8b\u5982\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u6216GUI\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>1. \u4ece\u547d\u4ee4\u884c\u8bfb\u53d6\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u4ece\u547d\u4ee4\u884c\u8bfb\u53d6\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = int(input(&quot;Enter number of rows: &quot;))<\/p>\n<p>cols = int(input(&quot;Enter number of columns: &quot;))<\/p>\n<p>matrix = []<\/p>\n<p>for i in range(rows):<\/p>\n<p>    row = list(map(int, input(f&quot;Enter elements for row {i+1}, separated by space: &quot;).split()))<\/p>\n<p>    matrix.append(row)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\uff0c\u7136\u540e\u901a\u8fc7\u5d4c\u5957\u7684<code>for<\/code>\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u77e9\u9635\u5185\u5bb9\uff0c\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u5c06\u8f93\u5165\u5206\u5272\u6210\u5143\u7d20\u5217\u8868\uff0c\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff0c\u6700\u540e\u5c06\u6bcf\u884c\u6dfb\u52a0\u5230\u77e9\u9635\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>1. \u4ece\u5217\u8868\u521b\u5efaDataFrame<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>DataFrame<\/code>\u51fd\u6570\u4ece\u5d4c\u5957\u5217\u8868\u521b\u5efa\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>nested_list = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>]<\/p>\n<p>matrix = pd.DataFrame(nested_list)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Pandas\u7684<code>DataFrame<\/code>\u51fd\u6570\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\u3002<\/p>\n<\/p>\n<p><h4>2. \u4eceCSV\u6587\u4ef6\u8bfb\u53d6DataFrame<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>matrix = pd.read_csv(&#39;matrix.csv&#39;, header=None)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Pandas\u7684<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\uff0c<code>header=None<\/code>\u8868\u793a\u6587\u4ef6\u4e2d\u6ca1\u6709\u5217\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Scipy\u5e93<\/h3>\n<\/p>\n<p><p>Scipy\u662fPython\u4e2d\u5904\u7406\u79d1\u5b66\u8ba1\u7b97\u7684\u5f3a\u5927\u5de5\u5177\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Scipy\u7684<code>sparse<\/code>\u6a21\u5757\u521b\u5efa\u7a00\u758f\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.sparse import csr_matrix<\/p>\n<p>dense_matrix = np.array([<\/p>\n<p>    [0, 0, 3],<\/p>\n<p>    [4, 0, 0],<\/p>\n<p>    [0, 5, 0]<\/p>\n<p>])<\/p>\n<p>sparse_matrix = csr_matrix(dense_matrix)<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7a20\u5bc6\u77e9\u9635\uff0c\u7136\u540e\u4f7f\u7528Scipy\u7684<code>csr_matrix<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>2. \u4ece\u6587\u4ef6\u8bfb\u53d6\u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Scipy\u7684<code>sparse<\/code>\u6a21\u5757\u4ece\u6587\u4ef6\u8bfb\u53d6\u7a00\u758f\u77e9\u9635\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.io import mmread<\/p>\n<p>sparse_matrix = mmread(&#39;matrix.mtx&#39;)<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Scipy\u7684<code>mmread<\/code>\u51fd\u6570\u4eceMatrix Market\u683c\u5f0f\u7684\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528TensorFlow\u5e93<\/h3>\n<\/p>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u5f3a\u5927\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u5f20\u91cf\uff08\u591a\u7ef4\u6570\u7ec4\uff09\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5f20\u91cf<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528TensorFlow\u7684<code>constant<\/code>\u51fd\u6570\u521b\u5efa\u5f20\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>matrix = tf.constant([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86TensorFlow\u7684<code>constant<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a2&#215;3\u7684\u5f20\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2. \u4eceNumPy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5f20\u91cf<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528TensorFlow\u7684<code>convert_to_tensor<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5f20\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import tensorflow as tf<\/p>\n<p>np_array = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>tensor = tf.convert_to_tensor(np_array)<\/p>\n<p>print(tensor)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86TensorFlow\u7684<code>convert_to_tensor<\/code>\u51fd\u6570\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3a\u5f20\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u8f93\u5165\u548c\u5904\u7406\u77e9\u9635\u3002<strong>\u5d4c\u5957\u5217\u8868<\/strong>\u662f\u6700\u57fa\u672c\u7684\u65b9\u5f0f\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1b<strong>NumPy\u5e93<\/strong>\u662f\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u590d\u6742\u77e9\u9635\u8fd0\u7b97\u7684\u7528\u6237\uff1b<strong>\u4ece\u6587\u4ef6\u8bfb\u53d6<\/strong>\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\uff1b<strong>Pandas\u5e93<\/strong>\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u6570\u636e\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff1b<strong>Scipy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u5904\u7406\u79d1\u5b66\u8ba1\u7b97\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u9002\u5408\u5904\u7406\u7a00\u758f\u77e9\u9635\uff1b<strong>TensorFlow\u5e93<\/strong>\u9002\u5408\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u5e94\u7528\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u5165\u77e9\u9635\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u8868\u6216NumPy\u5e93\u3002\u4f7f\u7528\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u624b\u52a8\u8f93\u5165\u6bcf\u4e00\u884c\u7684\u6570\u636e\uff1b\u4f7f\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u8f93\u5165\u77e9\u9635\nmatrix = []\nrows = int(input(&quot;\u8f93\u5165\u77e9\u9635\u7684\u884c\u6570: &quot;))\nfor i in range(rows):\n    row = list(map(int, input(f&quot;\u8f93\u5165\u7b2c{i+1}\u884c\u7684\u5143\u7d20\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;).split()))\n    matrix.append(row)\n\nprint(&quot;\u8f93\u5165\u7684\u77e9\u9635\u4e3a:&quot;, matrix)\n\n# \u4f7f\u7528NumPy\u8f93\u5165\u77e9\u9635\nimport numpy as np\nmatrix_np = np.array([list(map(int, input(f&quot;\u8f93\u5165\u7b2c{i+1}\u884c\u7684\u5143\u7d20\uff0c\u4ee5\u7a7a\u683c\u5206\u9694: &quot;).split())) for i in range(rows)])\nprint(&quot;\u8f93\u5165\u7684\u77e9\u9635\u4e3a:\\n&quot;, matrix_np)\n<\/code><\/pre>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u4ec0\u4e48\u5e93\u6765\u5904\u7406\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u77e9\u9635\u7684\u5e38\u7528\u5e93\u5305\u62ecNumPy\u548cPandas\u3002NumPy\u4e13\u6ce8\u4e8e\u9ad8\u6027\u80fd\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002Pandas\u5219\u66f4\u9002\u5408\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8868\u683c\u6570\u636e\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u8f93\u5165\u77e9\u9635\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8f93\u5165\u77e9\u9635\u65f6\uff0c\u786e\u4fdd\u6bcf\u4e00\u884c\u7684\u5143\u7d20\u6570\u91cf\u4e00\u81f4\uff0c\u8fd9\u662f\u77e9\u9635\u5b9a\u4e49\u7684\u57fa\u672c\u8981\u6c42\u3002\u5982\u679c\u884c\u6570\u4e0d\u76f8\u7b49\uff0c\u5c06\u5bfc\u81f4\u521b\u5efa\u4e0d\u89c4\u5219\u7684\u5217\u8868\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u540e\u7eed\u8ba1\u7b97\u65f6\u51fa\u73b0\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u65f6\uff0c\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\u4e5f\u9700\u8981\u4fdd\u6301\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u6253\u5370\u8f93\u51fa\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u6253\u5370\u8f93\u51fa\u4e00\u4e2a\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u5982\u679c\u4f7f\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u5229\u7528\u5176\u5185\u5efa\u7684\u6253\u5370\u529f\u80fd\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u6253\u5370\u5217\u8868\u5f62\u5f0f\u7684\u77e9\u9635\nfor row in matrix:\n    print(row)\n\n# \u6253\u5370NumPy\u5f62\u5f0f\u7684\u77e9\u9635\nprint(matrix_np)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u6e05\u6670\u6613\u61c2\uff0c\u800c\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7f\u77e9\u9635\u7684\u7ed3\u6784\u66f4\u52a0\u660e\u663e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u5165\u4e00\u4e2a\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1056519,"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\/1056512"}],"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=1056512"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056512\/revisions"}],"predecessor-version":[{"id":1056520,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056512\/revisions\/1056520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056519"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}