我正在使用 KNeighborsRegressor,但我想将它与自定义距离函数一起使用。我的训练集是 pandas DataFrame,如下所示:
week_day hour minute temp humidity
0 1 9 0 1
1 1 9 0 1
2 1 9 0 1
3 1 9 0 1
4 1 9 1 1
...
def customDistance(a, b):
print a, b
return np.sum((a-b)**2)
dt = DistanceMetric.get_metric("pyfunc", func=customDistance)
knn_regression = KNeighborsRegressor(n_neighbors=15, metric='pyfunc', metric_params={"func": customDistance})
knn_regression.fit(trainSetFeatures, trainSetResults)
我还尝试直接从 KNeighborsRegressor 构造函数调用 customDistance,例如:
knn_regression = KNeighborsRegressor(n_neighbors=15, metric=customDistance)
这两种方法都会执行函数,但结果有点奇怪。首先,我希望看到 DataFrame 中的函数输入 A 和 B 行,但我得到的却不是:
[0.87716989 11.46944914 1.00018801 1.10616031 1.] [ 1. 9. 0. 1. 1.]
第二个属性 B 显然是我的训练集中的行,但我无法澄清第一行来自哪里?如果有人可以解释或发布将自定义距离函数正确插入到上述算法中的示例,我们将不胜感激。
提前致谢。
最好的问候,克莱门