Okay so if you come across errors while using a tutorial from an old post? Its highly likely that you are using python2 code in python3
File “”, line 4 print “Train:” + repr(len(trainingSet)) ^ SyntaxError: invalid syntax
Like that error for example. It was making me frustrated as hell when I was trying to run it in a jupyter notebook which was using python3 as default.
The correct input text was supposed to be
print (‘, ‘).join(row)
instead of
print ‘, ‘.join(row)
which would have worked in case we were still on py2
Seriously, these errors are scary and can get in way of you trying to learn or just push you into a state where you’d be like:
Its okay. Its normal, I’ve been there. Just take a walk, come back and google search what did you screw up in 🙂
So I am trying to learn this k-nearest neighbours algorithm from scratch from:
Tutorial To Implement k-Nearest Neighbors in Python From Scratch
I gotta say this is an awesome site indeed. Lets see if I can copy it at least!
What is it used for? I got no clue.
What I wanna do is take an input of labeled data and learn from that if the number is prime or not. Based on that model, I wanna be able to give it some random number and it should be able to predict if the number is prime or not. To do that I searched on google and came across k-nn Algorithm. It was being implemented on the Iris data set. I’ll try to modify it for my problem after I figure out whats going on in KNN.
A little ahead after I managed to fix the dumb print error?
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-29717066dae6> in <module>()
1 import csv
----> 2 with open('iris.data', 'rb') as csvfile:
3 lines = csv.reader(csvfile)
4 for row in lines:
5 print (', ').join(row)
FileNotFoundError: [Errno 2] No such file or directory: 'iris.data'
That comes along..
According to that error? It means that iris.data file isnt available in the working directory where my notebook is open right now. Okay, I downloaded and ran it again.
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-29717066dae6> in <module>()
1 import csv
----> 2 with open('iris.data', 'rb') as csvfile:
3 lines = csv.reader(csvfile)
4 for row in lines:
5 print (', ').join(row)
FileNotFoundError: [Errno 2] No such file or directory: 'iris.data'
STILL THE SAME THING!
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-29717066dae6> in <module>()
1 import csv
----> 2 with open('iris.data', 'rb') as csvfile:
3 lines = csv.reader(csvfile)
4 for row in lines:
5 print (', ').join(row)
FileNotFoundError: [Errno 2] No such file or directory: 'iris.data'
Turns out, the file that was downloaded was iris.data.txt
UGHH, Just that TXT held me back one more minute.
Lemme try executing that command without the txt extension in file name.
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-2-29717066dae6> in <module>()
2 with open('iris.data', 'rb') as csvfile:
3 lines = csv.reader(csvfile)
----> 4 for row in lines:
5 print (', ').join(row)
Error: iterator should return strings, not bytes (did you open the file in text mode?)
GREAT ANOTHER ERROR
First trial at bug fix, when I was downloading the file, it would show as a text edit file over and over agian..
I went ahead to open the downloaded file in atom and copied the contents of that “txt” file.
created a new “iris.data” file in atom and saved it.
Lets see if it works this time…
Well…
Nothing moving ahead.
This one was a fail. Couldnt copy KNN Algorithm from that site!
Edit: This was from the time when I was getting started with coding for real. Looking back at where I was, makes me feel really good about the level of progress that I’ve made in personal growth.
Last modified on 2020-07-26