We are going to be doing text
classification using Spacy word
embeddings in this video. I have taken a
news data set where the news is
classified as fake or real and this is a
CSV file I have. Someone wrote a new
saying top Trump surrogate brutally
stabs in in the back. Clearly it's a fake
news and it is classified such in this
CSV file.
Let's load that file into pandas data
frame. The data frame looks something
like this if you notice the shape of the
file
9900 records in total,
and I will do
value counts just to figure out if there
is a class imbalance or not. Looks like
none
almost similar uh samples we have if
these samples were very different let's
say you have
fake news is 5000 and real news is only
1000, then you have to do something to
address that class imbalance. I will now
convert this label column into numbers.
Obviously machine learning models
understand the
numbers better than the text so
from a label column I want to generate
new column called label num
which will be a simple number and you
can use map function for this purpose
where you can say if it is fake.
Let's say for fake you want to keep zero
number
and for real you want to keep it one
okay?
All right
so you get this label number column
created. Now you want to convert this
text column into the corresponding word
vectors. Now in the previous video we
already saw spacy's word vectors and we
learned that we need to load a large
model right? So I'm hoping you're
watching the videos in this playlist in
the sequence so that you have some
context. So in the last video we loaded
the large model that has word vectors
and now we want to convert
this text into a word vector and the way
you do that is let's say you have text
like this you put it
in a document object and that document
object
will have a vector. So this vector size
is 300 okay 300 element vector and I
want to do this
for every single row okay? So I'm
thinking maybe I can create a new column
in my pandas data frame which can store
vector for that particular text okay and
the way to do that would be similar to
what we did here right we created DF
text okay and
okay from DF text you want to convert to
a vector right? So what will you do so
you will say okay DF vector
is equal to df.text.apply
so you want to apply some function on
every element in this text column and
you know in pandas you can do that
easily using a Lambda function.
So Lambda x x will give you each
individual text, the whole news and then
that news you can convert it to NLP see we
can convert NLP in bracket text so this
will be the text.vector
so this should give you the vector
you can run it. Now the thing is
this is not a very fast operation. So
when I do control enter execute this
it's gonna take more than 15 minutes. So
I will do that and I'll be back after
few minutes. All right, so I'm back now
and you can see that
the vector column is created each
element is 300 element vector
and now we will use this particular
vector to train our model and whenever
we train our model the first thing we do
is we import train test split from sklearn
we have done this so many times so I
know I don't need to type it and I can
just copy paste and in the train test
split you will specify your x, so your X
is df.vector your Y is label number
okay and your test size let's say I want
only 20 percent sample to be in my test
category and then
I I like to specify random state that
way I run this multiple time I see some
predictability okay,
and you can
specify stratify but I think it's okay,
and in the result you will get this
okay, now when you look at x train
let's see
actually if I do df.vector that's
not good because I want to get the
values in it
so let me do values here.
All right so if you look at the shape
total there are 7900 element okay which
is fine and X test has 1980 80 20% split.
But if you look at the actual array
it is a numpy array but the every single
individual element is another numpy
array,
when we give this to our classifier for
the training it is expecting 2D numpy
array so we need to convert this array
from this format to a 2D numpy array and
how do you do that. Well in numpy there
is a function called
stack. So let's say numpy Stack function
and if you look at that stack function
see
let's say you have an array let me see
if I I can find an example here see here
each individual array is a numpy array
itself. But when you do h np.stack
it will convert it into native 2D numpy
array okay? If you don't know about numpy
you can follow my tutorial so you'll get
an idea. So here
uh let's see where were we yes I am
going to
now
import numpy so import
numpy as np
np.stack
and I will call it
maybe x train as you do the same
thing for X test also
okay?
So X test test.
Now check this,
see this is there proper 2D numpy array
if you looked at our previous x train it
was an array and every single element in
there array was another numpy array and
that kind of structure doesn't work well
with your sklearn training okay. Now we
are ready to import
our sklearn
uh classifier. So I will just import sklearn
uh classifier I'll use multinomial naive base
model first because that's the most
popular one for
NLP type of problem.
It tends to perform well
and then on that you can do fit
okay
and then y train,
now see this array has negative elements
so most likely I will get a problem
and the reason for the problem is that
negative values are not acceptable in
multinomial NB.
Hmm what do you do now? So you have
positive negative values in your
training dataset now you want to tackle
this problem of not having negative
values.
So if you know about
scaling so scaling can help you
convert those values into positive range.
So you can do maybe a min max scalar I
will import a min max scalar here okay,
and then create an object of this scalar
class and this is how you do it,
scalar and then on scalar you will do
fit transform right fit transform. If you
have followed my machine learning videos
previously you will get an idea,
and this 2D I want it to be
passed as an argument and I will call
this an
scaled train embed and I'll do the same
thing for the test embed okay
and here you don't need to do fit
transform, you can just say transform.
All right so now I will pass scaled uh
train dataset as an input into
classification model looks like training
worked and it trained it so freaking fast.
Okay now I will print a classification
report so usually whenever I train a
model you know it's a general practice
not just me everyone tries to evaluate
the performance on the test dataset and
the way you do that is you will do clf
.predict on X test 2D
and that
you get it as actually not x test 2D
I'm thinking this one yes
and that is your y prediction
okay and then you can just in in the
classification report you supply y
truth
and y prediction
and you use print because that way it
can print it in a nice format.
Look at the result, rock solid right. More
than 90 percent precision, recall, F1
score. So you see how easy is it to train
a text classification model using the
ready-made word embeddings that come
with the Spacy. We have done the similar
thing uh by using TF-IDF and bag of
n-grams and so on in our previous videos,
but this was little easier because
you got the embedding ready made out of
the box.
Now let me try one more classifier which
is
KNN
K nearest neighbor okay and I will
create
let's say neighbors is five. If you don't
know about KNN I have a separate video
on my YouTube channel so just in YouTube
search Codeasics KNN classifier
you'll get an idea. So I'm just gonna go
ahead and just to save time just
straight away train it okay train and
print classification model.
I'm using my most powerful tool copy
paste.
Oh my goodness! This is unbelievable!
Look at the precision, recall almost
perfect 99 percent.
This can't get better than this because
especially with KNN classifier I don't
know if you noticed but in the previous
videos when we are using TF-IDF and bag
of n-grams, etc. KNN would perform
horribly
because it has an issue with higher
dimension and with TF-IDF bag of words
our dimensions are huge.
Here it's just 300 Dimension Vector okay?
So that could be a conclusion that
um KNN really works well with a with a
dense representation of your text. All
right? So that's all I had. Just to
summarize we use spacy's word embedding,
train KNN and multinomial Naive based
models. KNN performed really well but
multinomial was also pretty good more
than 94 4 percent is pretty good A
Precision recall and even F1 score.
I'm gonna be posting the exercises later.
So
whenever you check my videos make sure
in the video description you look for
the exercises. Sometimes I post a video
and then post exercise after few days
all right, and if you feel like
contributing back please give me a pull
request. I hope you are having fun
learning NLP if you like this playlist
by the way give it a thumbs up share it
with your friends and Linkedin is an
amazing platform. So you can write a post
on LinkedIn uh talking about this
particular playlist if you find it
useful. If you don't find it useful don't
bother. All right, thank you!
[Music]