我需要从知道样本大小的相对频率重建绝对频率。
这应该很容易,但绝对频率和样本量numpy.int64
,相对频率numpy.float64
。
我知道浮点十进制值通常没有精确的二进制表示形式,我们可能会遇到一些精度损失。情况似乎是这样,浮点运算产生了意想不到的结果,我不能相信重建的绝对频率。
复制错误的示例代码:
import pandas as pd
import numpy as np
absolutes = np.arange(100000, dtype=np.int64) #numpy.int64
sample_size = absolutes.sum() # numpy.int64
relatives = absolutes / sample_size #float64
# Rebuilding absolutes from relatives
rebuilt_float = relatives * sample_size #float64
rebuilt_int = rebuilt_float.astype(np.int64)
df = pd.DataFrame({'absolutes': absolutes,
'relatives': relatives,
'rebuilt_float': rebuilt_float,
'rebuilt_int': rebuilt_int})
df['check_float'] = df['absolutes'] == df['rebuilt_float']
df['check_int'] = df['absolutes'] == df['rebuilt_int']
print('Failed FLOATS: ', len(df[df['check_float'] == False]))
print('Failed INTS:', len(df[df['check_int'] == False]))
print('Sum of FLOATS:', df['rebuilt_float'].sum())
print('Sum of INTS:', df['rebuilt_int'].sum())
是否可以使用 numpy 解决问题,而无需将每个数字转换为小数?
np.isclose(df['absolutes'], df['rebuilt_float'], atol=.99999)
numpy.isclose()
是一个不精确的FP感知比较。它具有额外的参数atol
和相对和绝对公差rtol
。
您可以通过更改atol
来查看删除了多少舍入错误:
>>> len(np.where( np.isclose(df['absolutes'], df['rebuilt_int'], atol=.99999) == False )[0])
0
>>> len(np.where( np.isclose(df['absolutes'], df['rebuilt_int'], atol=.5) == False )[0])
2767
>>> len(np.where( np.isclose(df['absolutes'], df['rebuilt_int'], atol=1) == False )[0])
0
如果在转换为整数之前对重建的值进行舍入,则会得到零个失败的整数。 也就是说,使用
rebuilt_int = np.round(rebuilt_float).astype(np.int64)
然后输出
Failed FLOATS: 11062
Failed INTS: 0
Sum of FLOATS: 4999950000.0
Sum of INTS: 4999950000