2

わかった、

丸め誤差の問題に遭遇しました。リストを行ごとにいくつかの数字で読み取るJavaプログラムがあります。プログラムでは、このリストが浮動小数点数 (どのタイプの浮動小数点数ですか? 単精度だと思います) で、合計 S が .999 か​​ら 1.001 の間 (つまり、.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 プログラム (metasim) がエラーを返すため、作成されたファイルが Java プログラムで問題を引き起こしているのではないかと心配しています。

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

小数点以下 3 桁まで正確にする必要がある場合は、値に 1000 を掛けて、すべての計算を整数で行い、小数点のみを出力に追加してみませんか? そうすれば、合計が正確であることを簡単に確認できます。

于 2012-07-17T03:07:51.293 に答える
1

numpy.linalg.normord=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、あなたが提供した仕様によると、それはうまくいくはずです。

でも・・・まだダメみたいです。-- で遊んでみてください。floatnumpy.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 に答える