{"id":1178325,"date":"2025-01-15T18:09:28","date_gmt":"2025-01-15T10:09:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178325.html"},"modified":"2025-01-15T18:09:31","modified_gmt":"2025-01-15T10:09:31","slug":"%e7%9f%a9%e9%98%b5%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178325.html","title":{"rendered":"\u77e9\u9635\u5728python\u4e2d\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113015\/9eb310e3-c66e-4037-ad9b-7556619dac74.webp\" alt=\"\u77e9\u9635\u5728python\u4e2d\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> \u77e9\u9635\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7<strong>\u5217\u8868\u5d4c\u5957\u3001NumPy\u5e93\u3001Pandas\u5e93<\/strong>\u7b49\u65b9\u5f0f\u8868\u793a\u3002<strong>\u4f7f\u7528NumPy\u5e93<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\u548c\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u80fd\u529b\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u6765\u8868\u793a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u5d4c\u5957\u8868\u793a\u77e9\u9635<\/h3>\n<\/p>\n<p><h4>1.1 \u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u6765\u8868\u793a\u77e9\u9635\u3002\u6bcf\u4e2a\u5185\u90e8\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\uff0c\u591a\u4e2a\u5185\u90e8\u5217\u8868\u7ec4\u6210\u6574\u4e2a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a 3x3 \u7684\u77e9\u9635<\/p>\n<p>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>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u8bbf\u95ee\u77e9\u9635\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/p>\n<p>element = matrix[1][2]<\/p>\n<p>print(element)  # \u8f93\u51fa6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u8868\u793a\u77e9\u9635<\/h3>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5NumPy\u5e93<\/h4>\n<\/p>\n<p><p>NumPy\u662f\u7528\u4e8e\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u6838\u5fc3\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u652f\u6301\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u521b\u5efa\u77e9\u9635\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7<code>numpy.array<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a 3x3 \u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u8bbf\u95ee\u77e9\u9635\u5143\u7d20<\/h4>\n<\/p>\n<p><p>NumPy\u77e9\u9635\u7684\u5143\u7d20\u8bbf\u95ee\u548c\u5217\u8868\u5d4c\u5957\u7c7b\u4f3c\uff0c\u4f46\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/p>\n<p>element = matrix[1, 2]<\/p>\n<p>print(element)  # \u8f93\u51fa6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.4 \u77e9\u9635\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u8fd0\u7b97\u652f\u6301\uff0c\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/p>\n<p>matrix1 = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p>matrix2 = np.array([<\/p>\n<p>    [9, 8, 7],<\/p>\n<p>    [6, 5, 4],<\/p>\n<p>    [3, 2, 1]<\/p>\n<p>])<\/p>\n<h2><strong>\u77e9\u9635\u52a0\u6cd5<\/strong><\/h2>\n<p>result_add = np.add(matrix1, matrix2)<\/p>\n<p>print(result_add)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>result_mul = np.dot(matrix1, matrix2)<\/p>\n<p>print(result_mul)<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>result_transpose = np.transpose(matrix1)<\/p>\n<p>print(result_transpose)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u8868\u793a\u77e9\u9635<\/h3>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5Pandas\u5e93<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684DataFrame\u6765\u8868\u793a\u77e9\u9635\uff0cDataFrame\u662f\u4e00\u4e2a\u4e8c\u7ef4\u7684\u8868\u683c\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a 3x3 \u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = pd.DataFrame([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u8bbf\u95ee\u77e9\u9635\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u884c\u5217\u7d22\u5f15\u6765\u8bbf\u95eeDataFrame\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/p>\n<p>element = matrix.iloc[1, 2]<\/p>\n<p>print(element)  # \u8f93\u51fa6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u77e9\u9635\u8fd0\u7b97\u7684\u8be6\u7ec6\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><h4>4.1 \u77e9\u9635\u52a0\u6cd5<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u52a0\u6cd5\u662f\u6307\u4e24\u4e2a\u76f8\u540c\u5927\u5c0f\u7684\u77e9\u9635\u5bf9\u5e94\u5143\u7d20\u76f8\u52a0\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u52a0\u6cd5<\/p>\n<p>result_add = np.add(matrix1, matrix2)<\/p>\n<p>print(result_add)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u77e9\u9635\u51cf\u6cd5<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u51cf\u6cd5\u662f\u6307\u4e24\u4e2a\u76f8\u540c\u5927\u5c0f\u7684\u77e9\u9635\u5bf9\u5e94\u5143\u7d20\u76f8\u51cf\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u51cf\u6cd5<\/p>\n<p>result_sub = np.subtract(matrix1, matrix2)<\/p>\n<p>print(result_sub)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u77e9\u9635\u4e58\u6cd5<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u4e58\u6cd5\u4e0d\u662f\u5bf9\u5e94\u5143\u7d20\u76f8\u4e58\uff0c\u800c\u662f\u77e9\u9635\u7684\u884c\u4e58\u4ee5\u53e6\u4e00\u4e2a\u77e9\u9635\u7684\u5217\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u3002\u9700\u8981\u6ce8\u610f\u77e9\u9635\u4e58\u6cd5\u7684\u524d\u63d0\u662f\u7b2c\u4e00\u4e2a\u77e9\u9635\u7684\u5217\u6570\u7b49\u4e8e\u7b2c\u4e8c\u4e2a\u77e9\u9635\u7684\u884c\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/p>\n<p>result_mul = np.dot(matrix1, matrix2)<\/p>\n<p>print(result_mul)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.4 \u77e9\u9635\u8f6c\u7f6e<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u8f6c\u7f6e\u662f\u6307\u5c06\u77e9\u9635\u7684\u884c\u548c\u5217\u4e92\u6362\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u8f6c\u7f6e<\/p>\n<p>result_transpose = np.transpose(matrix1)<\/p>\n<p>print(result_transpose)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.5 \u77e9\u9635\u7684\u884c\u5217\u5f0f<\/h4>\n<\/p>\n<p><p>\u884c\u5217\u5f0f\u662f\u77e9\u9635\u7684\u4e00\u4e2a\u91cd\u8981\u5c5e\u6027\uff0c\u7528\u4e8e\u89e3\u51b3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u3001\u77e9\u9635\u7684\u9006\u7b49\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u5217\u5f0f<\/p>\n<p>det = np.linalg.det(matrix1)<\/p>\n<p>print(det)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.6 \u77e9\u9635\u7684\u9006<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u7684\u9006\u662f\u6307\u4e00\u4e2a\u77e9\u9635\u4e0e\u5176\u9006\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u5355\u4f4d\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8ba1\u7b97\u77e9\u9635\u7684\u9006<\/p>\n<p>inverse_matrix = np.linalg.inv(matrix1)<\/p>\n<p>print(inverse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u77e9\u9635\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>5.1 \u7ebf\u6027\u4ee3\u6570<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u5728\u7ebf\u6027\u4ee3\u6570\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5982\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u3001\u6c42\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ebf\u6027\u65b9\u7a0b\u7ec4 Ax = B \u7684\u89e3<\/p>\n<p>A = np.array([<\/p>\n<p>    [3, 1],<\/p>\n<p>    [1, 2]<\/p>\n<p>])<\/p>\n<p>B = np.array([<\/p>\n<p>    [9],<\/p>\n<p>    [8]<\/p>\n<p>])<\/p>\n<h2><strong>\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4<\/strong><\/h2>\n<p>X = np.linalg.solve(A, B)<\/p>\n<p>print(X)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u8ba1\u7b97\u673a\u56fe\u5f62\u5b66<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u5728\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u4e2d\u7528\u4e8e\u56fe\u5f62\u53d8\u6362\uff0c\u5982\u5e73\u79fb\u3001\u65cb\u8f6c\u3001\u7f29\u653e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5e73\u79fb\u77e9\u9635<\/p>\n<p>translation_matrix = np.array([<\/p>\n<p>    [1, 0, 2],<\/p>\n<p>    [0, 1, 3],<\/p>\n<p>    [0, 0, 1]<\/p>\n<p>])<\/p>\n<h2><strong>\u65cb\u8f6c\u77e9\u9635\uff08\u9006\u65f6\u9488\u65cb\u8f6c45\u5ea6\uff09<\/strong><\/h2>\n<p>theta = np.radians(45)<\/p>\n<p>cos_theta, sin_theta = np.cos(theta), np.sin(theta)<\/p>\n<p>rotation_matrix = np.array([<\/p>\n<p>    [cos_theta, -sin_theta, 0],<\/p>\n<p>    [sin_theta, cos_theta, 0],<\/p>\n<p>    [0, 0, 1]<\/p>\n<p>])<\/p>\n<h2><strong>\u7f29\u653e\u77e9\u9635<\/strong><\/h2>\n<p>scaling_matrix = np.array([<\/p>\n<p>    [2, 0, 0],<\/p>\n<p>    [0, 2, 0],<\/p>\n<p>    [0, 0, 1]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3 <a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u5728\u673a\u5668\u5b66\u4e60\u4e2d\u7528\u4e8e\u8868\u793a\u6570\u636e\u96c6\u3001\u8ba1\u7b97\u68af\u5ea6\u3001\u8bad\u7ec3\u6a21\u578b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8868\u793a\u6570\u636e\u96c6<\/p>\n<p>data = np.array([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4],<\/p>\n<p>    [5, 6]<\/p>\n<p>])<\/p>\n<h2><strong>\u8ba1\u7b97\u68af\u5ea6<\/strong><\/h2>\n<p>weights = np.array([0.1, 0.2])<\/p>\n<p>predictions = np.dot(data, weights)<\/p>\n<p>errors = predictions - np.array([1, 2, 3])<\/p>\n<p>gradient = np.dot(data.T, errors)<\/p>\n<p>print(gradient)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u77e9\u9635\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>6.1 \u77e9\u9635\u7684\u5206\u5757<\/h4>\n<\/p>\n<p><p>\u77e9\u9635\u53ef\u4ee5\u5206\u4e3a\u82e5\u5e72\u4e2a\u5b50\u77e9\u9635\uff0c\u8fdb\u884c\u5206\u5757\u64cd\u4f5c\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u77e9\u9635\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u5206\u5757<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3, 4],<\/p>\n<p>    [5, 6, 7, 8],<\/p>\n<p>    [9, 10, 11, 12],<\/p>\n<p>    [13, 14, 15, 16]<\/p>\n<p>])<\/p>\n<h2><strong>\u5206\u5757\u4e3a\u56db\u4e2a 2x2 \u7684\u5b50\u77e9\u9635<\/strong><\/h2>\n<p>sub_matrix1 = matrix[:2, :2]<\/p>\n<p>sub_matrix2 = matrix[:2, 2:]<\/p>\n<p>sub_matrix3 = matrix[2:, :2]<\/p>\n<p>sub_matrix4 = matrix[2:, 2:]<\/p>\n<p>print(sub_matrix1)<\/p>\n<p>print(sub_matrix2)<\/p>\n<p>print(sub_matrix3)<\/p>\n<p>print(sub_matrix4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u77e9\u9635\u7684\u62fc\u63a5<\/h4>\n<\/p>\n<p><p>\u5c06\u591a\u4e2a\u77e9\u9635\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u6784\u6210\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u62fc\u63a5<\/p>\n<p>matrix1 = np.array([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4]<\/p>\n<p>])<\/p>\n<p>matrix2 = np.array([<\/p>\n<p>    [5, 6],<\/p>\n<p>    [7, 8]<\/p>\n<p>])<\/p>\n<h2><strong>\u6309\u7167\u884c\u62fc\u63a5<\/strong><\/h2>\n<p>result_hstack = np.hstack((matrix1, matrix2))<\/p>\n<p>print(result_hstack)<\/p>\n<h2><strong>\u6309\u7167\u5217\u62fc\u63a5<\/strong><\/h2>\n<p>result_vstack = np.vstack((matrix1, matrix2))<\/p>\n<p>print(result_vstack)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3 \u77e9\u9635\u7684\u5e7f\u64ad<\/h4>\n<\/p>\n<p><p>\u5e7f\u64ad\u662f\u4e00\u79cd\u5904\u7406\u4e0d\u540c\u5f62\u72b6\u7684\u77e9\u9635\u8fd0\u7b97\u7684\u65b9\u6cd5\uff0c\u4f7f\u5f97\u8f83\u5c0f\u7684\u77e9\u9635\u5728\u8ba1\u7b97\u65f6\u81ea\u52a8\u6269\u5c55\u5230\u4e0e\u8f83\u5927\u77e9\u9635\u76f8\u540c\u7684\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u5e7f\u64ad<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>vector = np.array([1, 2, 3])<\/p>\n<h2><strong>\u8fdb\u884c\u5e7f\u64ad\u8fd0\u7b97<\/strong><\/h2>\n<p>result = matrix + vector<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u77e9\u9635\u7684\u7a00\u758f\u8868\u793a<\/h3>\n<\/p>\n<p><h4>7.1 \u7a00\u758f\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u7a00\u758f\u77e9\u9635\u662f\u6307\u5927\u90e8\u5206\u5143\u7d20\u4e3a\u96f6\u7684\u77e9\u9635\uff0c\u4f7f\u7528\u7a00\u758f\u77e9\u9635\u8868\u793a\u53ef\u4ee5\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u548c\u8ba1\u7b97\u65f6\u95f4\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([<\/p>\n<p>    [1, 0, 0],<\/p>\n<p>    [0, 0, 3],<\/p>\n<p>    [4, 0, 0]<\/p>\n<p>])<\/p>\n<p>print(sparse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u7a00\u758f\u77e9\u9635\u7684\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u7a00\u758f\u77e9\u9635\u7684\u8fd0\u7b97\u4e0e\u666e\u901a\u77e9\u9635\u7c7b\u4f3c\uff0c\u4f46\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u7a00\u758f\u77e9\u9635\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/p>\n<p>sparse_matrix1 = csr_matrix([<\/p>\n<p>    [1, 0, 0],<\/p>\n<p>    [0, 0, 3],<\/p>\n<p>    [4, 0, 0]<\/p>\n<p>])<\/p>\n<p>sparse_matrix2 = csr_matrix([<\/p>\n<p>    [0, 2, 0],<\/p>\n<p>    [0, 0, 0],<\/p>\n<p>    [0, 0, 5]<\/p>\n<p>])<\/p>\n<p>result_sparse_mul = sparse_matrix1.dot(sparse_matrix2)<\/p>\n<p>print(result_sparse_mul)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u77e9\u9635\u7684\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><h4>8.1 \u4f7f\u7528Matplotlib\u5e93<\/h4>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u77e9\u9635\u7684\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<h2><strong>\u7ed8\u5236\u77e9\u9635<\/strong><\/h2>\n<p>plt.imshow(matrix, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2 \u70ed\u529b\u56fe<\/h4>\n<\/p>\n<p><p>\u70ed\u529b\u56fe\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u77e9\u9635\u53ef\u89c6\u5316\u65b9\u6cd5\uff0c\u901a\u8fc7\u989c\u8272\u8868\u793a\u77e9\u9635\u4e2d\u7684\u6570\u503c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<h2><strong>\u7ed8\u5236\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>sns.heatmap(matrix, annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a\u77e9\u9635\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u77e9\u9635\u8fd0\u7b97\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u77e9\u9635\u7684\u57fa\u672c\u64cd\u4f5c\u548c\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u53ef\u4ee5\u89e3\u51b3\u5404\u79cd\u590d\u6742\u7684\u8ba1\u7b97\u95ee\u9898\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\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6]]\n<\/code><\/pre>\n<p>\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u5efa\u8bae\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u5e93\u65f6\u5982\u4f55\u5b9a\u4e49\u548c\u64cd\u4f5c\u77e9\u9635\uff1f<\/strong><br \/>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684N\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u77e9\u9635\u3002\u4f7f\u7528NumPy\u521b\u5efa\u77e9\u9635\u975e\u5e38\u7b80\u5355\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.array([[1, 2, 3], [4, 5, 6]])\n<\/code><\/pre>\n<p>\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u77e9\u9635\u7684\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix_transpose = matrix.T  # \u8f6c\u7f6e\nmatrix_product = np.dot(matrix, matrix.T)  # \u77e9\u9635\u4e58\u6cd5\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4ece\u6587\u4ef6\u4e2d\u5bfc\u5165\u77e9\u9635\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u5bfc\u5165\u77e9\u9635\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>loadtxt<\/code>\u6216<code>genfromtxt<\/code>\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = np.loadtxt(&#39;matrix_data.txt&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u6570\u636e\u6587\u4ef6\u4e2d\u7684\u683c\u5f0f\u6b63\u786e\uff0c\u901a\u5e38\u8981\u6c42\u6bcf\u884c\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\uff0c\u6570\u503c\u4e4b\u95f4\u7528\u7a7a\u683c\u6216\u9017\u53f7\u5206\u9694\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u77e9\u9635\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u5d4c\u5957\u3001NumPy\u5e93\u3001Pandas\u5e93\u7b49\u65b9\u5f0f\u8868\u793a\u3002\u4f7f\u7528NumPy\u5e93\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1178330,"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\/1178325"}],"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=1178325"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178325\/revisions"}],"predecessor-version":[{"id":1178331,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178325\/revisions\/1178331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178330"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}