当我尝试在 KNN 算法中为 minkowski 度量定义 p 值小于 1 时,我面临以下错误。 谁能告诉我如何将 minkowski 指标的 p 值调整为小于 1
sc=StandardScaler()
p_kn =Pipeline([('sc',sc),('kn',KNeighborsClassifier())])
grid_kn={'kn__n_neighbors':np.arange(3,30),'kn__weights':['uniform','distance'],'kn__p':[1,2,0.5]}
KN=GridSearchCV(p_kn,grid_kn,'accuracy',cv=cv)
KN.fit(x,y)
低于错误
ValueError: p must be greater than one for minkowski metric
最佳答案
你不能,仅仅因为 p < 1
Minkowski 距离不是一个度量,因此它对于任何基于距离的分类器(例如 kNN)没有用处;来自Wikipedia :
For p ≥ 1, the Minkowski distance is a metric as a result of the Minkowski inequality. When p < 1, the distance between (0,0) and (1,1) is
2^(1 / p) > 2
, but the point (0,1) is at a distance 1 from both of these points. Since this violates the triangle inequality, for p < 1 it is not a metric.
关于python - 如何在 KNN 中的 minkowski 度量中设置 p < 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54854740/