{"id":1122367,"date":"2025-01-08T19:19:51","date_gmt":"2025-01-08T11:19:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1122367.html"},"modified":"2025-01-08T19:19:54","modified_gmt":"2025-01-08T11:19:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%bb%e5%87%ba%e5%85%83%e7%b4%a0%e5%85%a8%e4%b8%ba1%e7%9a%84%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1122367.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u753b\u51fa\u5143\u7d20\u5168\u4e3a1\u7684\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084241\/99a82f11-729e-4e18-94db-263a80ab2a4d.webp\" alt=\"python\u4e2d\u5982\u4f55\u753b\u51fa\u5143\u7d20\u5168\u4e3a1\u7684\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u4e00\u4e2a\u5143\u7d20\u5168\u4e3a1\u7684\u77e9\u9635\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u5bfc\u5165NumPy\u5e93\u3001\u4f7f\u7528<code>ones<\/code>\u51fd\u6570\u521b\u5efa\u77e9\u9635\u3001\u6307\u5b9a\u77e9\u9635\u7684\u5f62\u72b6\u3002<\/strong> \u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.ones((m, n))<\/code>\u6765\u521b\u5efa\u4e00\u4e2am\u884cn\u5217\u7684\u51681\u77e9\u9635\u3002\u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u5143\u7d20\u5168\u4e3a1\u7684\u77e9\u9635\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u89e3\u91ca\u76f8\u5173\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165NumPy\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528NumPy\u5e93\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\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\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\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><p>\u4e8c\u3001\u521b\u5efa\u51681\u77e9\u9635<\/p>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570<code>ones<\/code>\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5f62\u72b6\u7684\u51681\u77e9\u9635\u3002\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numpy.ones(shape, dtype=float, order=&#39;C&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>shape<\/code>\uff1a\u8868\u793a\u77e9\u9635\u7684\u5f62\u72b6\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u6574\u6570\u6216\u8005\u4e00\u4e2a\u5143\u7ec4\uff08\u4f8b\u5982\uff1a(m, n)\uff09\u3002<\/li>\n<li><code>dtype<\/code>\uff1a\u6307\u5b9a\u77e9\u9635\u4e2d\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\uff0c\u9ed8\u8ba4\u4e3a<code>float<\/code>\u3002<\/li>\n<li><code>order<\/code>\uff1a\u6307\u5b9a\u591a\u7ef4\u6570\u636e\u7684\u5b58\u50a8\u987a\u5e8f\uff0c\u9ed8\u8ba4\u4e3a&#39;C&#39;\uff08\u6309\u884c\u5b58\u50a8\uff09\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3\u884c4\u5217\u7684\u51681\u77e9\u9635<\/p>\n<p>matrix_1 = np.ones((3, 4))<\/p>\n<p>print(matrix_1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6307\u5b9a\u77e9\u9635\u5f62\u72b6<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u77e9\u9635\u65f6\uff0c\u5f62\u72b6\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u53c2\u6570\u3002\u5f62\u72b6\u51b3\u5b9a\u4e86\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2\u884c3\u5217\u7684\u51681\u77e9\u9635<\/p>\n<p>matrix_2 = np.ones((2, 3))<\/p>\n<p>print(matrix_2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u4e2a2\u884c3\u5217\u7684\u51681\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4fee\u6539\u5143\u7d20\u7c7b\u578b<\/p>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>ones<\/code>\u51fd\u6570\u521b\u5efa\u7684\u77e9\u9635\u5143\u7d20\u7c7b\u578b\u4e3a\u6d6e\u70b9\u6570\u3002\u5982\u679c\u9700\u8981\u521b\u5efa\u6574\u6570\u7c7b\u578b\u7684\u51681\u77e9\u9635\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dtype<\/code>\u53c2\u6570\u8fdb\u884c\u6307\u5b9a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u76842\u884c2\u5217\u51681\u77e9\u9635<\/p>\n<p>matrix_3 = np.ones((2, 2), dtype=int)<\/p>\n<p>print(matrix_3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u66f4\u591a\u5173\u4e8e\u77e9\u9635\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u521b\u5efa\u51681\u77e9\u9635\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\uff0c\u4e0b\u9762\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u521d\u59cb\u5316\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>ones<\/code>\u51fd\u6570\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u521d\u59cb\u5316\u77e9\u9635\u7684\u51fd\u6570\uff0c\u4f8b\u5982<code>zeros<\/code>\u3001<code>full<\/code>\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u51680\u77e9\u9635<\/p>\n<p>matrix_zeros = np.zeros((3, 3))<\/p>\n<p>print(matrix_zeros)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6240\u6709\u5143\u7d20\u4e3a7\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix_full = np.full((3, 3), 7)<\/p>\n<p>print(matrix_full)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u57fa\u672c\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>NumPy\u652f\u6301\u77e9\u9635\u7684\u57fa\u672c\u8fd0\u7b97\uff0c\u4f8b\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/p>\n<p>matrix_a = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix_b = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u77e9\u9635\u52a0\u6cd5<\/strong><\/h2>\n<p>matrix_sum = np.add(matrix_a, matrix_b)<\/p>\n<p>print(matrix_sum)<\/p>\n<h2><strong>\u77e9\u9635\u51cf\u6cd5<\/strong><\/h2>\n<p>matrix_diff = np.subtract(matrix_a, matrix_b)<\/p>\n<p>print(matrix_diff)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>matrix_prod = np.dot(matrix_a, matrix_b)<\/p>\n<p>print(matrix_prod)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u8f6c\u7f6e<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>transpose<\/code>\u51fd\u6570\u5bf9\u77e9\u9635\u8fdb\u884c\u8f6c\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_c = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>matrix_transpose = np.transpose(matrix_c)<\/p>\n<p>print(matrix_transpose)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u5207\u7247<\/h3>\n<\/p>\n<p><p>NumPy\u5141\u8bb8\u5bf9\u77e9\u9635\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff0c\u4ee5\u83b7\u53d6\u5b50\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>sub_matrix = matrix_d[0:2, 1:3]<\/p>\n<p>print(sub_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u5f62\u72b6\u53d8\u6362<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u5bf9\u77e9\u9635\u7684\u5f62\u72b6\u8fdb\u884c\u53d8\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_e = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>reshaped_matrix = np.reshape(matrix_e, (3, 2))<\/p>\n<p>print(reshaped_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u62fc\u63a5<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86<code>hstack<\/code>\u548c<code>vstack<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u6c34\u5e73\u548c\u5782\u76f4\u62fc\u63a5\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/p>\n<p>matrix_f = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix_g = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u6c34\u5e73\u62fc\u63a5<\/strong><\/h2>\n<p>matrix_hstack = np.hstack((matrix_f, matrix_g))<\/p>\n<p>print(matrix_hstack)<\/p>\n<h2><strong>\u5782\u76f4\u62fc\u63a5<\/strong><\/h2>\n<p>matrix_vstack = np.vstack((matrix_f, matrix_g))<\/p>\n<p>print(matrix_vstack)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u6c42\u548c\u4e0e\u5e73\u5747\u503c<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86<code>sum<\/code>\u548c<code>mean<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u77e9\u9635\u7684\u6c42\u548c\u4e0e\u5e73\u5747\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_h = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u6c42\u548c<\/strong><\/h2>\n<p>sum_all = np.sum(matrix_h)<\/p>\n<p>print(sum_all)<\/p>\n<h2><strong>\u6309\u5217\u6c42\u548c<\/strong><\/h2>\n<p>sum_column = np.sum(matrix_h, axis=0)<\/p>\n<p>print(sum_column)<\/p>\n<h2><strong>\u6309\u884c\u6c42\u548c<\/strong><\/h2>\n<p>sum_row = np.sum(matrix_h, axis=1)<\/p>\n<p>print(sum_row)<\/p>\n<h2><strong>\u6c42\u5e73\u5747\u503c<\/strong><\/h2>\n<p>mean_all = np.mean(matrix_h)<\/p>\n<p>print(mean_all)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u6807\u51c6\u5dee\u548c\u65b9\u5dee<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>std<\/code>\u548c<code>var<\/code>\u51fd\u6570\u8ba1\u7b97\u77e9\u9635\u7684\u6807\u51c6\u5dee\u548c\u65b9\u5dee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_i = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_all = np.std(matrix_i)<\/p>\n<p>print(std_all)<\/p>\n<h2><strong>\u65b9\u5dee<\/strong><\/h2>\n<p>var_all = np.var(matrix_i)<\/p>\n<p>print(var_all)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u7684\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>sort<\/code>\u51fd\u6570\u5bf9\u77e9\u9635\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix_j = np.array([[3, 1, 2], [6, 4, 5]])<\/p>\n<h2><strong>\u6309\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_matrix = np.sort(matrix_j, axis=1)<\/p>\n<p>print(sorted_matrix)<\/p>\n<h2><strong>\u6309\u5217\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_matrix = np.sort(matrix_j, axis=0)<\/p>\n<p>print(sorted_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5e94\u7528\u5b9e\u4f8b<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528NumPy\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\uff0c\u4e0b\u9762\u5c06\u5c55\u793a\u4e00\u4e9b\u5177\u4f53\u7684\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u53ef\u4ee5\u8868\u793a\u4e3a\u4e8c\u7ef4\u77e9\u9635\u3002NumPy\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8c03\u6574\u4eae\u5ea6\u3001\u65cb\u8f6c\u3001\u88c1\u526a\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u51681\u77e9\u9635\uff0c\u8c03\u6574\u56fe\u50cf\u4eae\u5ea6<\/strong><\/h2>\n<p>brightness_matrix = np.ones(image.shape, dtype=&#39;uint8&#39;) * 50<\/p>\n<p>bright_image = cv2.add(image, brightness_matrix)<\/p>\n<h2><strong>\u663e\u793a\u539f\u56fe\u548c\u8c03\u6574\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.subplot(1, 2, 1)<\/p>\n<p>plt.title(&#39;Original Image&#39;)<\/p>\n<p>plt.imshow(image, cmap=&#39;gray&#39;)<\/p>\n<p>plt.subplot(1, 2, 2)<\/p>\n<p>plt.title(&#39;Bright Image&#39;)<\/p>\n<p>plt.imshow(bright_image, cmap=&#39;gray&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ebf\u6027\u4ee3\u6570\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u7ebf\u6027\u4ee3\u6570\u4e2d\uff0c\u77e9\u9635\u662f\u57fa\u7840\u7684\u6570\u636e\u7ed3\u6784\u3002NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ebf\u6027\u4ee3\u6570\u51fd\u6570\uff0c\u4f8b\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u9006\u77e9\u9635\u3001\u7279\u5f81\u503c\u5206\u89e3\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_k = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u8ba1\u7b97\u9006\u77e9\u9635<\/strong><\/h2>\n<p>inverse_matrix = np.linalg.inv(matrix_k)<\/p>\n<p>print(inverse_matrix)<\/p>\n<h2><strong>\u8ba1\u7b97\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>eigenvalues, eigenvectors = np.linalg.eig(matrix_k)<\/p>\n<p>print(eigenvalues)<\/p>\n<p>print(eigenvectors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u79d1\u5b66\u8ba1\u7b97\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\uff0c\u77e9\u9635\u7528\u4e8e\u8868\u793a\u5404\u79cd\u6570\u636e\u548c\u6a21\u578b\u3002NumPy\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u53ef\u4ee5\u7b80\u5316\u8bb8\u591a\u8ba1\u7b97\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_l = np.random.rand(3, 3)<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u5217\u5f0f<\/strong><\/h2>\n<p>determinant = np.linalg.det(matrix_l)<\/p>\n<p>print(determinant)<\/p>\n<h2><strong>\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4 Ax = b<\/strong><\/h2>\n<p>A = np.array([[3, 1], [1, 2]])<\/p>\n<p>b = np.array([9, 8])<\/p>\n<p>x = np.linalg.solve(A, b)<\/p>\n<p>print(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684NumPy\u5e93\u521b\u5efa\u548c\u64cd\u4f5c\u51681\u77e9\u9635\u3002\u901a\u8fc7\u5bfc\u5165NumPy\u5e93\u3001\u4f7f\u7528<code>ones<\/code>\u51fd\u6570\u521b\u5efa\u77e9\u9635\u3001\u6307\u5b9a\u77e9\u9635\u5f62\u72b6\u548c\u4fee\u6539\u5143\u7d20\u7c7b\u578b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u6240\u9700\u7684\u51681\u77e9\u9635\u3002\u6b64\u5916\uff0c\u672c\u6587\u8fd8\u4ecb\u7ecd\u4e86NumPy\u4e2d\u7684\u5176\u4ed6\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\uff0c\u5982\u521d\u59cb\u5316\u77e9\u9635\u3001\u57fa\u672c\u8fd0\u7b97\u3001\u8f6c\u7f6e\u3001\u5207\u7247\u3001\u5f62\u72b6\u53d8\u6362\u3001\u62fc\u63a5\u3001\u6c42\u548c\u4e0e\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u548c\u65b9\u5dee\u3001\u6392\u5e8f\u7b49\u3002\u6700\u540e\uff0c\u901a\u8fc7\u56fe\u50cf\u5904\u7406\u3001\u7ebf\u6027\u4ee3\u6570\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u7684\u5177\u4f53\u5e94\u7528\u5b9e\u4f8b\uff0c\u5c55\u793a\u4e86NumPy\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\uff0c\u8bfb\u8005\u80fd\u591f\u6df1\u5165\u7406\u89e3\u5982\u4f55\u4f7f\u7528NumPy\u521b\u5efa\u548c\u64cd\u4f5c\u51681\u77e9\u9635\uff0c\u5e76\u638c\u63e1\u66f4\u591a\u7684\u77e9\u9635\u64cd\u4f5c\u6280\u5de7\uff0c\u4ee5\u4fbf\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u4efb\u52a1\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\u5168\u4e3a1\u7684\u77e9\u9635\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u5168\u4e3a1\u7684\u77e9\u9635\u3002\u53ea\u9700\u4f7f\u7528<code>np.ones()<\/code>\u51fd\u6570\uff0c\u5e76\u6307\u5b9a\u77e9\u9635\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c<code>np.ones((3, 4))<\/code>\u5c06\u751f\u6210\u4e00\u4e2a3\u884c4\u5217\u7684\u51681\u77e9\u9635\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5176\u4ed6\u65b9\u6cd5\u6765\u751f\u6210\u5168\u4e3a1\u7684\u77e9\u9635\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPython\u7684\u6807\u51c6\u5e93\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5168\u4e3a1\u7684\u77e9\u9635\uff1a<code>[[1 for _ in range(columns)] for _ in range(rows)]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u4e0d\u5982NumPy\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5168\u4e3a1\u7684\u77e9\u9635\u5728\u6570\u636e\u5206\u6790\u4e2d\u6709\u54ea\u4e9b\u5e94\u7528\uff1f<\/strong><br \/>\u5168\u4e3a1\u7684\u77e9\u9635\u5728\u6570\u636e\u5206\u6790\u4e2d\u6709\u591a\u79cd\u5e94\u7528\uff0c\u5305\u62ec\u7279\u5f81\u77e9\u9635\u7684\u6784\u5efa\u3001\u521d\u59cb\u5316\u6743\u91cd\u3001\u4ee5\u53ca\u5728\u67d0\u4e9b\u7b97\u6cd5\u4e2d\u4f5c\u4e3a\u57fa\u51c6\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u53ef\u4ee5\u7528\u5168\u4e3a1\u7684\u77e9\u9635\u6765\u8868\u793a\u504f\u7f6e\u9879\uff0c\u5e2e\u52a9\u6a21\u578b\u5b66\u4e60\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u4e00\u4e2a\u5143\u7d20\u5168\u4e3a1\u7684\u77e9\u9635\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u5bfc\u5165NumPy\u5e93\u3001\u4f7f\u7528on [&hellip;]","protected":false},"author":3,"featured_media":1122371,"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\/1122367"}],"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=1122367"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122367\/revisions"}],"predecessor-version":[{"id":1122374,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1122367\/revisions\/1122374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1122371"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1122367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1122367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1122367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}