Answered step by step
Verified Expert Solution
Question
1 Approved Answer
help with my python code... following a textbook code and keep getting this name error dont understand why ; this is the entire function #
help with my python code... following a textbook code and keep getting this name error dont understand why ; this is the entire function
# our own KNN model def knn(training points, test_point, k): distances = {} # the number of axes we are dealing with dimension = test_point.shape[1] #calculating euclidean distance between each #point in the training data and test data for x in range (len(training points)): dist = euclidean_distance(test_point, training_points.iloc[x], dimension) # record the distance for each training points distance[x] = dist[@] #sort the distances sorted_d = sorted (distance. items(), key=operator.itemgetter(1)) # to store the neighbors neighbors = [] #extract the top k neighbors for x in range(k): neighbors.append(sorted_d[x] [@]) #for each neighbor found, find out its class class_counter for x in range(len(neighbors)): #find out the class for that particular point cls = training_points.iloc [neighbors[x]] [-1] if cls in class_counter: class_counter(cls] += 1 else: class_counter[cls] = 1 # sort the class_counter in descending order sorted_counter = sorted(class_counter.items(), key=operator.itemgetter(1), reverse=True) #return the class with the most count as well as the neighbors found return(sorted_counter[@] [@], neighbors) NameError Traceback (most recent call last)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