代码详解:用Python构建邻域矩阵( 五 )


[3 4 5
[6 7 8
[9 10 11

)

索引数组是一个numpy数组 , 包含M(测试样本数)行和K(邻域数)列 。 那么如何将其转换为我们需要的矩阵呢?示例如下:

在一个情节中看到所选邻域会很有趣:

import matplotlib.pyplot as plt

n = 0 # first element in the test dataset

xy_test = XY_test[n

index = indices[n

neighbours = XY_train[index

plt.clf()

plt.scatter(xy_test[0
xy_test[1
color="red")

plt.scatter(neighbours[:0
neighbours[:1
color="blue")

plt.xlabel("x")

plt.ylabel("y")

推荐阅读