{"id":1010736,"date":"2024-12-27T11:23:50","date_gmt":"2024-12-27T03:23:50","guid":{"rendered":""},"modified":"2024-12-27T11:23:52","modified_gmt":"2024-12-27T03:23:52","slug":"python-%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010736.html","title":{"rendered":"python \u4e2d\u5982\u4f55\u8f93\u5165\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085141\/174ae7f9-f87b-4f0f-b1a8-e4f0cf3a708f.webp\" alt=\"python \u4e2d\u5982\u4f55\u8f93\u5165\u77e9\u9635\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8f93\u5165\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\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u3002<strong>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93<\/strong>\u662f\u4e09\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u7684\u652f\u6301\u3002\u8981\u4f7f\u7528NumPy\u8f93\u5165\u77e9\u9635\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528NumPy\u8f93\u5165\u77e9\u9635\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165NumPy\u5e93<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165NumPy\u5e93\uff0c\u4e00\u822c\u4f7f\u7528<code>import numpy as np<\/code>\u6765\u7b80\u5316\u8c03\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u77e9\u9635<\/strong>\uff1a\u4f7f\u7528NumPy\u4e2d\u7684<code>array()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u64cd\u4f5c<\/strong>\uff1aNumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\uff0c\u5982\u77e9\u9635\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\u3002\u4f8b\u5982\uff0c\u8981\u8ba1\u7b97\u77e9\u9635\u7684\u8f6c\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matrix.T<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7NumPy\u5e93\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u975e\u5e38\u65b9\u4fbf\uff0c\u5c24\u5176\u9002\u5408\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u8f93\u5165\u77e9\u9635\u7684\u591a\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u5b9e\u73b0\u77e9\u9635\u7684\u65b9\u6cd5\u3002\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u6765\u5b9a\u4e49\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5217\u8868\u662f\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9a\u4e49\u77e9\u9635\u3002\u6bcf\u4e2a\u5b50\u5217\u8868\u5c31\u662f\u77e9\u9635\u7684\u4e00\u884c\u3002<\/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>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bbf\u95ee\u77e9\u9635\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[0][1]  # \u8f93\u51fa2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4fee\u6539\u77e9\u9635\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5c06\u7b2c\u4e00\u884c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4fee\u6539\u4e3a10\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix[0][1] = 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u904d\u5386\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in matrix:<\/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><h2>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><p>NumPy\u5e93\u662fPython\u4e2d\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u77e9\u9635\u521b\u5efa\u548c\u64cd\u4f5c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u77e9\u9635\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>NumPy\u77e9\u9635\u6709\u5f88\u591a\u5c5e\u6027\u53ef\u4ee5\u83b7\u53d6\uff0c\u5982\u5f62\u72b6\uff08shape\uff09\u3001\u7ef4\u5ea6\uff08ndim\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">shape = matrix.shape  # \u8f93\u51fa(3, 3)<\/p>\n<p>ndim = matrix.ndim    # \u8f93\u51fa2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u77e9\u9635\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u8fd0\u7b97\u51fd\u6570\uff0c\u5982\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u52a0\u6cd5<\/p>\n<p>matrix2 = np.array([[9, 8, 7],<\/p>\n<p>                    [6, 5, 4],<\/p>\n<p>                    [3, 2, 1]])<\/p>\n<p>result = matrix + matrix2<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>result = np.dot(matrix, matrix2)<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>transpose = matrix.T<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u751f\u6210\u7279\u6b8a\u77e9\u9635<\/h3>\n<\/p>\n<p><p>NumPy\u53ef\u4ee5\u751f\u6210\u7279\u6b8a\u77e9\u9635\uff0c\u5982\u5168\u96f6\u77e9\u9635\u3001\u5168\u4e00\u77e9\u9635\u3001\u5355\u4f4d\u77e9\u9635\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">zeros = np.zeros((3, 3))<\/p>\n<p>ones = np.ones((3, 3))<\/p>\n<p>identity = np.eye(3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h2>\n<\/p>\n<p><p>Pandas\u5e93\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5904\u7406\u77e9\u9635\u5f62\u5f0f\u7684\u6570\u636e\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u8868\u683c\u5f62\u5f0f\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efaDataFrame<\/h3>\n<\/p>\n<p><p>DataFrame\u662fPandas\u4e2d\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u770b\u4f5c\u662f\u5e26\u6709\u884c\u5217\u6807\u7b7e\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;A&#39;: [1, 4, 7],<\/p>\n<p>        &#39;B&#39;: [2, 5, 8],<\/p>\n<p>        &#39;C&#39;: [3, 6, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bbf\u95eeDataFrame\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6807\u7b7e\u6216\u4f4d\u7f6e\u8bbf\u95eeDataFrame\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u6807\u7b7e<\/p>\n<p>element = df[&#39;A&#39;][0]  # \u8f93\u51fa1<\/p>\n<h2><strong>\u4f7f\u7528\u4f4d\u7f6e<\/strong><\/h2>\n<p>element = df.iloc[0, 1]  # \u8f93\u51fa2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4fee\u6539DataFrame\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u6216\u4f4d\u7f6e\u76f4\u63a5\u4fee\u6539DataFrame\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;A&#39;][0] = 10  # \u4fee\u6539\u4e3a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. DataFrame\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8fd0\u7b97\u529f\u80fd\uff0c\u5305\u62ec\u57fa\u672c\u7684\u7b97\u672f\u8fd0\u7b97\u548c\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u52a0\u6cd5<\/p>\n<p>df[&#39;D&#39;] = df[&#39;A&#39;] + df[&#39;B&#39;]<\/p>\n<h2><strong>\u884c\u548c<\/strong><\/h2>\n<p>row_sum = df.sum(axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u7528\u6237\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u77e9\u9635\u6570\u636e\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c<code>input()<\/code>\u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>1. \u4ece\u7528\u6237\u8f93\u5165\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = int(input(&quot;Enter number of rows: &quot;))<\/p>\n<p>columns = 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(&quot;Enter row values: &quot;).split()))<\/p>\n<p>    matrix.append(row)<\/p>\n<p>print(&quot;Matrix:&quot;)<\/p>\n<p>for row in matrix:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u9a8c\u8bc1\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u662f\u5426\u7b26\u5408\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid_matrix(matrix, rows, columns):<\/p>\n<p>    for row in matrix:<\/p>\n<p>        if len(row) != columns:<\/p>\n<p>            return False<\/p>\n<p>    return len(matrix) == rows<\/p>\n<p>if is_valid_matrix(matrix, rows, columns):<\/p>\n<p>    print(&quot;Valid matrix&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid matrix&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u7528\u6237\u8f93\u5165\u7684\u77e9\u9635\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u83b7\u53d6\u4e86\u7528\u6237\u8f93\u5165\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u8ba1\u7b97\u77e9\u9635\u7684\u8f6c\u7f6e<\/p>\n<p>transpose = [[matrix[j][i] for j in range(rows)] for i in range(columns)]<\/p>\n<p>print(&quot;Transpose:&quot;)<\/p>\n<p>for row in transpose:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u6587\u4ef6\u8f93\u5165<\/h2>\n<\/p>\n<p><p>\u5728\u6570\u636e\u91cf\u8f83\u5927\u6216\u9700\u8981\u4fdd\u5b58\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>1. \u4ece\u6587\u4ef6\u8bfb\u53d6\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u8bfb\u53d6\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u7684\u77e9\u9635\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;matrix.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    matrix = [list(map(int, line.split())) for line in file]<\/p>\n<p>print(&quot;Matrix from file:&quot;)<\/p>\n<p>for row in matrix:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5c06\u77e9\u9635\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u77e9\u9635\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u4ee5\u4fbf\u540e\u7eed\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output_matrix.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for row in matrix:<\/p>\n<p>        file.write(&#39; &#39;.join(map(str, row)) + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u7684\u8def\u5f84\u3001\u7f16\u7801\u548c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;matrix.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>        matrix = [list(map(int, line.split())) for line in file]<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;File not found. Please check the file path.&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;File cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns non-integer values.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8f93\u5165\u77e9\u9635\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u5d4c\u5957\u5217\u8868\u9002\u5408\u7b80\u5355\u573a\u666f\uff0cNumPy\u5e93\u9002\u5408\u590d\u6742\u6570\u5b66\u8fd0\u7b97\uff0cPandas\u5e93\u9002\u5408\u6570\u636e\u5206\u6790\uff0c\u7528\u6237\u8f93\u5165\u548c\u6587\u4ef6\u8f93\u5165\u9002\u5408\u52a8\u6001\u548c\u6301\u4e45\u5316\u6570\u636e\u5b58\u50a8<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u8f93\u5165\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u8f93\u5165\u77e9\u9635\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install numpy<\/code>\u5b8c\u6210\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u77e9\u9635\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.array([[1, 2], [3, 4]])\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u8f93\u5165\u77e9\u9635\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u5165\u77e9\u9635\uff0c\u4f8b\u5982\u624b\u52a8\u8f93\u5165\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6\u6216\u4f7f\u7528\u968f\u673a\u751f\u6210\u7684\u6570\u503c\u3002\u624b\u52a8\u8f93\u5165\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u7684\u5f62\u5f0f\u3002\u82e5\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.loadtxt()<\/code>\u6216<code>numpy.genfromtxt()<\/code>\u51fd\u6570\uff0c\u65b9\u4fbf\u5730\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u5bf9\u4e8e\u968f\u673a\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.rand()<\/code>\u51fd\u6570\u751f\u6210\u6307\u5b9a\u5f62\u72b6\u7684\u77e9\u9635\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u8f93\u5165\u7684\u77e9\u9635\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/>\u5728\u8f93\u5165\u77e9\u9635\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u77e9\u9635\u7684\u5f62\u72b6\u548c\u6570\u636e\u7c7b\u578b\u6765\u9a8c\u8bc1\u5176\u6709\u6548\u6027\u3002\u4f7f\u7528NumPy\u7684<code>shape<\/code>\u5c5e\u6027\u53ef\u4ee5\u67e5\u770b\u77e9\u9635\u7684\u7ef4\u5ea6\uff0c\u4f8b\u5982\uff1a<code>matrix.shape<\/code>\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>dtype<\/code>\u5c5e\u6027\u53ef\u4ee5\u786e\u8ba4\u77e9\u9635\u4e2d\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<code>matrix.dtype<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u77e9\u9635\u7b26\u5408\u9884\u671f\u7684\u7ed3\u6784\u548c\u7c7b\u578b\uff0c\u907f\u514d\u5728\u540e\u7eed\u64cd\u4f5c\u4e2d\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u5165\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\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u3002\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1010741,"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\/1010736"}],"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=1010736"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010736\/revisions"}],"predecessor-version":[{"id":1010742,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010736\/revisions\/1010742"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010741"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}