2

好的,

我遇到了一个舍入错误问题。我有一个 java 程序,它逐行读取一些数字列表。该程序要求此列表为浮点数(什么类型的浮点数?我怀疑是单精度)数字,其总和 S 在 0.999 和 1.001 之间(即 0.999 <= S <= 1.001)。这是一个例子:

from numpy import array, linalg

def Normalize(X):
    NumpyX = array(X)
    Norm = linalg.norm(NumpyX)
    Normalize = NumpyX/Norm
    return Normalize

def FileMaker(FilePointer,Vector)
    for i in Vector:
        FilePointer.write('%f\n'%i)
    return


#sum(SubstitutionPoints) does not add to 1
SubstitutionPoints =[0.00606508512067950,0.00675296642376962,0.00688999694872917,0.00580692396866418,0.00680583604896024,0.00609061670962565,0.00585881991631447,0.00577148570812953,0.00600882981888663,0.00618499536435559,0.00650767341787896,0.00670521809234427,0.00699374780209504,0.00841141135948587,0.00830145870238677,0.00879477131238090,0.00918627324146331,0.00958946761973615,0.01032404247887830,0.01093417870737930,0.01188202458790520,0.01261860720648550,0.01355451051017660,0.01473818756656830,0.01623978223562570,0.01811682034513980,0.01990010225231130,0.02154250858435480,0.02418185925226890,0.02583490296173980,0.02844212438633430,0.03194935989118780,0.03534228607419560,0.03971366519834600,0.04548573525944540,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660,0.05190994307855660]
NormedSP = Normalize(SubstitutionPoints)
SubFile = open('Sub.txt','w')
FileMaker(SubFile,NormedSP)
SubFile.close()

由于python不使用单精度浮点数,我担心创建的文件会导致我的java程序出现问题,因为我的java程序(metasim)返回错误:

Simulating files:
[BorreliaBurgdorferiB31_CP32-3.fasta]
java.lang.Exception: Substitution rates don't sum to 1.0.
!!! FAILED !!!

不幸的是,我无法调试 java 程序,因为它是“java 可执行文件”。我唯一的选择是提供正确的输入。

任何建议/帮助表示赞赏。

编辑

从尝试 float32() 的一些建议中,我修改了我的规范化方法:

def Normalize(self,X):
    Total = sum(X)
    NumpyX = array([float32(i) for i in X])
    Norm = linalg.norm(NumpyX,ord=1)
    Normalize = NumpyX/Norm
    return Normalize

我开始怀疑这是单精度浮点问题。

我的输出是否被截断并导致错误?

4

3 回答 3

2

如果您只需要精确到小数点后三位,为什么不将这些值乘以 1000,用整数进行所有数学运算,然后只在输出中添加小数点?这样,应该很容易确保总和是准确的。

于 2012-07-17T03:07:51.293 回答
1

numpy.linalg.norm需要ord=1关键字 arg 来执行您期望的操作。使用您当前的数据和定义...

>>> Normalize(substitution_points).sum()
9.0451896403987444

您可以改为这样做 - 这也可以正确缩放负值(对值linalg.norm求和abs):

>>> def normalize(x):
...     a = numpy.array(x)
...     return a / a.sum()
... 
>>> normalize(substitution_points).sum()
0.99999999999999789

0.9999999... > 0.999,因此根据您提供的规范,它应该可以工作。

但是......它似乎仍然无法正常工作。尝试使用numpy.float16-- 并注意创建浮点数组的更惯用的方法是这样的:

array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)
于 2012-07-17T02:26:29.023 回答
0

您的 Normalize 方法不会强制列表的总和为 1:除以范数会将数设置为 1。要将总数设置为 1,您需要除以当前总数:

def normalize(X):
   total = sum(X)
   return [x/total for x in X]
于 2012-07-17T03:36:18.397 回答