{"id":973053,"date":"2024-12-27T05:53:45","date_gmt":"2024-12-26T21:53:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/973053.html"},"modified":"2024-12-27T05:53:47","modified_gmt":"2024-12-26T21:53:47","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e4%ba%a7%e7%94%9f%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/973053.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u4ea7\u751f\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195422\/cc0cddb2-3043-4f94-9e04-5d9b495532ab.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u4ea7\u751f\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u968f\u673a\u4ea7\u751f\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u968f\u673a\u6a21\u5757\u7b49\u3002NumPy\u5e93\u662f\u6700\u5f3a\u5927\u548c\u65b9\u4fbf\u7684\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u7528\u4e8e\u751f\u6210\u968f\u673a\u77e9\u9635\u3001\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3001\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3002\u5728\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528numpy.random\u6a21\u5757\u4e2d\u7684rand\u3001randn\u3001randint\u7b49\u51fd\u6570\u6839\u636e\u9700\u6c42\u4ea7\u751f\u4e0d\u540c\u7c7b\u578b\u7684\u968f\u673a\u77e9\u9635\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93\u751f\u6210\u968f\u673a\u77e9\u9635<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\u3002\u5bf9\u4e8e\u751f\u6210\u968f\u673a\u77e9\u9635\uff0cNumPy\u63d0\u4f9b\u4e86\u591a\u4e2a\u51fd\u6570\u9009\u9879\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528numpy.random.rand<\/li>\n<\/ol>\n<p><p><code>numpy.random.rand<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u57280\u52301\u4e4b\u95f4\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u77e9\u9635\u3002\u53ef\u4ee5\u6307\u5b9a\u77e9\u9635\u7684\u5f62\u72b6\uff0c\u4f8b\u5982\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.random.rand(3, 3)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u6570\u77e9\u9635\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u662f\u4ece\u5747\u5300\u5206\u5e03\u4e2d\u62bd\u53d6\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528numpy.random.randn<\/li>\n<\/ol>\n<p><p><code>numpy.random.randn<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u6807\u51c6\u6b63\u6001\u5206\u5e03\uff08\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\uff09\u7684\u968f\u673a\u6570\u77e9\u9635\u3002\u5b83\u7684\u7528\u6cd5\u548c<code>rand<\/code>\u7c7b\u4f3c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.random.randn(3, 3)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u5728\u9700\u8981\u6a21\u62df\u81ea\u7136\u73b0\u8c61\u6216\u5176\u4ed6\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u60c5\u51b5\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528numpy.random.randint<\/li>\n<\/ol>\n<p><p><code>numpy.random.randint<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u6574\u6570\u968f\u673a\u6570\u77e9\u9635\uff0c\u5141\u8bb8\u6307\u5b9a\u968f\u673a\u6570\u7684\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.random.randint(low=0, high=10, size=(3, 3))<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u975e\u5e38\u9002\u5408\u7528\u4e8e\u751f\u6210\u79bb\u6563\u7684\u968f\u673a\u6570\u77e9\u9635\uff0c\u4f8b\u5982\u9700\u8981\u5728\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u968f\u673a\u6a21\u5757\u548c\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528NumPy\uff0c\u4e5f\u53ef\u4ee5\u7528Python\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u968f\u673a\u77e9\u9635\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982NumPy\u9ad8\u6548\uff0c\u4f46\u5728\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u4e0a\u540c\u6837\u6709\u6548\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u6d6e\u70b9\u6570\u77e9\u9635<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>rows, cols = 3, 3<\/p>\n<p>matrix = [[random.random() for _ in range(cols)] for _ in range(rows)]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u77e9\u9635\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4ece0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u6574\u6570\u77e9\u9635<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>rows, cols = 3, 3<\/p>\n<p>matrix = [[random.randint(0, 10) for _ in range(cols)] for _ in range(rows)]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u662f0\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5e94\u7528\u573a\u666f\u4e0e\u9009\u62e9<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u751f\u6210\u968f\u673a\u77e9\u9635\u4e3b\u8981\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u89c4\u6a21\u548c\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0cNumPy\u7531\u4e8e\u5176\u5e95\u5c42\u4f18\u5316\u548c\u9ad8\u6548\u7684\u5b9e\u73b0\uff0c\u662f\u751f\u6210\u548c\u64cd\u4f5c\u968f\u673a\u77e9\u9635\u7684\u9996\u9009\u5de5\u5177\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7c7b\u578b\u548c\u5206\u5e03<\/strong>\uff1a\u6839\u636e\u9700\u8981\u7684\u968f\u673a\u6570\u5206\u5e03\uff08\u5747\u5300\u3001\u6b63\u6001\u6216\u79bb\u6563\uff09\u9009\u62e9\u5408\u9002\u7684NumPy\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u5206\u5e03\uff0cNumPy\u63d0\u4f9b\u7684<code>numpy.random<\/code>\u6a21\u5757\u8fd8\u6709\u66f4\u591a\u7684\u9009\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b80\u5355\u6027\u548c\u7075\u6d3b\u6027<\/strong>\uff1a\u5bf9\u4e8e\u8f83\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u6216\u7b80\u5355\u7684\u751f\u6210\u9700\u6c42\uff0c\u4f7f\u7528Python\u5185\u7f6e\u7684<code>random<\/code>\u6a21\u5757\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u662f\u4e00\u79cd\u53ef\u884c\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u8fdb\u9636\u6280\u5de7\u4e0e\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u751f\u6210\u968f\u673a\u77e9\u9635\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4e00\u4e9b\u8fdb\u9636\u6280\u5de7\u548c\u4f18\u5316\u7b56\u7565\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u79cd\u5b50\u8bbe\u7f6e<\/strong>\uff1a\u5728\u9700\u8981\u53ef\u91cd\u590d\u7684\u968f\u673a\u77e9\u9635\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.random.seed(42)<\/code>\u6765\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u65f6\u751f\u6210\u7684\u968f\u673a\u77e9\u9635\u76f8\u540c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7c7b\u578b\u6307\u5b9a<\/strong>\uff1aNumPy\u51fd\u6570\u5141\u8bb8\u6307\u5b9a\u751f\u6210\u7684\u968f\u673a\u77e9\u9635\u7684\u6570\u636e\u7c7b\u578b\uff08dtype\uff09\uff0c\u4f8b\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\uff0c\u8fd9\u5bf9\u4e8e\u5185\u5b58\u4f18\u5316\u548c\u8ba1\u7b97\u7cbe\u5ea6\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6279\u91cf\u751f\u6210<\/strong>\uff1a\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528NumPy\u7684\u6279\u91cf\u751f\u6210\u7279\u6027\uff0c\u4e00\u6b21\u751f\u6210\u591a\u4e2a\u968f\u673a\u77e9\u9635\uff0c\u800c\u4e0d\u662f\u5728\u5faa\u73af\u4e2d\u9010\u4e2a\u751f\u6210\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u77e9\u9635\u64cd\u4f5c\u548c\u8fd0\u7b97<\/strong>\uff1aNumPy\u4e0d\u4ec5\u53ef\u4ee5\u751f\u6210\u968f\u673a\u77e9\u9635\uff0c\u8fd8\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\uff0c\u5982\u52a0\u51cf\u4e58\u9664\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\uff0c\u8fd9\u5bf9\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5efa\u6a21\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9ad8\u6548\u5730\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u77e9\u9635\uff0c\u4e3a\u6570\u636e\u5206\u6790\u3001\u79d1\u5b66\u8ba1\u7b97\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u4efb\u52a1\u5960\u5b9a\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u6307\u5b9a\u5927\u5c0f\u7684\u968f\u673a\u77e9\u9635\uff1f<\/strong><br \/>\u8981\u751f\u6210\u6307\u5b9a\u5927\u5c0f\u7684\u968f\u673a\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.rand()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u7ed9\u5b9a\u5f62\u72b6\u7684\u77e9\u9635\u3002\u4f8b\u5982\uff0c<code>numpy.random.rand(3, 4)<\/code>\u5c06\u751f\u6210\u4e00\u4e2a3\u884c4\u5217\u7684\u968f\u673a\u77e9\u9635\uff0c\u77e9\u9635\u4e2d\u7684\u503c\u57280\u52301\u4e4b\u95f4\u3002<\/p>\n<p><strong>Python\u4e2d\u751f\u6210\u6574\u6570\u968f\u673a\u77e9\u9635\u7684\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u968f\u673a\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.randint()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u751f\u6210\u7684\u968f\u673a\u6574\u6570\u7684\u8303\u56f4\u53ca\u77e9\u9635\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c<code>numpy.random.randint(1, 10, (3, 4))<\/code>\u5c06\u751f\u6210\u4e00\u4e2a3\u884c4\u5217\u3001\u503c\u57281\u52309\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u77e9\u9635\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u77e9\u9635\u5177\u6709\u7279\u5b9a\u7684\u5206\u5e03\u7279\u5f81\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.normal()<\/code>\u6765\u751f\u6210\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u77e9\u9635\u3002\u8be5\u51fd\u6570\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u5747\u503c\u548c\u6807\u51c6\u5dee\u3002\u4f8b\u5982\uff0c<code>numpy.random.normal(loc=0, scale=1, size=(3, 4))<\/code>\u5c06\u751f\u6210\u4e00\u4e2a3\u884c4\u5217\u7684\u968f\u673a\u77e9\u9635\uff0c\u6570\u636e\u7b26\u5408\u5747\u503c\u4e3a0\u3001\u6807\u51c6\u5dee\u4e3a1\u7684\u6b63\u6001\u5206\u5e03\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u7edf\u8ba1\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u4e2d\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u968f\u673a\u4ea7\u751f\u77e9\u9635\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u968f\u673a\u6a21\u5757\u7b49\u3002N [&hellip;]","protected":false},"author":3,"featured_media":973060,"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\/973053"}],"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=973053"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973053\/revisions"}],"predecessor-version":[{"id":973061,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973053\/revisions\/973061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/973060"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=973053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=973053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=973053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}