{"id":1169590,"date":"2025-01-15T16:12:32","date_gmt":"2025-01-15T08:12:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1169590.html"},"modified":"2025-01-15T16:12:35","modified_gmt":"2025-01-15T08:12:35","slug":"python3-%e5%a6%82%e4%bd%95%e8%ae%be%e8%ae%a1%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1169590.html","title":{"rendered":"python3 \u5982\u4f55\u8bbe\u8ba1\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070525\/ea22e2c6-697a-46a3-886d-86eb2057dcce.webp\" alt=\"python3 \u5982\u4f55\u8bbe\u8ba1\u77e9\u9635\" \/><\/p>\n<p><p> <strong>Python3 \u8bbe\u8ba1\u77e9\u9635\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001Pandas\u5e93\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\uff0c\u4f7f\u5f97\u77e9\u9635\u8fd0\u7b97\u975e\u5e38\u65b9\u4fbf\u548c\u9ad8\u6548\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u8bbe\u8ba1\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165NumPy\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528NumPy\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\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\u5728Python\u811a\u672c\u6216\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u5bfc\u5165NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u77e9\u9635<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u867d\u7136NumPy\u662f\u5904\u7406\u77e9\u9635\u7684\u9996\u9009\u5e93\uff0c\u4f46\u5d4c\u5957\u5217\u8868\u4e5f\u662f\u521b\u5efa\u77e9\u9635\u7684\u4e00\u79cd\u57fa\u672c\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\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>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u77e9\u9635\uff0c\u4f46\u5bf9\u4e8e\u5927\u578b\u77e9\u9635\uff0c\u6548\u7387\u8f83\u4f4e\u4e14\u64cd\u4f5c\u4e0d\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528NumPy\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u77e9\u9635\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>a\u3001\u4ece\u5d4c\u5957\u5217\u8868\u521b\u5efaNumPy\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">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><\/code><\/pre>\n<\/p>\n<p><h4>b\u3001\u521b\u5efa\u5168\u96f6\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">zero_matrix = np.zeros((3, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>c\u3001\u521b\u5efa\u5168\u4e00\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">one_matrix = np.ones((3, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>d\u3001\u521b\u5efa\u5355\u4f4d\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">identity_matrix = np.eye(3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>e\u3001\u521b\u5efa\u968f\u673a\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">random_matrix = np.random.rand(3, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u77e9\u9635\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>1\u3001\u77e9\u9635\u52a0\u6cd5\u548c\u51cf\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u52a0\u51cf\u6cd5\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\uff0c\u76f4\u63a5\u4f7f\u7528\u52a0\u51cf\u53f7\u5373\u53ef\uff1a<\/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>diff_matrix = matrix1 - matrix2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u77e9\u9635\u4e58\u6cd5<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u77e9\u9635\u4e58\u6cd5\u65b9\u6cd5\uff0c\u5305\u62ec\u5143\u7d20\u4e58\u6cd5\u548c\u77e9\u9635\u4e58\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>a\u3001\u5143\u7d20\u4e58\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">elementwise_product = matrix1 * matrix2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b\u3001\u77e9\u9635\u4e58\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_product = np.dot(matrix1, matrix2)<\/p>\n<h2><strong>\u6216\u8005\u4f7f\u7528 @ \u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p>matrix_product = matrix1 @ matrix2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u77e9\u9635\u8f6c\u7f6e<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u8f6c\u7f6e\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>transpose<\/code>\u65b9\u6cd5\u6216\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transposed_matrix = matrix1.T<\/p>\n<h2><strong>\u6216\u8005<\/strong><\/h2>\n<p>transposed_matrix = np.transpose(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u77e9\u9635\u6c42\u9006<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u6c42\u9006\u662f\u7ebf\u6027\u4ee3\u6570\u4e2d\u4e00\u4e2a\u91cd\u8981\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>linalg.inv<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inverse_matrix = np.linalg.inv(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u77e9\u9635\u884c\u5217\u5f0f<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u5217\u5f0f\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>linalg.det<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">determinant = np.linalg.det(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6\u3001\u77e9\u9635\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/h3>\n<\/p>\n<p><p>\u6c42\u89e3\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>linalg.eig<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">eigenvalues, eigenvectors = np.linalg.eig(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001Pandas\u5e93\u4e2d\u7684\u77e9\u9635\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u867d\u7136NumPy\u662f\u5904\u7406\u77e9\u9635\u7684\u9996\u9009\u5e93\uff0c\u4f46Pandas\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u4fbf\u7684\u77e9\u9635\u64cd\u4f5c\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u65b9\u9762\u3002Pandas\u7684DataFrame\u5bf9\u8c61\u53ef\u4ee5\u770b\u4f5c\u662f\u5e26\u6709\u6807\u7b7e\u7684\u77e9\u9635\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u521b\u5efaDataFrame<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001DataFrame\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>a\u3001\u9009\u62e9\u5217<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">column_a = df[&#39;A&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b\u3001\u9009\u62e9\u884c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">row_1 = df.loc[0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>c\u3001\u77e9\u9635\u52a0\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">df2 = df + df<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>d\u3001\u77e9\u9635\u4e58\u6cd5<\/h4>\n<\/p>\n<p><p>Pandas\u7684DataFrame\u5bf9\u8c61\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u77e9\u9635\u4e58\u6cd5\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528NumPy\u7684\u65b9\u6cd5\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_product = df.dot(df.T)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u77e9\u9635\u8bbe\u8ba1<\/h2>\n<\/p>\n<p><h3>1\u3001\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u4e09\u7ef4\u77e9\u9635\uff08\u9ad8\u5ea6\u00d7\u5bbd\u5ea6\u00d7\u989c\u8272\u901a\u9053\uff09\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u56fe\u50cf\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>image = Image.open(&#39;path\/to\/image.jpg&#39;)<\/p>\n<p>image_matrix = np.array(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h3>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u77e9\u9635\u7684\u5f62\u5f0f\u8868\u793a\u3002\u7279\u5f81\u77e9\u9635\u548c\u6807\u7b7e\u77e9\u9635\u662f\u5e38\u89c1\u7684\u8868\u793a\u5f62\u5f0f\u3002\u4f7f\u7528NumPy\u548cPandas\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u8fd9\u4e9b\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u5c06CSV\u6587\u4ef6\u8bfb\u53d6\u4e3a\u7279\u5f81\u77e9\u9635\u548c\u6807\u7b7e\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_csv(&#39;path\/to\/data.csv&#39;)<\/p>\n<p>features = data.iloc[:, :-1].values<\/p>\n<p>labels = data.iloc[:, -1].values<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u79d1\u5b66\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\uff0c\u77e9\u9635\u8fd0\u7b97\u662f\u5e38\u89c1\u64cd\u4f5c\u3002NumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u79d1\u5b66\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">coefficients = np.array([[2, 1], [1, 2]])<\/p>\n<p>constants = np.array([5, 7])<\/p>\n<p>solutions = np.linalg.solve(coefficients, constants)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u77e9\u9635\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>1\u3001\u77e9\u9635\u5206\u89e3<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u5206\u89e3\u662f\u7ebf\u6027\u4ee3\u6570\u4e2d\u7684\u91cd\u8981\u64cd\u4f5c\uff0cNumPy\u63d0\u4f9b\u4e86\u591a\u79cd\u77e9\u9635\u5206\u89e3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>a\u3001LU\u5206\u89e3<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.linalg import lu<\/p>\n<p>P, L, U = lu(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b\u3001QR\u5206\u89e3<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">Q, R = np.linalg.qr(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>c\u3001SVD\u5206\u89e3<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">U, S, V = np.linalg.svd(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u7a00\u758f\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u77e9\u9635\u4e2d\u5927\u90e8\u5206\u5143\u7d20\u4e3a\u96f6\uff0c\u6b64\u65f6\u4f7f\u7528\u7a00\u758f\u77e9\u9635\u53ef\u4ee5\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u548c\u8ba1\u7b97\u65f6\u95f4\u3002\u53ef\u4ee5\u4f7f\u7528SciPy\u5e93\u521b\u5efa\u7a00\u758f\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.sparse import csr_matrix<\/p>\n<p>sparse_matrix = csr_matrix(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u77e9\u9635\u7684\u9ad8\u7ea7\u7edf\u8ba1\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u77e9\u9635\u7684\u7edf\u8ba1\u5206\u6790\u662f\u5e38\u89c1\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u77e9\u9635\u6bcf\u5217\u7684\u5747\u503c\u548c\u6807\u51c6\u5dee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_means = np.mean(matrix1, axis=0)<\/p>\n<p>column_stds = np.std(matrix1, axis=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Python3\u8bbe\u8ba1\u548c\u64cd\u4f5c\u77e9\u9635\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001Pandas\u5e93\u7b49\u3002\u5176\u4e2d\uff0cNumPy\u5e93\u7531\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u7b49\u9886\u57df\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u8bfb\u8005\u5df2\u7ecf\u638c\u63e1\u4e86Python3\u4e2d\u77e9\u9635\u7684\u57fa\u672c\u521b\u5efa\u65b9\u6cd5\u3001\u5e38\u89c1\u64cd\u4f5c\u4ee5\u53ca\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f7f\u7528\u6280\u5de7\u3002\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u53ef\u4ee5\u901a\u8fc7\u67e5\u9605NumPy\u548cPandas\u7684\u5b98\u65b9\u6587\u6863\uff0c\u4e86\u89e3\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u521b\u5efa\u77e9\u9635\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u53cc\u91cd\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u7b80\u5355\u7684\u5217\u8868\u5b9a\u4e49\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u77e9\u9635\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6]]\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u77e9\u9635\u64cd\u4f5c\u3002\u4f7f\u7528NumPy\u521b\u5efa\u77e9\u9635\u7684\u65b9\u5f0f\u5982\u4e0b\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><strong>\u5728Python3\u4e2d\u5982\u4f55\u5bf9\u77e9\u9635\u8fdb\u884c\u57fa\u672c\u64cd\u4f5c\uff1f<\/strong><br \/>\u77e9\u9635\u7684\u57fa\u672c\u64cd\u4f5c\u5305\u62ec\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u8f6c\u7f6e\u3002\u5728\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u65f6\uff0c\u60a8\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u77e9\u9635\u76f8\u52a0\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]\n<\/code><\/pre>\n<p>\u4f7f\u7528NumPy\u5219\u66f4\u4e3a\u7b80\u5355\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd0\u7b97\u7b26\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">result = np.add(matrix1, matrix2)\n<\/code><\/pre>\n<p>NumPy\u8fd8\u63d0\u4f9b\u4e86\u8f6c\u7f6e\u7684\u7b80\u5355\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\">transposed_matrix = np.transpose(matrix)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python3\u4e2d\u7684\u77e9\u9635\u8fdb\u884c\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff1f<\/strong><br \/>Python3\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u8fdb\u884c\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0cNumPy\u662f\u5176\u4e2d\u6700\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u3002\u4f7f\u7528NumPy\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\u3001\u6c42\u9006\u3001\u6c42\u7279\u5f81\u503c\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u77e9\u9635\u4e58\u6cd5\u53ef\u4ee5\u901a\u8fc7<code>np.dot()<\/code>\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">result = np.dot(matrix1, matrix2)\n<\/code><\/pre>\n<p>\u8981\u8ba1\u7b97\u77e9\u9635\u7684\u9006\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>np.linalg.inv()<\/code>\uff1a  <\/p>\n<pre><code class=\"language-python\">inverse_matrix = np.linalg.inv(matrix)\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0cNumPy\u8fd8\u652f\u6301\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>np.linalg.solve()<\/code>\u6765\u5b8c\u6210\u8fd9\u4e00\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python3 \u8bbe\u8ba1\u77e9\u9635\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001Pandas\u5e93\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1169596,"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\/1169590"}],"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=1169590"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169590\/revisions"}],"predecessor-version":[{"id":1169599,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1169590\/revisions\/1169599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1169596"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1169590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1169590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1169590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}