Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 KNN algorithm Assignment can't understand the question
#1
The below is the question, I have no knowledge regarding KNN , been following some tutorials but can't get the jest of the question. Someone please explain it to me. Thnaks



1. Load the data.
2. Initialize K to your chosen number of neighbors.
3. For each example in the data (read from file):
3.1 Calculate the distance between the query(user input) example and the current example from
the data.
3.2 Add the distance and the index of the example to an ordered collection (your chosen data
structure).
4. Sort the ordered collection of distances and indices from smallest to largest (in ascending order) by the
distances.
5. Pick the first K entries from the sorted collection.
6. Get the labels of the selected K entries.
7. Depending on the labels you obtained, classify your new point.


a. Based on this algorithm, write a Python function myknn that takes as inputs: 1. the value of K
(no. of neighbors) and, 2. one data(example) to be classified, and performs k-NN based
classification of that input point based on some old data which is read from the given file
(Entrance_Selection.csv). The function should return the output class of the given input example.
Make sure your function gives an exception if there is any problem when reading the file. Hint: Use
exception handling.
Note: The default value of k should be 5.
Quote
#2
And what have you tried?
pyzyx3qwerty
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Quote
#3
A challenge, indeed, but on what step are you stuck? What have you gotten so far (and what does the data look like)?
Quote
#4
(May-24-2020, 11:43 AM)jefsummers Wrote: A challenge, indeed, but on what step are you stuck? What have you gotten so far (and what does the data look like)?
I actually don't really understand the question.

I tried using

(May-24-2020, 06:37 AM)pyzyx3qwerty Wrote: And what have you tried?
I watched a ton of videos and Ipreety much undersatnd the topic now but I don't really get what the question's wants
Quote
#5
It's a set of steps. Do number 1 - load the data. Can you do that?
Quote
#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]
Quote
#7
I think you need clarification. You can use sklearn to do your knn classification, typically you would use Pandas to read the data. If you search the sklearn docs for distance you will see lots of options so you can use sklearn for that.

I remain confused as to whether they are wanting you to mimic the knn process, measuring distances from arbitrary staring points and assigning to the cluster by which point the data is closest to, or something else. Is that what they want?
Quote

Top Page

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

Forum Jump:


Users browsing this thread: 1 Guest(s)