Answered step by step
Verified Expert Solution
Link Copied!

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 #

image text in transcribedhelp 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) in 35 class_counter[cls] = 1 36 # sort the class_counter in descending order -> 37 sorted_counter = sorted (class_counter.items(), key=operator.itemgetter(1), rever se=True) 38 39 NameError: name 'class_counter' is not defined

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions

Question

=+What is your height and weight?

Answered: 1 week ago

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago