如何在 KNN 的 minkowski 度量中设置 p < 1?
How to set p < 1 in minkowski metric in KNN?
当我尝试为 KNN 算法中的 minkowski 度量定义小于 1 的 p 值时,我遇到了以下错误。
谁能告诉我如何将 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
闵可夫斯基距离不是度量,因此它对任何基于距离的分类器(例如 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.
当我尝试为 KNN 算法中的 minkowski 度量定义小于 1 的 p 值时,我遇到了以下错误。 谁能告诉我如何将 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
闵可夫斯基距离不是度量,因此它对任何基于距离的分类器(例如 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.