5

我正在使用 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 显然是我训练集中的行,但我无法澄清第一行来自哪里?如果有人可以解释或发布将自定义距离函数正确插入到上述算法中的示例,将不胜感激。

提前致谢。

最好的问候,克莱门

4

0 回答 0