Question
Add a wrapper to this function so that it computers multiple runs (a user-specified value) and computes an average accuracy over the multiple runs rather
Add a wrapper to this function so that it computers multiple runs (a user-specified value) and computes an average accuracy over the multiple runs rather than a single accuracy?
Code:
importcsv
importrandom
importmath
importoperator
importurllib.request
defgetLines(filename):
lines=[]
if(filename.startswith(('http','ftp','sftp'))):
#Skipdownloadingitandopendirectlyonline:
response=urllib.request.urlopen(filename)
lines=csv.reader(response.read().decode('utf-8').splitlines())
else:
#TutorialsPointIDErequires'r',not'rb'
withopen(filename,'r')ascsvfile:
#csvreaderisanobjectthatisessentiallyalistoflists
csvreader=csv.reader(csvfile)
forlineincsvreader:
lines.append(line)
returnlines
defloadDatasetFinal(filename,split,trainingSet=[],testSet=[]):
lines=getLines(filename)
#forrowinlines:
#print(','.join(row))
dataset=list(lines)
forxinrange(len(dataset)-1):
foryinrange(4):#legend=('Sepallength','Sepalwidth','Petallength','Petalwidth')
dataset[x][y]=float(dataset[x][y])
ifrandom.random() trainingSet.append(dataset[x]) else: testSet.append(dataset[x]) defeuclideanDistance(instance1,instance2,length): distance=0 forxinrange(length): distance+=pow((instance1[x]-instance2[x]),2) returnmath.sqrt(distance) defgetNeighbors(trainingSet,testInstance,k): distances=[] length=len(testInstance)-1 forxinrange(len(trainingSet)): dist=euclideanDistance(testInstance,trainingSet[x],length) distances.append((trainingSet[x],dist)) distances.sort(key=operator.itemgetter(1)) neighbors=[] forxinrange(k): neighbors.append(distances[x][0]) returnneighbors defgetResponse(neighbors): classVotes={} forxinrange(len(neighbors)): response=neighbors[x][-1] ifresponseinclassVotes: classVotes[response]+=1 else: classVotes[response]=1 sortedVotes=sorted(classVotes.items(),key=operator.itemgetter(1),reverse=True) returnsortedVotes[0][0] defgetAccuracy(testSet,predictions): correct=0 forxinrange(len(testSet)): iftestSet[x][-1]==predictions[x]: correct+=1 return(correct/float(len(testSet)))*100.0 defmain(): #setourparameters k=3 split=0.67 filename='iris.data' filename='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' #preparedata trainingSet=[] testSet=[] loadDatasetFinal(filename,split,trainingSet,testSet) print('Trainset:'+repr(len(trainingSet))) print('Testset:'+repr(len(testSet))) #generatepredictions predictions=[] forxinrange(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()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started