Pichu or Pikachu? pt.2

A M
2 min readSep 14, 2023

--

So I redid pretty much all of my code. It said in the instructions to use what we’ve learned so far in Python and Pandas was not a part of it.

This time I used a lot of lists and list comprehension already at the reading of the .txt file. I also threw in some and int() and float() to get it out of the way. To plot the points I made a dictionary of columns, other than that the code remains the same.

sns.relplot(data=TRAINING_COLUMNS, x='Width', y='Height', hue='Label', hue_order=[0,1])
plt.show()

The tricky part was calculating the manual cost function to calculate distance between test and training point. I ended up making like 4 nested list comprehensions:

  • First one to just calculate the distances of all points to test point, divided into the same amount of lists as rows of test points. In this case 4, times the amount of training points equaled to 600 neighbor distances.
  • Second nested list comprehension was a copy of the first one, but sorted and shortened down to only 10 closest neighbors. Ergo 40 distances.
  • Third was for the indexes of the closes neighbors according to their unsorted index in the first one. In dataframe you could see the entire row, here we had to blindly rely on the index.
  • The last nested list comprehension was for the actual training points that corresponded to the indexes.

Complex, but it worked like a charm.

To classify the test data I looped through the last list comprehension and used the .count(1) method to see which test data distances was connected to which training data point row and how many 1 that row contained. 5 or more classified it as a Pikachu.

I was done, it was automated but I was on fire. So I ended up some bonus instructions, including a function to add new test points that automatically would get ran through the pipeline and a menu for a user to choose to plot graph, classify straight away or add new test point.

I’m done for today. Wasn’t really looking forward to documenting it but I still did it and I kinda started getting into it.

Gnight

--

--