我正在编写代码来对测试和训练数据的矩阵执行k-NN搜索。有三个矩阵有问题;测试数据,训练数据和矩阵是一列,并且包含训练数据的每个行向量的相应类。我已经定义了一个函数,当给出距离矩阵的行时,将每个距离与一个类配对,并返回与它们的类的k个最小距离。这是函数;
def closest(distanceRow, classes, k):
labeled = []
for x in range(distanceRow.shape[0]):
# | each element in the row corresponds to the distance between one training vector
# | and one test vector. Each distance is paired with its respective training
# | vector class.
labeled.append((classes[x][0], distanceRow[x]))
# | The list of pairs is sorted based on distance.
sortedLabels = labeled.sort(key=operator.itemgetter(1))
k_values = []
# | k values are then taken from the beginning of the sorted list, giving us our k nearest
# | neighbours and their distance.
for x in range(k):
k_values.append((sortedLabels[x]))
return k_values
当我运行代码时,我在行
处出现类型错误k_values.append((sortedLabels[x]))
我得到TypeError:' Nonetype'对象没有属性' getitem '我不确定为什么。
非常感谢任何帮助!