我正在尝试在python中从头实现KNN。所有功能都可以正常工作,直到我进入main()函数
import csv
import random
import math
import operator
with open('iris.csv', 'r') as csvfile:
lines = csv.reader(csvfile)
for row in lines:
print(', '.join(row))
在这里我创建了一个函数来读取iris.csv文件
def loadDataset(filename, split, trainingSet=[] , testSet=[]):
with open(filename, 'r') as csvfile:
lines = csv.reader(csvfile)
dataset = list(lines)
for x in range(len(dataset)-1):
for y in range(4):
dataset[x][y] = dataset[x][y]
if random.random() < split:
trainingSet.append(dataset[x])
else:
testSet.append(dataset[x])
这是我测试功能的地方
trainingSet=[]
testSet=[]
loadDataset('iris.csv', 0.66, trainingSet, testSet)
print('Train: ' + repr(len(trainingSet)))
print('Test: ' + repr(len(testSet)))
这是距离函数
def euclideanDistance(instance1, instance2, length):
distance = 0
for x in range(length):
distance += pow((instance1[x] - instance2[x]), 2)
return math.sqrt(distance)
测试距离函数
data1 = [2, 2, 2, 'a']
data2 = [4, 4, 4, 'b']
distance = euclideanDistance(data1, data2, 3)
print('Distance: ' + repr(distance))
邻居功能
def getNeighbors(trainingSet, testInstance, k):
distances = []
length = len(testInstance)-1
for x in range(len(trainingSet)):
dist = euclideanDistance(testInstance, trainingSet[x], length)
distances.append((trainingSet[x], dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
测试邻居功能
trainSet = [[2, 2, 2, 'a'], [4, 4, 4, 'b']]
testInstance = [5, 5, 5]
k = 3
neighbors = getNeighbors(trainSet, testInstance, 1)
print(neighbors)
获得响应的功能
def getResponse(neighbors):
classVotes = {}
for x in range(len(neighbors)):
response = neighbors[x][-1]
if response in classVotes:
classVotes[response] += 1
else:
classVotes[response] = 1
sortedVotes = sorted(classVotes.items(), key=operator.itemgetter(1), reverse=True)
return sortedVotes[0][0]
测试功能
neighbors = [[1,1,1,'a'], [2,2,2,'a'], [3,3,3,'b']]
response = getResponse(neighbors)
print(response)
精度函数
def getAccuracy(testSet, predictions):
correct = 0
for x in range(len(testSet)):
if testSet[x][-1] is predictions[x]:
correct += 1
return (correct/float(len(testSet))) * 100.0
测试准确性功能
testSet = [[1,1,1,'a'], [2,2,2,'a'], [3,3,3,'b']]
predictions = ['a', 'a', 'a']
accuracy = getAccuracy(testSet, predictions)
print(accuracy)
出现错误所在的main()
#main
def main():
# prepare data
trainingSet=[]
testSet=[]
split = 0.67
loadDataset('iris.csv', split, trainingSet, testSet)
print ('Train set: ' + repr(len(trainingSet)))
print ('Test set: ' + repr(len(testSet)))
# generate predictions
predictions=[]
k = 3
for x in range(len(testSet)):
neighbors = getNeighbors(trainingSet, testSet[x], k)
result = getResponse(neighbors)
predictions.append(result)
print('> predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1]))
accuracy = getAccuracy(testSet, predictions)
print('Accuracy: ' + repr(accuracy) + '%')
main()
但是当我进入main()函数时,会收到此错误消息
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-68-ba4ef0fcfe9f> in <module>()
18 print('Accuracy: ' + repr(accuracy) + '%')
19
---> 20 main()
<ipython-input-68-ba4ef0fcfe9f> in main()
11 k = 3
12 for x in range(len(testSet)):
---> 13 neighbors = getNeighbors(trainingSet, testSet[x], k)
14 result = getResponse(neighbors)
15 predictions.append(result)
<ipython-input-62-d13cbe2070b3> in getNeighbors(trainingSet, testInstance, k)
3 length = len(testInstance)-1
4 for x in range(len(trainingSet)):
----> 5 dist = euclideanDistance(testInstance, trainingSet[x], length)
6 distances.append((trainingSet[x], dist))
7 distances.sort(key=operator.itemgetter(1))
<ipython-input-60-93ee3f7cf267> in euclideanDistance(instance1, instance2,
length)
2 distance = 0
3 for x in range(length):
----> 4 distance += pow(float(instance1[x] - instance2[x]), 2)
5 return math.sqrt(distance)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
答案 0 :(得分:3)
您已经发布了太多的代码,但是幸运的是,错误消息和堆栈跟踪很清楚:您正在尝试“减去”字符串而不是数字这两项。 tableView.value(forKey: "pendingSelectionIndexPath") as! IndexPath
包将所有内容返回为字符串,因此在将数字列视为数字之前,需要将结果中的数字列显式转换为UIViewControllerPreviewingDelegate
(或csv
)。