我目前正在研究解决以下问题
问题:
根据时间戳,尽可能按比例确定其位置值。
当前公式:
position= 最小位置+(最大位置−最小位置)× (最大时间戳−最小时间戳)/ (timestamp−min_timestamp)
我使用的数据集:
时间戳差异 = 当前时间戳 - 上一个时间戳
dataset = [
{"timestamp": 1718778085122, "position": None, "Timestamp diff": 0},
{"timestamp": 1718778085145, "position": None, "Timestamp diff": 23},
{"timestamp": 1718778085169, "position": None, "Timestamp diff": 24},
{"timestamp": 1718778104300, "position": None, "Timestamp diff": 19131},
{"timestamp": 1718778104327, "position": None, "Timestamp diff": 27},
{"timestamp": 1718778104356, "position": None, "Timestamp diff": 29},
{"timestamp": 1718778104357, "position": None, "Timestamp diff": 1},
{"timestamp": 1718778123487, "position": None, "Timestamp diff": 19130},
{"timestamp": 1718778123557, "position": None, "Timestamp diff": 70},
{"timestamp": 1718778123572, "position": None, "Timestamp diff": 15},
{"timestamp": 1718778123575, "position": None, "Timestamp diff": 3},
{"timestamp": 1718778142511, "position": None, "Timestamp diff": 18936},
{"timestamp": 1718778142562, "position": None, "Timestamp diff": 51},
{"timestamp": 1718778142563, "position": None, "Timestamp diff": 1},
{"timestamp": 1718778161664, "position": None, "Timestamp diff": 19101},
{"timestamp": 1718778161761, "position": None, "Timestamp diff": 97},
{"timestamp": 1718778161762, "position": None, "Timestamp diff": 1}
]
公式的位置结果:
https://www.programiz.com /online-compiler/18tOf6K7QDmht
位置差异 = 当前位置 - 先前位置
Final positions:
Timestamp: 1718778085122, Position: 0.00
Timestamp: 1718778085145, Position: 0.03 ---> Position diff: 0.03)
Timestamp: 1718778085169, Position: 0.06 ---> Position diff: 0.03)
Timestamp: 1718778104300, Position: 25.02 ---> Position diff: 24.96)
Timestamp: 1718778104327, Position: 25.06 ---> Position diff: 0.04)
Timestamp: 1718778104356, Position: 25.10 ---> Position diff: 0.04)
Timestamp: 1718778104357, Position: 25.10 ---> Position diff: 0.00)
Timestamp: 1718778123487, Position: 50.06 ---> Position diff: 24.96)
Timestamp: 1718778123557, Position: 50.15 ---> Position diff: 0.09)
Timestamp: 1718778123572, Position: 50.17 ---> Position diff: 0.02)
Timestamp: 1718778123575, Position: 50.17 ---> Position diff: 0.00)
Timestamp: 1718778142511, Position: 74.88 ---> Position diff: 24.71)
Timestamp: 1718778142562, Position: 74.95 ---> Position diff: 0.07)
Timestamp: 1718778142563, Position: 74.95 ---> Position diff: 0.00)
Timestamp: 1718778161664, Position: 99.87 ---> Position diff: 24.92)
Timestamp: 1718778161761, Position: 100.00 ---> Position diff: 0.13)
Timestamp: 1718778161762, Position: 100.00 ---> Position diff: 0.00)
也就是说,在问题陈述中,我提到其位置“尽可能成比例”,原因是 我希望有那些小于 1 的位置差异值至少为 1。
(另外,为了进一步说明,我希望那些小于 1 的值至少为 1,这样我至少可以有一个当我绘制矩形时,它们是 1 px 值;否则,它们无处可见)
我想要实现的一些标准:
- 最小的时间戳值始终具有 0 的位置值。最大时间戳值的位置值始终为 100。
- 最后一个数据点之前的所有值的位置值都小于最后一个值。
- 所有值也必须是唯一的,并且尽可能与时间戳成比例。
- 你们能否就如何实现所有这些条件提供一些帮助或反馈?
我想要的结果是这样的:
My desired result is something like this:
--these number are roughly made up just to show my desired data
Final positions:
Timestamp: 1718778085122, Position: 0.00, Timestamp diff: 0
Timestamp: 1718778085145, Position: 2.08, Timestamp diff: 23
Timestamp: 1718778085169, Position: 3.10, Timestamp diff: 24
Timestamp: 1718778104300, Position: 20.36, Timestamp diff: 19131
Timestamp: 1718778104327, Position: 22.63, Timestamp diff: 27
Timestamp: 1718778104356, Position: 25.00, Timestamp diff: 29
Timestamp: 1718778104357, Position: 26.00, Timestamp diff: 1
Timestamp: 1718778123487, Position: 45.30, Timestamp diff: 19130
Timestamp: 1718778123557, Position: 49.66, Timestamp diff: 70
Timestamp: 1718778123572, Position: 50.38, Timestamp diff: 15
Timestamp: 1718778123575, Position: 51.49, Timestamp diff: 3
Timestamp: 1718778142511, Position: 69.67, Timestamp diff: 18936
Timestamp: 1718778142562, Position: 73.10, Timestamp diff: 51
Timestamp: 1718778142563, Position: 74.10, Timestamp diff: 1
Timestamp: 1718778161664, Position: 93.37, Timestamp diff: 19101
Timestamp: 1718778161761, Position: 99.00, Timestamp diff: 97
Timestamp: 1718778161762, Position: 100.00, Timestamp diff: 1
我可以帮助你解决这个问题。你的目标是根据时间戳计算位置值,确保这些值与时间差成比例,并且位置差至少为 1。以下是一种可以实现此目的的方法:
1. 归一化时间戳差异:
- 计算时间戳差异的最大值。
- 将每个时间戳差异除以最大值,以获得 0 到 1 之间的归一化时间戳差异。
2. 按比例调整位置差异:
- 确定所需位置值的总数(例如,100)。
- 将每个归一化时间戳差异乘以位置值的总数,得到按比例调整的位置差异。
3. 累积位置差异并应用下限:
- 从 0 开始初始化第一个位置。
-
对于每个按比例调整的位置差异:
- 如果差异小于 1,则将其设置为 1。
- 将差异添加到先前的位置,以获得新的位置。
Python 代码:
dataset = [
{"timestamp": 1718778085122, "position": None, "Timestamp diff": 0},
{"timestamp": 1718778085145, "position": None, "Timestamp diff": 23},
{"timestamp": 1718778085169, "position": None, "Timestamp diff": 24},
# ... 其余数据集 ...
]
# 计算最大时间戳差异
max_time_diff = max(data["Timestamp diff"] for data in dataset)
# 计算位置
total_positions = 100
current_position = 0
for data in dataset:
# 归一化时间戳差异
normalized_diff = data["Timestamp diff"] / max_time_diff
# 按比例调整位置差异
position_diff = normalized_diff * total_positions
# 应用下限并更新位置
current_position += max(1, position_diff)
data["position"] = current_position
# 打印结果
for data in dataset:
print(f"Timestamp: {data['timestamp']}, Position: {data['position']:.2f}, Timestamp diff: {data['Timestamp diff']}")
这种方法可以确保:
- 最小时间戳的位置为 0(初始位置)。
- 最大时间戳的位置为 100(总位置)。
- 位置值按时间戳差异成比例分布。
- 位置差异至少为 1,从而在视觉表示(如矩形)中可见。
请记住,所得的位置值将大于 100。如果需要将它们保持在 0 到 100 范围内,可以在计算后对它们进行归一化和缩放。
标签:python,algorithm,math From: 78804241