So in this lecture, we are going to look at the count victories are in code.
Now this lecture is a bit of a preview of machine learning since technically we haven't yet discussed
those concepts in this particular section.
However, you need not be intimidated by this, since we are only going to make use of psychic learning.
As you may know, this involves only three lines of code.
In particular, we create the model objects.
We fit the model to the training data.
Then we check the models train and test performance.
So if you are not familiar with these steps, please let me know on the Q&A so we can get that fixed
for you promptly.
I have free resources to help you refresh on this process, so please take advantage of them.
Otherwise, I'm going to assume that you are comfortable with these steps.
OK, so let's start by importing everything we need.
We'll start with basic numerical computing libraries such as numpy and pandas from site to learn we'll
need the count, voucherize the class, the multi normal and B class and the train to split function.
Multi a.m. Be is our classifier of choice for this notebook.
But of course, you can feel free to use whatever you like.
Note that we also have several imports from NCTC, which should look familiar.
The next step is to download the data we need for analytic.
The next step is to download our data set, which consists of samples of BBC News.
The goal of this data set is to classify the documents.
As you recall, if you read the news articles are usually categorized into sections.
For example, there's the business section, entertainment, politics, sports and so forth.
Note that this will be a supervised task since we will be given the labels.
The next step is to read in our Series V. using PD that reads GSV.
The next step is to call the FDA ahead to see what our data looks like.
As you can see, this data has two columns, as you may recall.
This is what I described in the previous lectures.
The text column contains their documents, and the labels column contains the label for each document.
Each row is a separate document.
Please feel free to actually print out the full text yourself to get a better sense of the data we're
using.
The next step is to assign the two columns to separate variables, which I've called inputs and labels.
The next step is to plot a histogram of our labels.
The point of this is to see whether or not we have imbalanced classes.
That is if any class is over or underrepresented.
This can be an issue when we check our model's performance since, for example, if ninety nine percent
of our data belongs to one class, we can obtain ninety nine percent accuracy by only predicting that
class.
In that case, we would want to use other metrics in order to get a better sense of how our model is
doing.
So notice that our labels seem to be pretty evenly spread out because of this, we can say that our
data is not imbalanced.
And so there isn't a great need to check alternative metrics.
Also, this gives us a chance to see what labels were working with notice that we have five labels business,
entertainment, politics, sport and tech.
The next step is to do a train test, split note that we do this before using the Koun vector riser.
The next step is to instantiate our counterfactual your object.
Note that for our first experiment, we will be using all the default values and thus, for this instance,
we're not going to have any arguments.
The next step is to call fit transform on the training inputs.
We follow that by calling Transform on the test inputs.
Sometimes, as students inquire about why we call fit transform in one case, but only transform in
the other case.
Recall that the training data is supposed to represent what we have when we build our model.
The test data is supposed to represent what we have when we apply our model to data we haven't seen
before.
As such, we would not want to fit on the test data because that's not how test data will be used.
OK, so although the count of exerciser has given us X training next test, you may be wondering what
do these variables represent?
Recall why we are doing this?
Our input data is text, but machine learning only works on numbers.
We have just converted our text into vectors of numbers.
Specifically, X train and X tests are matrices with number of rows equal to the number of data samples
and number of columns equal to the vocabulary size.
So let's just print out X train.
As you can see, ex train is not what you may have expected, or perhaps it is if you remember what
I told you earlier.
You may have expected to see an array of numbers since that's what ex train it is, but instead we see
something else.
We see that X train is a sparse matrix.
As you recall, the reason these special types of matrices are used is because most of the values in
the Matrix are zero.
That is, most documents do not make use of most words.
Using a sparse matrix representation is more efficient.
Also note the size of this matrix we have about six hundred rows and about 26000 columns.
Note that this is typically very undesirable in machine learning.
We normally like to have many more rows compared to columns, as you'll see.
This is not necessarily a detriment in this particular case.
Now to see just how sparse this matrix is.
We're going to compute the percentage of values in extreme, which are non-zero.
So let's break down this expression on the numerator.
We have the count of non-zero values.
So why does this give us that count?
Basically, you have to work from the inside out.
First, we have extranet, not equal zero.
So what does this give us?
This gives us a Boolean matrix with true, where the value is non-zero and false.
Otherwise, as you recall in Python.
True is treated as one and false is treated as zero.
Therefore, if we simply take the sum of all the elements in this matrix, we will get the sum of all
the non-zero values.
On the denominator, we have the product of extreme nut shape.
So why does that make sense?
Well, extreme nut shape will give us a tuple containing the number of rows and number of columns.
How many elements are inside its train?
Well, it's the number of rows times the number of columns.
In other words, the products.
OK, so I hope you're convinced that this expression is correct.
It's the number of non-zero elements divided by the total number of elements.
So as you can see, less than one percent of the Matrix contains non-zero values, thus we are justified
in using a sparse representation.
So in the next block of code, we'd like to do our usual machine learning steps.
Again, we start by creating an instance of our model, which is multi a.m. NIVEIS The second step is
to fit the model on the train sets.
The third step is to check the performance of the model on both the train and test sets.
As you recall, the score function returns the accuracy.
OK, so we get about ninety nine percent on the train, said about ninety seven percent on the test
set.
This is already pretty good, but let's see if any of the variations we discussed will be able to beat
this score.
So the next experiment in this script will be to use stop words, as you recall, by default, stop
words are not removed.
Note that this time we're going to do everything in a single block of code.
Since you've already seen this code before, so please double check each line for yourself to make sure
that this is the case.
OK, so as you can see, our performance is nearly the same as before.
Note that although we did a bit better this time, this was not always the case when I ran this code.
So please do your own experiments and check how consistent this result is.
The next experiment in this notebook will be to use the limited zation now because we're using the count
of exercise your class in Saikat Learn.
This is mainly an exercise in figuring out how to work with the API to use an external tokenize error.
We'll begin by defining a function that will map parts of speech tags in Moltke.
As you recall, this was described in a previous lecture.
The next block of code is used to define a class called dilemma tokenize error.
Basically, this is going to do all the work of tokenizing and limiting each document, although the
syntax is a bit complex.
Pay attention to the high level idea.
What do we want to do is create an object and then we want to be able to call that object as if it were
a function.
We can accomplish that by defining a special call function with two underscores on each side.
OK, so in the initial teaser, we start by instantiating a word net, lemme Taser object.
The next step is to look at the call function.
This essentially takes in one argument, which is the document to tokenize inside the function.
We start by calling the word tokenize function from Nulty K.
This will convert our documents into tokens.
As you recall, you can think of this like a fancier version of String Split.
The next step is to obtain the parts of speech tags, as you recall.
This can be done by calling an object post tag.
This returns a list containing tuples, and each tuple contains each word in the document, along with
its corresponding tag.
The final step is to look through each word and tag pair and to call the limitées function.
The output of this is a list containing each lemma ties the word in the input document.
The next step is to try a limited organizer with the count vector riser.
So how do we use it?
As you can see, we pass in an object of type lemma tokenize here for the tokenize their argument.
Note that this takes in any callable, so an object with a call function is acceptable.
A regular function would be fine as well.
In fact, you may want to rewrite this code to use a regular function if you're more comfortable with
that approach.
And again, the remaining steps are the same.
OK, so interestingly, while the train score is still about 99 percent, the test score has dropped
to about 96 something percent, which is less than before.
Note that you're encouraged to try running this yourself to observe that limitation is the slowest process
out of everything we tried in this notebook.
This video has been edited to make it look like it didn't take that much time, but please run this
yourself to see.
So the next experiment in the script is to try stemming, as you recall, stemming is a more crude version
of limitation.
Again, we're going to accomplish this by defining a class with a call function in the initial teaser.
We're going to instantiate a porter steamer inside the call function.
We're going to call, we're tokenize to tokenize are documents and then we call Porter Dot STEM for
each token.
Note that there is no need for parts of speech tags.
Again, the output will be a list of stemmed tokens.
The next step is to try to stem tokenize air with the count victimizer.
Note that the syntax is the same as before.
OK, so this time our train performance is slightly worse.
While our test performance is slightly better, but note that this is still worse than our first two
experiments, which did not use any special tokenize error.
The next step is to define the simplest possible token isare, which is just a string split note that
we need to put this into a function since, as you recall the tokenize, their argument must be callable
and it must accept a document as input.
In other words, this function has the same interface as the limited and str..
We defined above.
OK, so the next step is to try a simple tokenize here with the count of exerciser.
So this time our train score has increased, but our test score is only on par with what we got before.
At the same time, this is a sign that a simple string split is a reasonable choice.
OK, so what have we learned in this lecture?
We've learned that it's not at all clear which method will perform best before you even try.
Oftentimes, people assume that the most complex method will perform best.
In this case, that was limitation.
But what we saw was there.
This, in fact, led to the worst performance out of all the experiments we tried.
However, also note that this was just for a single instance of the experiment.
As a final exercise for this lecture, you should print out the size of X train in each case, what
you should find is that there is quite a large difference between the cases.
Consider why these differences exist using what you've learned about each method.