简体   繁体   English

KNeighborsClassifier欧式距离计算

[英]KNeighborsClassifier Euclidean Distance calculation

I have a question regarding KNeighborsClassifier 我有一个关于KNeighborsClassifier的问题

This is the code I have for iris dataset. 这是iris数据集的代码。

iris = datasets.load_iris()
X = iris.data # Independent variables
y = iris.target # response or target or dependent variables

x_train, x_test, y_train,y_test = (train_test_split(X,y, test_size=0.3,
                                                   random_state=42,
                                                   stratify=y))

knn = KNeighborsClassifier(n_neighbors = 5)
knn.fit(x_train, y_train)
prediction = knn.predict(x_test)
print (accuracy_score(y_test, prediction))

So I know the distance between two points are calculated using Euclidean Distance. 所以我知道两点之间的距离是使用欧几里德距离计算的。

For example train iris data set has 4 features and test iris data set also has 4 features so how is euclidean distance calculated between these 4 column values. 例如,火车虹膜数据集具有4个特征,而测试虹膜数据集也具有4个特征,那么如何计算这4个列值之间的欧式距离。 Assume this is our train data 假设这是我们的train数据

array([[5.1, 2.5, 3. , 1.1],
       [6.2, 2.2, 4.5, 1.5],
       [5.1, 3.8, 1.5, 0.3],
       [6.8, 3.2, 5.9, 2.3]]

And this is our test data 这是我们的test数据

array([[7.3, 2.9, 6.3, 1.8],
       [6.1, 2.9, 4.7, 1.4],
       [6.3, 2.8, 5.1, 1.5],
       [6.3, 3.3, 4.7, 1.6]]

How is the Euclidean distance calculated for four points? 如何计算四个点的欧几里得距离?

Same as you would for two points on a plane, or for 3 points in a 3D space - but now extended to a 4 dimensional vector space: 与您在平面上的两个点或在3D空间中的3个点相同-但现在扩展到4维向量空间:

sqrt((a1-b1)^2+(a2-b2)^2+(a3-b3)^2+(a4-b4)^2)

This extends to any number of dimensions 这可以扩展到任何尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM