【问题标题】:python numpy log2 returns -inf when using Popen [closed]python numpy log2在使用Popen时返回-inf [关闭]
【发布时间】:2026-01-28 21:50:02
【问题描述】:

我正在尝试运行这一行:

print(np.log2( (ngram_list.count(('i',)) + 1 )/( 100000 + len(set(n1gram_list)) )))

当我从终端运行我的脚本时,我得到 -1.54814270552。 但是当我通过 subprocess.Popen 运行相同的脚本时,我得到了 -inf。 基本上每次 log2 的输出为正时,我都会在两种方法中得到正确的输出,但对于负值,我只有在使用 Popen 时才会得到 -inf。

【问题讨论】:

标签: python numpy subprocess


【解决方案1】:

您在使用Popen 时似乎使用的是Python 2,它使用整数除法。

要么使用from __future__ import division,将100000 + len(set(n1gram_list)) 转换为float,要么调用Python 3。

【讨论】:

  • 在 Popen 中,我运行命令“python lm.py”。当我尝试运行“python3 lm.py”时,什么也没有发生。如何使用 python 3 运行此命令?