Python Forum
KNN algorithm Assignment can't understand the question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
KNN algorithm Assignment can't understand the question
#6
(May-24-2020, 01:03 PM)jefsummers Wrote: It's a set of steps. Do number 1 - load the data. Can you do that?
I could do the following way but my professor wantes me to do it using sklearn where I'm helpless

def knn(data, query, k, distance_fn, choice_fn):
neighbor_distances_and_indices = []

# 3. For each example in the data
for index, example in enumerate(data):
# 3.1 Calculate the distance between the query example and the current
# example from the data.
distance = distance_fn(example[:-1], query)

# 3.2 Add the distance and the index of the example to an ordered collection
neighbor_distances_and_indices.append((distance, index))

# 4. Sort the ordered collection of distances and indices from
# smallest to largest (in ascending order) by the distances
sorted_neighbor_distances_and_indices = sorted(neighbor_distances_and_indices)

# 5. Pick the first K entries from the sorted collection
k_nearest_distances_and_indices = sorted_neighbor_distances_and_indices[:k]

# 6. Get the labels of the selected K entries
k_nearest_labels = [data[i][1] for distance, i in k_nearest_distances_and_indices]
Reply


Messages In This Thread
RE: KNN algorithm Assignment can't understand the question - by spoooder - May-24-2020, 01:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Question code from datacamp I don't understand fad3r 3 3,940 Feb-04-2018, 08:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020