{"id":1152214,"date":"2025-01-13T17:23:03","date_gmt":"2025-01-13T09:23:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152214.html"},"modified":"2025-01-13T17:23:05","modified_gmt":"2025-01-13T09:23:05","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e8%b4%9f%e6%97%a0%e7%a9%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152214.html","title":{"rendered":"python\u5982\u4f55\u8868\u793a\u8d1f\u65e0\u7a77"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182256\/9ae2bc50-287b-459f-ada0-e0fdf320fed6.webp\" alt=\"python\u5982\u4f55\u8868\u793a\u8d1f\u65e0\u7a77\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>float(&#39;-inf&#39;)<\/code>\u3001<code>math.inf<\/code>\u3001<code>numpy.inf<\/code>\u6765\u8868\u793a\u8d1f\u65e0\u7a77\u3002<\/strong> \u5176\u4e2d\uff0c<code>float(&#39;-inf&#39;)<\/code>\u662f\u76f4\u63a5\u4f7f\u7528Python\u5185\u7f6e\u7684\u6d6e\u70b9\u6570\u7c7b\u578b\uff0c<code>math.inf<\/code>\u548c<code>numpy.inf<\/code>\u5219\u9700\u8981\u5206\u522b\u5bfc\u5165<code>math<\/code>\u548c<code>numpy<\/code>\u5e93\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8868\u793a\u8d1f\u65e0\u7a77\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>float(&#39;-inf&#39;)<\/code><\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u6d6e\u70b9\u6570\u7c7b\u578b<code>float<\/code>\u53ef\u4ee5\u76f4\u63a5\u8868\u793a\u8d1f\u65e0\u7a77\u3002\u4f7f\u7528<code>float(&#39;-inf&#39;)<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8d1f\u65e0\u7a77\u7684\u6d6e\u70b9\u6570\u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u65e0\u9700\u5bfc\u5165\u4efb\u4f55\u989d\u5916\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">negative_infinity = float(&#39;-inf&#39;)<\/p>\n<p>print(negative_infinity)  # \u8f93\u51fa\uff1a-inf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>float(&#39;-inf&#39;)<\/code>\u8868\u793a\u8d1f\u65e0\u7a77\u7684\u597d\u5904\u662f\u7b80\u6d01\u660e\u4e86\uff0c\u4e0d\u9700\u8981\u4f9d\u8d56\u4efb\u4f55\u5916\u90e8\u5e93\u3002\u5728\u5904\u7406\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\u6216\u6bd4\u8f83\u64cd\u4f5c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>math.inf<\/code><\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u4e2d\u7684<code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>math.inf<\/code>\u6765\u8868\u793a\u65e0\u7a77\u5927\u548c\u65e0\u7a77\u5c0f\u3002\u4f7f\u7528<code>math.inf<\/code>\u53ef\u4ee5\u8868\u793a\u6b63\u65e0\u7a77\uff0c\u800c\u4f7f\u7528<code>-math.inf<\/code>\u5219\u53ef\u4ee5\u8868\u793a\u8d1f\u65e0\u7a77\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>negative_infinity = -math.inf<\/p>\n<p>print(negative_infinity)  # \u8f93\u51fa\uff1a-inf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>math.inf<\/code>\u6765\u8868\u793a\u8d1f\u65e0\u7a77\u7684\u597d\u5904\u662f\u4ee3\u7801\u66f4\u52a0\u89c4\u8303\u548c\u6613\u8bfb\u3002<code>math<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u901a\u5e38\u4f1a\u5728\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u65f6\u5bfc\u5165\uff0c\u56e0\u6b64\u4f7f\u7528<code>math.inf<\/code>\u4e0d\u4f1a\u589e\u52a0\u989d\u5916\u7684\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>numpy.inf<\/code><\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u652f\u6301\u3002<code>numpy<\/code>\u5e93\u4e2d\u7684<code>numpy.inf<\/code>\u4e5f\u53ef\u4ee5\u7528\u6765\u8868\u793a\u6b63\u65e0\u7a77\u548c\u8d1f\u65e0\u7a77\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>negative_infinity = -np.inf<\/p>\n<p>print(negative_infinity)  # \u8f93\u51fa\uff1a-inf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy.inf<\/code>\u6765\u8868\u793a\u8d1f\u65e0\u7a77\u7684\u597d\u5904\u662f\u5982\u679c\u4f60\u7684\u9879\u76ee\u5df2\u7ecf\u5728\u4f7f\u7528<code>numpy<\/code>\u8fdb\u884c\u6570\u7ec4\u6216\u77e9\u9635\u8fd0\u7b97\uff0c\u90a3\u4e48\u4f7f\u7528<code>numpy.inf<\/code>\u4f1a\u66f4\u4e3a\u4e00\u81f4\u548c\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8d1f\u65e0\u7a77\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u6570\u5b66\u8fd0\u7b97\u548c\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u5b66\u8fd0\u7b97\u548c\u6bd4\u8f83\u4e2d\uff0c\u8d1f\u65e0\u7a77\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6570\u503c\u3002\u6bd4\u5982\u5728\u8ba1\u7b97\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u65f6\uff0c\u53ef\u4ee5\u521d\u59cb\u5316\u6700\u5c0f\u503c\u4e3a\u8d1f\u65e0\u7a77\uff0c\u786e\u4fdd\u4efb\u4f55\u5b9e\u9645\u6570\u503c\u90fd\u80fd\u66f4\u65b0\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [3, 5, 1, 7, 2]<\/p>\n<p>min_value = float(&#39;-inf&#39;)<\/p>\n<p>for number in numbers:<\/p>\n<p>    if number &gt; min_value:<\/p>\n<p>        min_value = number<\/p>\n<p>print(min_value)  # \u8f93\u51fa\uff1a7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c\u8d1f\u65e0\u7a77\u53ef\u4ee5\u7528\u4e8e\u6807\u8bc6\u7f3a\u5931\u503c\u6216\u4e0d\u53ef\u7528\u7684\u6570\u503c\u3002\u6bd4\u5982\u5728\u6570\u636e\u6e05\u6d17\u65f6\uff0c\u53ef\u4ee5\u5c06\u65e0\u6548\u6570\u636e\u66ff\u6362\u4e3a\u8d1f\u65e0\u7a77\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, None, 4, 5]<\/p>\n<p>cleaned_data = [x if x is not None else float(&#39;-inf&#39;) for x in data]<\/p>\n<p>print(cleaned_data)  # \u8f93\u51fa\uff1a[1, 2, -inf, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u503c\u4f18\u5316\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h4>\n<\/p>\n<p><p>\u5728\u6570\u503c\u4f18\u5316\u548c\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u8d1f\u65e0\u7a77\u53ef\u4ee5\u7528\u4e8e\u521d\u59cb\u5316\u6781\u503c\uff0c\u6216\u8005\u8868\u793a\u4e0d\u53ef\u80fd\u7684\u6982\u7387\u548c\u635f\u5931\u503c\u3002\u5728\u8bad\u7ec3\u6a21\u578b\u65f6\uff0c\u8d1f\u65e0\u7a77\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u521d\u59cb\u7684\u6781\u7aef\u503c\uff0c\u5e2e\u52a9\u6a21\u578b\u9010\u6e10\u6536\u655b\u5230\u5408\u7406\u7684\u8303\u56f4\u5185\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">best_loss = float(&#39;-inf&#39;)<\/p>\n<p>for epoch in range(100):<\/p>\n<p>    loss = tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_model(epoch)<\/p>\n<p>    if loss &gt; best_loss:<\/p>\n<p>        best_loss = loss<\/p>\n<p>print(best_loss)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u8d1f\u65e0\u7a77\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7c7b\u578b\u4e00\u81f4\u6027<\/strong>\uff1a\u786e\u4fdd\u5728\u6574\u4e2a\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e00\u81f4\u7684\u65b9\u6cd5\u6765\u8868\u793a\u8d1f\u65e0\u7a77\uff0c\u907f\u514d\u6df7\u6dc6\u3002<\/li>\n<li><strong>\u5e93\u4f9d\u8d56<\/strong>\uff1a\u5982\u679c\u4f7f\u7528<code>math<\/code>\u6216<code>numpy<\/code>\uff0c\u9700\u8981\u786e\u4fdd\u76f8\u5173\u5e93\u5df2\u7ecf\u5b89\u88c5\u5e76\u6b63\u786e\u5bfc\u5165\u3002<\/li>\n<li><strong>\u8fd0\u7b97\u7a33\u5b9a\u6027<\/strong>\uff1a\u5728\u67d0\u4e9b\u6570\u5b66\u8fd0\u7b97\u4e2d\uff0c\u8d1f\u65e0\u7a77\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6ea2\u51fa\u6216\u4e0d\u7a33\u5b9a\u7684\u7ed3\u679c\uff0c\u9700\u8981\u8c28\u614e\u5904\u7406\u3002<\/li>\n<\/ol>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8868\u793a\u8d1f\u65e0\u7a77\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<code>float(&#39;-inf&#39;)<\/code>\u3001<code>math.inf<\/code>\u548c<code>numpy.inf<\/code>\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9879\u76ee\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u54ea\u79cd\u65b9\u6cd5\uff0c<strong>\u8d1f\u65e0\u7a77\u5728\u6570\u5b66\u8fd0\u7b97\u3001\u6570\u636e\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528<\/strong>\uff0c\u53ef\u4ee5\u5e2e\u52a9\u89e3\u51b3\u6781\u503c\u521d\u59cb\u5316\u3001\u7f3a\u5931\u503c\u6807\u8bc6\u7b49\u95ee\u9898\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u8d1f\u65e0\u7a77\u53ef\u4ee5\u63d0\u5347\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8868\u793a\u8d1f\u65e0\u7a77\u5927\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8d1f\u65e0\u7a77\u5927\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>float<\/code>\u7c7b\u578b\u7684\u8d1f\u65e0\u7a77\u8868\u793a\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>float(&#39;-inf&#39;)<\/code>\u6765\u521b\u5efa\u8d1f\u65e0\u7a77\u7684\u503c\u3002\u8fd9\u79cd\u8868\u793a\u6cd5\u5728\u6570\u5b66\u8fd0\u7b97\u548c\u6bd4\u8f83\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5904\u7406\u6781\u7aef\u503c\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<p><strong>\u8d1f\u65e0\u7a77\u5728Python\u4e2d\u6709\u4ec0\u4e48\u5b9e\u9645\u5e94\u7528\uff1f<\/strong><br \/>\u8d1f\u65e0\u7a77\u5e38\u7528\u4e8e\u7b97\u6cd5\u548c\u6570\u636e\u5904\u7406\u4e2d\u7684\u8fb9\u754c\u6761\u4ef6\uff0c\u4f8b\u5982\u6700\u5c0f\u503c\u7684\u521d\u59cb\u5316\u3002\u5728\u5bfb\u627e\u6700\u5c0f\u503c\u6216\u8fdb\u884c\u52a8\u6001\u89c4\u5212\u65f6\uff0c\u60a8\u53ef\u4ee5\u5c06\u521d\u59cb\u503c\u8bbe\u4e3a\u8d1f\u65e0\u7a77\uff0c\u4ee5\u786e\u4fdd\u4efb\u4f55\u5b9e\u9645\u503c\u90fd\u80fd\u66ff\u4ee3\u5b83\u3002\u6b64\u5916\uff0c\u8d1f\u65e0\u7a77\u4e5f\u5e38\u7528\u4e8e\u8868\u793a\u4e0d\u53ef\u80fd\u53d1\u751f\u7684\u4e8b\u4ef6\uff0c\u4f8b\u5982\u5728\u56fe\u8bba\u4e2d\u7684\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\u4e2d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u4e3a\u8d1f\u65e0\u7a77\u5927\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u4e3a\u8d1f\u65e0\u7a77\u5927\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>isinf()<\/code>\u51fd\u6570\u3002\u7ed3\u5408\u8d1f\u53f7\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<code>import math; math.isinf(x) and x &lt; 0<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u60a8\u6709\u6548\u5730\u8bc6\u522b\u51fa\u8d1f\u65e0\u7a77\u5927\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7cbe\u786e\u5224\u65ad\u6781\u7aef\u503c\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528float(&#39;-inf&#39;)\u3001math.inf\u3001numpy.inf [&hellip;]","protected":false},"author":3,"featured_media":1152220,"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\/1152214"}],"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=1152214"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152214\/revisions"}],"predecessor-version":[{"id":1152221,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152214\/revisions\/1152221"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152220"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}