OK, so in this lecture, I'm going to give you an official exercise prompt in preparation for the next
coding lecture.
By the end of this lecture, you should know exactly what you need to do in order to complete the exercise
yourself without any further assistance.
So after watching this lecture, try to make sure that you don't watch the coding lecture without trying
the exercise first.
This lecture will give you a few extra pointers and tips.
Although technically, you could do the exercise already at this point.
OK, so just at a very high level, what is the exercise?
The exercise is to take two sets of poems by two different authors, Edgar Allan Poe and Robert Frost.
The Euros for these poems will be given to you in the coming notebooks.
So don't try to find them yourself or to type in your rows by hand as some students sometimes try to
do.
Instead, it's perfectly OK to grab the URLs from the notebooks directly.
Please don't feel bad about doing that.
Obviously, that's as far as you should go in terms of looking at the notebook.
If you want to do the exercise without cheating, the next step is to build a text classifier to distinguish
between the two authors, as described in the previous lecture.
This will involve building two separate Markov models, one for each of the poets.
Now, after building the model, you'll want to check how well the model performs by computing the train
and test the accuracy.
Furthermore, since the classes may be imbalanced, you might also want to use a metric that takes this
into account, such as the F1 score.
OK, so that's pretty much the whole exercise from a bird's-eye point of view.
Now, let's go through some details concerning the exercise, specifically, what I would like to do
is give you a step by step outline of the code.
Note that this is specific to my implementation.
So if you had other ideas about how you wanted to implement the high level steps, then these details
may not apply to you.
OK, so the first step will be to live through each data file and save each line of each file into a
list.
So each line of each poem will be considered one sample into our machine learning model.
Furthermore, while we do this, we're also going to keep track of the labels that is, which line belongs
to which poet.
So that will be another list.
The next step will be to do a train test split where we randomly split the data into train and test.
At this point, our input data is still in the form of lines of text.
Of course, this is not appropriate input for a machine learning model.
As you recall, we need to implement a Markov transition matrix where each state represents a word in
the vocabulary.
Therefore, we need to know which indices in the matrix correspond to which words.
Therefore, what we need is a mapping from unique word to unique integer index.
So the next step will be to loop through our data, collecting all the unique words.
Note that this will involve tokenizing each line of text, each of which currently contains multiple
words.
It's up to you how you want to do tokenization, but for this exercise, a simple string split should
suffice.
From these unique words will be able to assign each of them a unique integer index, which will then
be used as indices into a markup matrix.
So you should know that when we use the word mapping, typically that refers to a Python dictionary.
Also note that we'll need a special index for unknown words.
This is because there may be words in the test set that do not appear in the train set.
OK, so once you have your word to integer index mapping, you want to then convert all the lines of
text into integer representation.
This will be useful when we want to index the state transition matrices since we can use the integers
directly.
OK, so the next step is to Trina Markov model for each class, as you recall, we have two classes
Robert Frost and Edgar Allan Poe.
Each Markov model should be trained on lines of poems only for their respective poet.
So one Markov model will be trained on Edgar Allan Poe Lines, while the other Markov model will be
trained on Robert Frost lines.
Of course, the implementation details are your exercise.
Note that you want to use some form of smoothing, as mentioned in the previous lectures.
Furthermore, you should consider whether you need the A's in the Pies directly, or whether you only
need the log of the A's and the log of the pot.
In addition, in order to apply Bayes rule, you'll also need the priors, so compute these from the
data as well.
At this point, you should have everything you need to make a prediction.
Your data is stored as integers, your Markov models are trained and you have the priors for each class.
So the next step will be to write a function that can actually make predictions.
Specifically, it should take in an input sequence and it should compute the log posterior for each
class.
Then it should take the ARG Max to get the prediction for that input sequence.
And remember that there is no need to compute the full posterior since the denominator is constant for
each class.
Now, of course, once you have a function to make predictions, you can then compute the predictions
for both the train and test set.
After doing so, you should compute the accuracy for both the train and test set as a bonus, check
whether or not the classes are imbalanced.
Note that this information is in the priors.
If the classes are imbalanced, then compute the confusion matrix to see which class has incorrect predictions
most often.
Furthermore, you may want to compute a metric like the F1 score, which takes into account any imbalance
in the classes.
OK, so after having completed all these steps, the exercise will be done.
So please try this yourself and I'll see you in the next lecture.