Hello everyone and welcome to the natural language processing project solutions lecture in this lecture
.
We're going to begin coding out the solutions for the LP project.
Let's go ahead and jump to Jupiter notebook to get started.
All right.
Here I am at the project notebook and I'm going to go ahead and jump right in.
First thing you have to do is import and I just said the usual suspects.
So hopefully by now you're always importing pandas PD and umpires and T and then for visualization libraries
you can import those later on for the EDK section.
Or you can just import them now you can say import map plot live pipe plot as PLDT and then we'll go
ahead and import seaborne as Asinus and set map plot lib in line.
All right then we want to read the yelp that c a c file and set it to a data frame called Yelp.
Or go ahead and do that.
We'll say Yelp PD that read underscore CXXVIII.
And the trick here you can just start typing in y e.
Click.
Tab autocomplete.
And if it's in the same directory this will go ahead and find it for you.
Let's go in and check out the head in thone describe methods on this Yelp data frame.
First off to checking out the head.
Or go ahead and typed head equals.
Or two as the argument so I only see two rows.
Looks like we have a business side the a some to a string value the dates of the review the review ID
and then we have the amount of stars that was rated the text of the id the type.
Whatever this row is in this case it's a review the user id and then how many people said it was cool
and people said it was useful and now people said it was a funny review go in and check out the info
for this.
Can Say your info.
And we have 10000 entries so this is a good amount of data to work with and take in mind that it's actually
not using that much of our memory it's it's a small file but as far as the number of entries we do have
a substantial amount to work with and then we can check out as well the described method.
So you can say yelp that the scribe.
You'll see something that looks like this.
And the only numerical columns here are the stars.
Cool useful and funny columns.
Now we want to do it just like we did in the lecture.
We want to create a new column called Tech slang which is the number of words in the text column.
We'll go ahead and do that using apply.
So I'll just say Yelp text length is equal to Yelp text and then I'm going to just apply Eliane the
builtin length function.
Now we want to do some exploratory data analysis.
You can import the data visualization libraries you want done so already we actually already did that
.
But one thing I wanted to do is set the style for seaborne and I'll just have it be white.
But you can go ahead and not set style.
Doesn't really matter.
Next thing you wanted to use the Fassett grid from the seaborne library to create a grid of five histograms
of text length based off of the star ratings and one of the references Seaborn documentation for hints
on this if you need them but hopefully you feel comfortable using Fassett grid.
It's kind of a two step process.
First you have to actually create the Fassett grid by calling Asinus dot Fassett grid.
And I'm going to set it equal to this variable called G pass in my dataset and then I want to say that
columns are going to be equal to stars.
And if we look at this example output we can see stars 1 through 5 is the column for each of the histograms
.
OK.
And then we can go ahead and map PLDT that hissed and we'll go ahead and just do this with test X lengths
.
All right.
And there we have the actual histograms and you can continue adding arguments here such as Ben Zyxel
to 20.
So you wanted to get a little more detail as we have so many rows it's going to set sensical to 50 and
now we can really see some detail here.
It looks like the distribution of text lengths seems to be the same for all the number of stars.
It looks like the actual amount of text reviews seems to be skewed a lot higher towards four or 5 stars
.
So that may cause some issues as we go along throughout this process.
Let's go ahead and create it.
Box plots of text length for each star category.
Again see more and makes this quite easy.
We can just say s box plot passen x is equal to stars and Y is equal to text links or data source it's
just that Yelp data frame.
Let's go ahead and run this in.
If you want you can change the palette the palette I used in the solutions was Rainbow.
Just because I think it's looks a little nicer.
But again this is totally optional.
And here we have the box plot and just based off of the look at this box plop.
Who knows if text length is actually going to be that strong of a feature.
It appears that one to two star reviews have higher Text lengths but there's so many outliers shown
by these dots outside of the bars of the box plot that it may not be a useful feature.
Let's go ahead and create a count plot of the number of occurrences for each type of star rating.
And this is going to basically reflect what we saw in the histogram earlier in order to do this we can
just go ahead and say Asinus stot counts plot Vasant X is the Stars column.
My data source is that your data frame and I'm going to go ahead and set the palette again to Rainbow
So it looks a little nicer.
And here we have that count plods and like we saw in the history ramp we go ahead and jump up.
We saw that we have higher amounts of foreign Five-Star views.
And this is reflected in the counterplot as well.
We're going to go ahead and do is use group by to get the mean values of the numerical columns and you
should be able to create the state of frame with the operation.
So here's the data frame we're trying to create and it's actually pretty straightforward.
So I'm going to just say a new data frame called stars say Yelp group by.
And I just want to group by stars and then call mean as the aggregate function.
I will go ahead and just display stars to show it and that's how you can create using group by and then
an aggregate function the mean values of these numeric columns.
Let's go ahead and use the core method on the group by data frame to produce this data frame.
So that's actually quite simple.
So you can just say stars c o r r call the correlation method on that.
And I can see how correlated the actual columns are to each other.
So cool as a correlate is so useful and funny versus text length et cetera.
Now we can use Seaborn to actually create a heat map based off of that.
So again that's quite a simple step.
You say Asinus heat map assin stars c o r r r c map.
You can go ahead and just call this cool warm if you want or you can just use the default.
And I'm going to set a..
Equals to true.
So we can actually see the numerical values.
All right.
So looking at this it looks like funny is highly correlated to the useful value as well as useful being
correlated to text length.
And obviously we see a perfect correlation along the angle which makes sense because teks length is
perfectly correlated with itself.
It looks like we have some negative correlation between cool and teks link funny and useful.
That's an interesting no.
But you'll probably need more business domain knowledge of how people actually use the cool useful or
funny review features in Yelp actually get some sort of intuition or sense of what that actually means
so we'll go ahead and just leave it at that for now and move along to the end LP classification task
.
So make things a little easier go ahead and only grab reviews that were either one star or five star
.
We'll just go ahead and drop 2 3 and 4.
So created a frame called Yelp class that contains the columns of the updated frame but only for the
one or 5 star reviews.
So I want to create a data frame called Yelp underscore class and set that equal to yelp.
And then I want to filter out by two conditions where Yelp stars as equal to one or with the pipe operator
.
Remember you can't use OE are here otherwise so get an ambiguous truth series and they'll say yep stars
is equal to 5.
And if we check out your class info you'll notice you get now about less than half of the columns.
Thousand eighty 86.
OK moving along.
Want to create two objects.
X and Y X will be the text column of the class and y will be the stars column of the yellow class and
these are just our features in our target variables.
That means I set X equal to Yelp class the TX column and then I said.
Y equal to Yelp underscore class.
The stars column.
And I'm going to import count of vectorize or and create a count vectorize or objects.
And remember I say from S-K learn feature extraction text imports count vectorize her and then to make
an object call for the count vectorize her object just going to call it C-v and instantiate it just
like we would for any other model.
Then I'm going to use the transform method on the counts vectorize or object and pasan X that's the
text column and I'm going to save this result by overwriting X which basically just means I need to
do this in the X equal to C-v thought that underscore a transform X itself.
OK.
Now we're going to go ahead and do a split our data into training and testing data.
So you can use train test split to split data into X train x test Y train Y test and then use test size
equal 0.3 in random state 101 going to go ahead and import from cross-validation as learned that cross-validation
train test split run that I'll start typing train test split just using tab autocomplete here shift
tab expand the documentation and grab the useful example line.
So I don't have to type this all out.
I'll go ahead and grab this and then replace it.
Here I'll fix my test size to be point 3 in random state to be 1 0 1.
Let's go ahead and run this.
Make sure it works.
Looks like that.
And now it's time to train a model.
We're going to import multinomial and B and create an instance of the estimator and call it and be safe
from as learn naive Beys import multinomial and B and Set an instance of it to be multinomial and B
and I'm going to fit and be using that training data.
And B does that X train white train.
So I have my multinomial Navy base model trained.
And it's time to see how our model that predictions and evaluations will go ahead and use the Predict
method off of this and B object that they just train to predict labels from X test which means I'll
make a variable called predictions and I'll just say and B predict X underscore a test and we'll go
ahead and create a confusion matrix and classification report using these predictions and why test which
means from the S-K learn metrics all import of confusion matrix and classification report run that and
I'll go ahead and prints the confusion matrix pass in my test data compare it to the predictions.
I'll go ahead and print a newline and then I'll do the same thing.
But now if a classification report.
I'm just using tab autocomplete here.
Why test and then predictions.
They can run all that.
Now we see our confusion matrix and our classification report.
Not too bad.
We're looking at around 90 percent accuracy precision and recall between the 1 star and five star class
just totally based on the text data.
So that's pretty cool.
And let's go in and see what happens if we try to include T.F. ID after this process using a pipeline
.
And it may not be what you expect.
We'll go ahead and import the IDF T.F. IDF transformer from sikat learn.
And that's also under Eskdale learn feature extraction text will import t f IDF transformer will import
pipeline from sikat learned.
That's from Eski learned pipeline imports pipeline.
And now we will create a pipeline with the following steps.
Counter-act riser CFIT transformer and multinomial knife base north to do that we'll just go ahead and
set an object or call it pipe we call a pipeline.
And then you pasan a list of tuples or remember that tuples have two elements in it.
The name of the process and that's totally up to you what you call it and then the actual model you're
going to pass it through.
So we'll start off of a count vector Isar.
The next thing I'll put in is the T.F. idea if you remember the vector riser that turns the strings
that token integer counts are just are essentially a bag of words.
Next we're going to do is T.F. idea of transformer close parentheses.
This is the integer counts two weighted T.F. IDF scores.
And then finally if we go ahead and put in one more what we will do is train on the T.F. idea vectors
of NE's base.
So I'm going to say make sure my currencies are matched up here.
Sissons one more closing princes this tuples.
Good.
Now I need one more open one here.
OK.
So what would call this the model or classifier or whatever you want to call it.
Multinomial and be close parentheses.
And let's make sure that Ramm correctly.
I believe I need extra comma here and actually have unbalanced parentheses let me fix that and run this
.
OK great.
Looks like our pipes are working correctly now.
Keep in mind these princes are kind of hard to balance sometimes specially since you need close ones
on the model so just pick your full attention to that.
Now let's go ahead and use this pipeline.
Remember this pipeline has all your pre-process steps in it already.
Meaning we need to respect the original data.
Remember that we overwrote X as a count vectorize version.
And what we actually need is just the text.
So I'm going to go ahead and redo that train test split on the yellow class object you know going to
go ahead and set X again to help class text y to Yelp class stars.
Ok then we need to do the train test.
I'm going to go ahead and just grab it from where we did it earlier.
And that was up here and there they're trained to split needed to control a control C or command a Comanche
depending if you're on a Mac come back down here.
And then to have control of the or command we go in and run that.
Make sure it works.
Looks like we're good and that we will fit the pipeline to the training data.
And remember that you can't use the same training data as last time because that data has already been
vectorize which is why we need to passen just to text and labels which is what we did here in this step
.
Just want to make that clear for you.
Well go ahead and say pipe it to X train and y train run that.
And it went ahead and fit it.
It did a count vector Isar and then it went ahead and did the CDI T.F. IDF and the multinomial model
.
Now let's go ahead to do predictions and evaluations.
We're going to use the pipeline to predict from the X test and create a classification report and confusion
matrix snugness same.
Well we'll go ahead just overwrite predictions say predictions is equal to pipe not predicts on X test
whoops X test an s.
There we go.
And let's go ahead and then we can actually just copy and paste what we have up here.
Go in and copy that control see the here and we'll get an interesting result when we run this.
So notice what happened when we actually included the T.F. IDF.
It looks like CFIT have actually made things worse.
So that's actually where we're going to end this project because I want you to actually on your own
See if you can explore it.
We did a pretty good job just using the bag of words model and this is pretty acceptable depending on
what the application is.
But a 90 percent accuracy and precision and recall on just text data is it looks pretty good.
It's not exactly state of the art but it's pretty good given how easy it was just to run a couple of
lines of code.
But what I wanted to do is actually explore see if you can do some feature engineering play go back
play around the pipeline steps and see if maybe creating a custom analyzer like we did in the lecture
helps.
And a quick note.
It probably actually won't.
But go ahead and explore that or maybe recreate the pipeline or just the count vectorize or a base.
I also wanted to check out and explore this changing the actual machine learning classification model
at the end to maybe another classifier actually help at all.
OK.
Great job.
Hope you enjoy the project.
Again there's a lot more you can dive into from natural language processing.
So I really encourage you to check out the resources shown in the Jupiter note book.
Feel free to post any questions to the Q&A forum.
Thanks everyone and I'll see you at the next lecture