Я не могу понять причину этой ошибки. Я пытаюсь воссоздать столбец, вычисляя его, используя конкретную формулу, но я вижу проблемы. Колонка «C» была ранее рассчитана, возможно, с использованием формулы «A» - 'B'. Этот столбец уже существует в наборе данных. Теперь я пытаюсь пересчитать колонку «C» с той же формулой, но мои новые результаты немного отличаются от исходных значений. Например: < /p>
C_new C
0 0.559999 0.559999
1 0.859998 0.859998
2 0.569999 0.569999
3 -0.020000 -0.020000
4 0.979997 0.979998
... ... ...
948152 2.279995 2.279995
948153 2.269995 2.269995
948154 2.099995 2.099995
948155 2.299995 2.299995
948156 1.769996 1.769996
< /code>
Уведомление, например, строка в индексе 4: Есть крошечное расхождение. Следующий интересный шаблон: < /p>
C_new = df1['A'] - df1['B']
diffs = df1['C'] - C_new
< /code>
0 -0.000000000000001221
1 0.000000000000000888
2 -0.000000000000000999
3 -0.000000000000000427
4 0.000000999999999141
...
948152 -0.000000000000001332
948153 -0.000000000000001332
948154 0.000000000000000000
948155 0.000000000000000888
948156 0.000000000000000888
Length: 948157, dtype: float64
< /code>
Digging deeper, I counted how many rows exhibit differences around the magnitude of 0.000001. When I set a tolerance of 1e-7 and searched for differences close to 1e-6:
tolerance = 1e-7
print(diffs[np.abs(diffs - 1e-6) < tolerance])
4 0.000000999999999141
21 0.000001000000000168
25 0.000001000000000251
26 0.000000999999999696
27 0.000001000000000473
...
948083 0.000001000000001472
948087 0.000001000000001472
948093 0.000000999999998363
948123 0.000000999999999918
948126 0.000000999999999918
Length: 114311, dtype: float64
< /code>
I would have expected the error distribution to range more evenly, perhaps from 0.000001 to 0.000009, instead of clustering around exactly 0.000001 or -0.000001.
I realize there are many unknowns, such as how exactly the original 'C' column was computed or the numerical precision originally used in columns 'A' and 'B'. Could anyone explain why I'm observing this discrepancy? Is it purely a result of floating-point rounding errors? Moreover, why does this peculiar error distribution occur, clustering closely on -0.000001 and 0.000001, rather than being evenly spread out?
*Edit
I tried to reproduce calculating first in float32 then saving to csv using pandas and then load the csv and calculating C again but as float64.
import numpy as np
import pandas as pd
# Original float64 calculation
A32 = np.array([22.5443], dtype=np.float32)
B32 = np.array([30.12434630], dtype=np.float32)
C_exact = (A32- B32)
df = pd.DataFrame({
'C_exact': C_exact,
'A': A32,
'B': B32
})
df.to_csv('test.csv')
df_load = pd.read_csv('test.csv')
# Recalculate directly in float64 again
C_recomputed = (df_load['A'] - df_load['B'])
difference = df_load['C_exact'].values - C_recomputed
print(f"Exact : {df_load['C_exact'].values[0]:.18f}")
print(f"Recomputed: {C_recomputed[0]:.18f}")
print(f"Difference: {difference[0]:.18f}")
< /code>
and I get
Exact : -7.580045700000000330
Recomputed: -7.580045999999999395
Difference: 0.000000299999999065
< /code>
It's not 0.000001 like before.
Подробнее здесь: https://stackoverflow.com/questions/795 ... lated-ones
Почему вычисленные значения не могут точно соответствовать предварительно рассчитанным? [дублировать] ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение