python数值太小都为0怎么办,Python Numpy-将非常小的数字视为零

I want to calculate the Determinant of a Singular Matrix (which has a 0 determinant) with Numpy and when I print the determinant it shows a really small number (which is nearly zero = -7.09974814699e-30) but not zero itself...

when I try to print the determinant either with %s, %d or %f, sometimes it's zero, sometimes -0 and sometimes -7.09974814699e-30 .

Here's the Code :

import numpy as np

array = np.arange(16)

array = array.reshape(4, -1)

determinant = np.linalg.det(array)

print("Determinant is %s" % determinant)

print("Determinant is %d" % determinant)

print("Determinant is %f" % determinant)

Determinant is -7.09974814699e-30

Determinant is 0

Determinant is -0.000000

How can I make Numpy treat really small numbers such as -7.09974814699e-30 as zero and show zero to me. I also asked this question before, if you take a look at the matrix you see that it's filled with really small numbers but not zero while it should be a diagonal matrix with numbers on the diagonal and zeros elsewhere...

Thank you...

解决方案>>> if np.abs(determinant) < 0.000001:

... determinant=0

...

>>> print determinant

0

In case of array you can do it using a single operation (see my answer to your other question: https://stackoverflow.com/a/36395905/5088142)

To set array elements that are less than eps to zero:

array[np.abs(array) < eps] = 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值