相关疑难解决方法(0)

Sklearn KNeighborsRegressor 自定义距离度量

我正在使用 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)
Run Code Online (Sandbox Code Playgroud)

我还尝试直接从 KNeighborsRegressor 构造函数调用 customDistance ,例如:

knn_regression = KNeighborsRegressor(n_neighbors=15, metric=customDistance)
Run Code Online (Sandbox Code Playgroud)

函数执行的两种方式,但结果有点奇怪。首先,我希望从我的 DataFrame 中看到函数输入 A 和 B 行,但我得到的是:

[0.87716989 11.46944914 1.00018801 1.10616031 1.] [ 1. 9. 0. …
Run Code Online (Sandbox Code Playgroud)

distance metric knn scikit-learn

5
推荐指数
0
解决办法
808
查看次数

标签 统计

distance ×1

knn ×1

metric ×1

scikit-learn ×1