0
import csv  
import random  
import math  
import operator  



def loadDataset(filename,trainingSet=[],testSet=[]):  
    with open(filename, 'rt') as csvfile:  
        lines = csv.reader(csvfile)  
        dataset = list(lines)  
        z = len(dataset)-1  


 for x in range(len(dataset)-2):  
            for y in range(8,9):  
                dataset[x][y] = float (dataset[x][y])   
                trainingSet.append(dataset[x]) 


    for y in range(8,9):  
            dataset[z][y] = float (dataset[z][y])  
            testSet.append(dataset[z])


def euclideanDistance(instance1, instance2):  
    distance = 0  
    X= (instance1[9] - instance2[9]) +(instance1[8] - instance2[8])  
    distance += pow(X, 2)  
    return math.sqrt(distance)   

def getNeighbors(trainingSet, testInstance, k):  
    distances = []  
    for x in range(len(trainingSet)):  
        dist = euclideanDistance(testInstance, trainingSet[x])  
        distances.append((trainingSet[x], dist))  
    distances.sort(key=operator.itemgetter(1))  
    neighbors = []    
    for x in range(k): 
        neighbors.append(distances[x][0])  
    return neighbors  

def main():  
    trainingSet=[]  
    testSet=[]  
    loadDataset('G:\ABCD.csv', trainingSet, testSet)  
    print ('Train set: ' + repr(len(trainingSet)))  
    print ('Test set: ' + repr(len(testSet)))  
    k = 4     
    neighbors = getNeighbors(trainingSet, testSet[0], k)  
    a=(neighbors[0][1])  
    print('Best Neighbor is: ' + a)  

main()  

Error I am getting Dataset Screenshot

I am getting TypeError while executing the code basically in this program i am trying to find euclidian distance from a test point to each point in the given dataset and then after sorting trying to get neighbors with least distance .

1 Answer 1

1

The error says you are trying to subtract a string from a string (line 22 in your euclidianDistance function)

You need to parse the two co-ordinates into numbers to be able to subtract them. The float function will be able to do that.

Example - you're using instance1[9] which is a string representing a floating point number, so float(instance1[9]) should give you a number.

Just leave a comment if you're still struggling and I'll show you the update you need to make.

Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. Looks like you're new here, in case you don't know - question askers will typically click the "accept" checkmark on the answer that solved their problem to show others which answer helped (and gives the person who answered some reputation)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.