Now that we've covered the theory behind the Naive Bayes Classifier in detail, it's time to get our hands 1
dirty and write some Python code. In this lesson, 2
we've got a big and important topic coming up, namely how do you get your code to talk to your computer's 3
operating system? 4
How do you read a file? 5
Now a lot of this knowledge is going to be transferable to whatever programming language you're working 6
in and whatever project you're working in. 7
But with Python and machine learning, you're gonna be working with a lot of data and that data is gonna 8
be stored somewhere. 9
Most likely that's going to be stored locally on your computer or it's gonna be stored on a server. For 10
many of our projects, our Python code is going to need to interact with our own computer's operating 11
system and actually be able to read the files from the disk. 12
So while we're on this topic about learning how to read files, we're also going to cover quite a few 13
other topics as well, namely file paths, file locations, file extensions. 14
We're also going to see how our email data is actually structured and we're also going to see how we 15
can extract a message body from a raw email. 16
But first things first. 17
Whenever you're referencing a file in your Python code you're going to need to specify exactly where 18
that file is and what that file is called. 19
And this means you have to specify two things, namely the file path and the file name. In the lesson resources 20
that you've downloaded and added to your projects folder, 21
I included a practice email. 22
So taking a peek inside the spam data folder and then opening "01_Processing", you should see a file called 23
"practice_email.txt". 24
Now, the file name for this file should be pretty obvious because we're looking right at it. 25
It's "practice_email.txt". That "txt" part is called the file extension and this signals 26
what kind of file this is. A txt file 27
is just a boring old text file, but the thing is, these file extensions are key to the computer or you 28
or me understanding what kind of file it is that we're working with. 29
The extension tells us something about a file's format. 30
So for example, Microsoft Word documents have the extension "doc" or "docx" and PDFs have the extension 31
".pdf". Windows executable files on the other hand have the very dangerous extension ".exe". 32
So if you ever receive an email that has an attachment with a ".exe" extension, don't open the attachment. 33
This was always the classic way that people installed viruses on their computers back in the day. 34
I was going through my spam directory recently and one thing I noticed actually was that more and more 35
folks are sending out their malicious code with the ".js" or JavaScript extension instead of 36
"exe". 37
So yeah, it's always good to pay close attention to these file extensions. Now that we've talked about 38
the file name and the file extension, 39
what about this other thing that I've mentioned? 40
What about the path? The path or the file path is the location of the file. 41
Let me quickly show you how you can actually view the file path on Mac 42
and afterwards I'll show you how to see it on Windows. 43
If you're using a Mac, then you can open Finder and you can go to this "practice_ 44
email.txt" file in Finder and then you can go to "File" > "Get Info" or Command+I 45
and what you can see right here is the location of this file. 46
It's on my hard drive, under my username, inside my projects folder, 47
then SpamData and then 01_Processing. 48
This here is the location of the "practice_email.txt" file. 49
This right here is my file path. If you're running Windows on the other hand, you can always see the file 50
path and the location in this address bar right up here. 51
So right now this is the project's folder, 52
double clicking on SpamData will open my folder and you can see now that the location here has updated. 53
If I open "Processing" then I can clearly see the full file path to this practice email right here in 54
my address bar. If I click inside the address bar, then we can see the names of the folders and the sub 55
folders separated by a backslash. 56
In other words, my Processing folder is inside SpamData which is inside my projects folder which is 57
inside my username folder under Users and then on my C drive. 58
This long piece of text that we're looking at right here is the full path. And this brings me to the 59
two different kind of paths that you'll be encountering. 60
The first one is actually called an absolute path. Think of the absolute path as the long form. 61
This is the full path to the file or folder. On Windows, 62
you often see the absolute path starting with C and then going to your particular user profile and then 63
down to a particular folder. On Mac, 64
the absolute path to the practice email would look something more like this. 65
All right. 66
I think that's easy enough. But, while the absolute path is the long form, there is also a shorthand. You 67
see, you don't always have to work with an absolute path. 68
Instead, you can work with what's called a relative path. A relative path uses your current location as 69
the starting location and then points to a file or folder from there. 70
So suppose I'm in my MLProjects folder in Finder, then the relative path to the practice email would 71
be SpamData, 01_Processing 72
and there it is. 73
There is my "practice_email.txt" file. As such, the relative path from my MLProjects 74
folder to my practice_email.txt file would look something like this. 75
What you're looking at here is actually the formatting for windows. On a Mac, 76
you'd have a relative path that would look more like this. Notice, 77
the only difference are the slashes. Mac uses the forward slash and Windows uses the backslash in front 78
of the file and folder names. 79
Now this might be a small thing to point out, but the direction of the slashes actually matters when 80
you're writing your Python code. 81
So even though in Windows you see backslash is used in the path on the address bar, 82
it's actually a little problematic to try and just copy paste what's in here and the reason is is that 83
in Python you actually want to use a forward slash, because the backslash has a special purpose in the 84
Python programming language. 85
Let's fire up a new Python notebook and I'll show you exactly what I mean and why we want to use forward 86
slashes. In Jupyter navigate to your MLProjects folder and then click "New" > 87
"Python 3" notebook. Let's rename this notebook to "06 Bayes Classifier" and click "Rename". Say we print a line 88
'What is up?' and hit Shift+Enter. 89
Then we see this text printed below. 90
But what if we wanted to write 'What's up?'. 91
So if I wanted to write "What's up?" with an apostrophe, 92
you can see that I now have three single quotes in this string and that means that my string starts 93
here, ends here and this part onwards here is considered Python code and then it starts another string 94
here. 95
In other words, Python needs to know that it should ignore this single quote and for that we use the 96
backslash. The backslash escapes the character that follows it. 97
So by using the backslash, this single quote here is treated as part of the string 98
and if I hit Shift+Enter, I can get that apostrophe to show up in my output. 99
And that's also why when you're providing a path as a string, you want to make sure use forward slashes 100
instead of backslash is that you get from copy and pasting from the Windows address bar. Okay, 101
so so much for the theory behind relative paths, file names, file extensions and absolute paths. In the next 102
lesson, 103
I'll show you how to read that practice email into Jupyter notebook. 104
I'm gonna go ahead and get some coffee, but I hope I'll see you on the next lesson.