не выполняет компиляцию
на pycharm не работает ошибка компиляции
import csv
with open(r'C:\Users\Dell\Desktop\iris-data.csv') as csvfile:
lines=csv.reader(csvfile)
for row in lines:
print(','.join(row))
import csv
import random
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] = float(dataset[x][y])
if random.random() < split:
trainingSet.append(dataset[x])
else:
testSet.append(dataset[x])
trainingSet=[]
testSet=[]
loadDataset(r'C:\Users\Dell\Desktop\iris-data.csv',0.66,trainingSet,testSet)
print('Train: ' + repr(len(trainingSet)))
print('Test: ' + repr(len(testSet)))
import math
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))'''
import operator
def getNeighbours(trainingSet,testInstance,k):
distance=[]
length=len(testInstance)-1
for x in range(len(trainingSet)):
dist=euclideanDistance(testInstance,trainingSet[x],length)
distance.append((trainingSet[x],dist))
distance.sort(key=operator.itemgetter(1))
neighbours=[]
for x in range(k):
neighbours.append(distance[x][0])
return neighbours
'''trainSet=[[2,2,2,'a'],[4,4,4,'b']]
testInstance=[5,5,5]
k=1
neighbours=getNeighbours(trainSet,testInstance,1)
print(neighbours)'''
import operator
def getResponse(neighbours):
classVotes={}
for x in range(len(neighbours)):
response=neighbours[x][-1]
if response in classVotes:
classVotes[response]+=1
else:
classVotes[response]=1
sortedVotes=sorted(classVotes.iteritems(),key=operator.itemgetter(1),reverse=True)
return sortedVotes[0][0]
def getAccuracy(testSet,prediction):
correct=0
for x in range(len(testSet)):
if testSet[x][-1] is prediction[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)'''
def main():
trainingSet=[]
testSet=[]
split = 0.67
loadDataset(r'C:\Users\Dell\Desktop\iris-data.csv', split, trainingSet, testSet)
print 'Train set: ' + repr(len(trainingSet))
print 'Test set: ' + repr(len(testSet))
predictions=[]
k = 3
for x in range(len(testSet)):
neighbours = getNeighbours(trainingSet, testSet[x], k)
result = getResponse(neighbours)
predictions.append(result)
print('> predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1]))
accuracy = getAccuracy(testSet, predictions)
print ('Accuracy: ', accuracy)
main()
Файл "C: /Users/Dell/PycharmProjects/regression/knnalgorithm.py", строка 87
выведите 'Train set:' + repr (len (trainingSet))
^
Ошибка синтаксиса: неверный синтаксис
Файл "C: /Users/Dell/PycharmProjects/regression/knnalgorithm.py", строка 87
выведите 'Train set:' + repr (len (trainingSet))
^
Ошибка синтаксиса: неверный синтаксис