小编jer*_*rry的帖子

如何在Python中将复数四舍五入到小数点后两位?

我想将复数部分和实部四舍五入到小数点后两位。我怎样才能在Python中做到这一点?

angle = 2 * np.pi *2 
c1 = complex(np.cos(angle),np.sin(angle))
c1 = round(c1,2)
Run Code Online (Sandbox Code Playgroud)

python numpy rounding complex-numbers

5
推荐指数
1
解决办法
3545
查看次数

为什么 KL 散度给出 nan?是数学错误还是我的输入数据不正确?

在以下代码中 s 返回 nan。由于 Q<1 中的每个值,因此当我取其对数时,它会返回负值。这是否意味着我无法使用 P 和 Q 的这些值计算 KL 散度,或者我可以修复它吗?

`P= np.array([1.125,3.314,2.7414]) 
Q=np.array([0.42369288, 0.89152044, 0.60905852])

for i in range(len(P)):
    if P[i] != 0 and Q[i]!=0:
        s= P[i] *np.log(P[i]/Q[i])
        print("s: ",s)` 
Run Code Online (Sandbox Code Playgroud)

numpy python-3.x

1
推荐指数
1
解决办法
4303
查看次数

标签 统计

numpy ×2

complex-numbers ×1

python ×1

python-3.x ×1

rounding ×1