例如,我假设您正在测量温度、风和降水。我们将这些项目称为“功能”。所以有效值可能是:
- 温度:-50 到 100F(我在美国明尼苏达州)
- 风速:0 到 120 英里/小时(不确定这是否现实,但请耐心等待)
- 沉淀:0 到 100
首先标准化您的数据。Temp 的范围为 150 个单位,Wind 为 120 个单位,Precip 为 100 个单位。将你的风单位乘以 1.25 和 Precip 乘以 1.5,使它们与你的温度大致相同的“比例”。您可以在这里花哨并制定规则,使一项功能比其他功能更有价值。在此示例中,风的范围可能很大,但通常保持在较小的范围内,因此您希望减少它的权重以防止它扭曲您的结果。
现在,将每个测量值想象成多维空间中的一个点。此示例测量 3d 空间(温度、风、降水)。好消息是,如果我们添加更多特征,我们只是增加了空间的维度,但数学保持不变。无论如何,我们想找到最接近我们当前点的历史点。最简单的方法是欧几里得距离。所以测量从我们当前点到每个历史点的距离并保持最接近的匹配:
for each historicalpoint
distance = sqrt(
pow(currentpoint.temp - historicalpoint.temp, 2) +
pow(currentpoint.wind - historicalpoint.wind, 2) +
pow(currentpoint.precip - historicalpoint.precip, 2))
if distance is smaller than the largest distance in our match collection
add historicalpoint to our match collection
remove the match with the largest distance from our match collection
next
这是一种蛮力的方法。如果你有时间,你可以变得更漂亮。多维数据可以表示为像kd-trees或r-trees这样的树。如果您有大量数据,将您当前的观察结果与每个历史观察结果进行比较将太慢。树木可以加快您的搜索速度。您可能想看看Data Clustering and Nearest Neighbor Search。
干杯。