0

I am using scikit k nearest neighbor algorithm. In KNeighborsRegressor() function, I pass

weight = 'distance' and n_neighbors = 2 

but it gives this error -

ValueError: operands could not be broadcast together with shapes (1,2,4) (1,2)

My Code:

X = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15],[162,137,148,159,260]]
y = [[0,1,2,3],[5,6,7,8],[44,45,46,66],[12,13,14,15]]
from sklearn.neighbors import KNeighborsRegressor
neigh = KNeighborsRegressor(n_neighbors=2,weights='distance')
neigh.fit(X, y)
print(neigh.predict([[11.5,22,13.44,15.66,66]]))

If possible, please tell me why doesn't it work. Shouldn't it just pick 2 nearest neighbors, weight them based on the distance from the query point and then predict ?

4

1 回答 1

1

正如我在每封邮件中已经告诉你的那样,KNeighborsClassifier 不支持多元回归。不过,这将很容易实现。结果与独立执行组件相同,只是效率更高。

于 2013-06-10T15:50:41.393 回答