我从头开始学习了python KNN:https://machinelearningmastery.com/tutorial-to-implement-k-nearest-neighbors-in-python-from-scratch/ 我想从这里实现交叉验证: https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/ 这是我的实现:
#Input
file = 'wdbc.csv'
fitur = 30
k = 2
n_folds = 2
#split = 0.80
#Proses
dataset = loadDataset(file, fitur, trainingSet, testSet)
folds = cross_validation_split(dataset, n_folds)
for fold in folds:
trainingSet = list(folds)
trainingSet.remove(fold)
trainingSet = sum(trainingSet, [])
#print(trainingSet)
testSet = list()
predictions = []
for row in fold:
row_copy = list(row)
testSet.append(row_copy)
#row_copy[-1] = None
for x in range(len(testSet)):
neighbors = getNeighbors(trainingSet, testSet[x], k)
result = getResponse(neighbors)
predictions.append(result)
但是它的输出有错误:
File "C:/Users/user.compaq/Documents/help/cobaknn_crossval.py", line 45, in euclideanDistance
distance += math.pow((instance1[x] - instance2[x]), 2)
TypeError: unsupported operand type(s) for -: 'str' and 'float'
我该如何解决?我已将所有instance1 [x]和instance2 [x]都保证为浮点型,但我不知道程序如何识别其中之一为字符串。 帮我。谢谢。