我在Django中进行数据分析,并且我试图创建一个数据库查询,该查询可以迭代模型并打印出一个区间内的行数。
问题是结果的总量有所不同。如果" interval"改变了"总计"也会改变。 为什么这样做?
# Get the data
data = COPD6Model.objects.filter(patient=patients)
# Calculate the min and max value
max_min = data.aggregate(Max("fev1_procent"), Min("fev1_procent"))
max = float(max_min.get("fev1_procent__max"))
min = float(max_min.get("fev1_procent__min")) - float(interval)
# Set the interval
interval = 2.0
total = 0
# Iterate over the query
for start in numpy.arange(min, max, interval):
end = start + interval
result = len(data.filter(fev1_procent__gt=start, fev1_procent__lte=end))
total += result
print total
答案 0 :(得分:1)
浮点精度一定有问题。使用decimal而不是float。