Hello everyone and welcome to the tents or phyllo project exercise solutions lecture.
Let's go ahead and jump to the exercise notebook and walk through the solutions.
OK here I am at the project notebook.
I'm going to go ahead and get started.
First thing I wanted to do is actually get the data fleer pro by now doing this but I'll go ahead and
say import pandas PD and then we'll just use PD thought read see a city and then passen the bank note
C is free file.
You just use tab to autocomplete if you're in the same folder.
And I'm going to set the sequel to data.
It's going to check the head of the state of frame should look something like this will say data head
and it looks like we have some image variance the skew kurtosis and then the entropy as well as what
class belong to.
And the class indicates whether or not it was authentic.
So we'll go ahead and do a little bit of exploratory data analysis.
We'll just do a few quick plots of the data going to import seaborne as S.A..
And since I'm using that you put a notebook I'll use map plot lib in line and we'll go out and create
a counterplot of the classes.
Authentic versus fake.
Now Seaborn makes that really easy to essentially just Asinus thumb count plot and I need to pass in
my X and since I want my X to be class I'm going to just passen in this class and then I need to tell
seaborne what's my data source.
We'll go out and say data.
And there we have it.
Then we can create a pair plot of the data of seaborne and I'm going to that the hue to be equal to
class again Seaborn makes this really easy we just say per plot passen the whole data frame and say
whew hew.
Equal to the class column.
Run that and the status isn't too big.
So it doesn't take that long to run but it does take a little bit of time.
And there we can see and just basically by looking at this we can tell that there is pretty good separation
of the classes especially here we can see on image skewness versus image variance.
There's pretty good separation.
And those are pretty good separation image kurtosis versus skewness.
There is some overlap here looks like on entropy versus kurtosis.
There seems to be heavy overlap but we should expect to get pretty good results just visually you can
see almost immediately that on certain features it's very separable.
OK.
Let's go ahead and prepare our data now.
When using a neural network in deep learning based systems it's usually a good idea to standardize your
data.
This step isn't actually necessary for a particular day to get good results without it.
But I want to run through it just for practice.
I know we didn't do it previously but let's go ahead and just go for it.
We want to import standard scaler from sikat learn.
So we'll say from S-K learn dot pre-processing import standards scaler and hopefully or kind of remember
these steps from the nearest neighbors algorithm's section then we'll go ahead and create a standard
scalar object called scaler.
So we'll say scaler is equal to standard scaler close princes.
And then we're going to fit scaler to the features so I will say scaler fits and then data thought drop
and I'm going to say drop the class along x is equal to 1 and that allows me to just fit the actual
features.
So now my scaler has been fit and I can do now a fit transform.
So when we use the dot transform method to transform the features to a scaled version so I'm going to
say scaled features is equal to that scalar object thought fit.
It's underscore transform and and pasan the features of again data that drop.
And then I'm going to say class along access is equal to 1.
Run that and then I'm going to convert the skilled features to a data frame and check the head of the
state of France to make sure Skilling worked.
So let's go and say PD data frame passen my scaled features and then I want the actual columns of the
state of frame to be equal to data the columns everything.
But the last column because remember the last column was CLOS and all set this equal to the underscore
feet.
And then let's go ahead and CDF underscore feet's head run that and it looks like everything's been
scaled so you can compare these numbers versus if you scroll back up here to these numbers.
Now as far as the actual values for variance skewness keratosis and entropy they all seem to be around
the same order of magnitude.
So standardizing the variables wasn't a huge deal to do here but a lot of times it's a really good step
and you should pretty much always do it for neural networks.
So let's go ahead now that we have our standardized data we'll do a train test split.
I'm going to go and import that from the sikat learn.
But before I do that I need to create two objects X and Y which are the scaled feature variables and
labels respectively so I will say x is equal to the three since we just did that.
And then why is equal to data class.
Let me make sure it's a capital C there are rights we have are X and Y and I need to use the dot as
underscore matrix method on x and y and reset them equal to this result.
And we need to do this in order for tensor float to accept the data in a diary form and sort of append
the series.
Basically tent's tensor flow can only deal with numerical data in the form of an array.
So it's not going to be all or it's going to get confused if we just pass it and data frames or series
.
That's a little too high of an traction.
So what we can do is say x is equal to x as Matrix and we'll say Y is equal to y as matrix.
And there you have it and then we can use sikat learned to create the training and testing sets of the
data as we've done in previous lectures.
So that's just going to be two steps here.
Say from S.K. learn cross-validation in poor train test split run that and then I'm going to say X train
.
Actually what I'm going to do is you're saying train to split shift tab scroll down to see the example
just to save us some typing and grabbed this guy here and I will go ahead and paste it in and then we
will delete that line and delete these lines we'll go and just use point 3 and the default random states
actually find here.
Not a big deal since our data isn't huge and we'll go ahead and split that up into the training and
testing set.
And now we can use tensor flow Orkneys the contrib learnt method isn't so much easier to use in the
actual session.
So we need to import tensor flow and it's kind of a long line.
So I would expect you to have this memorized or just reference the notes.
You can say tensor flow the contrib loops and contrib da.
Learn Python learn and we'll import that is learn.
OK.
And then we're going to create an object called a classifier which is a deep neural network classifier
from the learn we'll set it to have two classes and a 10 2010 hidden unit layer structure.
So I will say classifier is equal to learn dot DNN classifier.
I'll sit my hidden units WIPs hidden under score units to be equal to 10 20 and 10 and you can mess
around with that later see how it affects your results.
And then the number of classes I need to actually specify there has two classes expected.
So there we go.
OK.
Now I have that classifier that basically an estimator object.
And now it can fit that classifier to the training data.
We're going to go ahead and use steps as equal to two hundred if a batch size of 20.
And again you can play around these values if you want and go ahead and ignore any warnings you get
here.
They shouldn't affect your actual output going to say fire dot fits in a fit it to my training data
.
So X train and y train.
And then the parameters are going to pass is take 200 steps with a batch size of 20 or run that.
Now this may take a while especially if you added more hidden units or more steps or larger or smaller
batch sizes but just keep that in mind larger the data the sort of fit can take a while.
OK so now it's time to actually evaluate this model and see how we did.
So we'll go ahead and use the prediction method from the class for our models create predictions from
the X test.
So I will say notes predictions since these are bank notes and call classifier say predict on X test
we'll run that.
And now we're going to create a classification report a confusion matrix and I'm going to ask you.
Does anything stand out to you and hopefully you noticed it.
I went over it a little bit in the previous lecture Bowl saying S-K learn that metrics import will do
a classification report and a confusion matrix run that and learn to print the confusion matrix for
our results so why underscore test.
And the No predictions and you'll probably notice that you did extremely well.
Now keep in mind due to the random split you may not get the exact same results.
I do sometimes I get a one over here and other times I've even gotten 7 misclassified labels.
But for right now I got lucky of my random split and it looks like I classified it perfectly so that's
something that should have stood out to you if you happen to get a similar split to me.
And if we go ahead and print the classification report on this we can say why underscore a test and
then versus no predictions run that and you notice everything that 100 percent so something like this
should immediately raise suspicion for you.
It's very rare for a model to do such a good classification job unless the data is highly separable
which we kind of got a hint scrolling back up here when we did this visualization we got a hint that
it's pretty separable but deep neural networks can be surprisingly good.
So you'll always kind of want to do a quick reality check which is why we have this optional comparison
here.
So as I mentioned you should notice extremely accurate results from that deep neural network model which
is I mean kind of the whole point and why this is the latest technology here.
So we're going to go out and compare to a random force classifier just for a reality check.
Make sure we didn't miss anything up.
So far a random force classifier also gets 100 percent than we know.
Maybe something strange.
The data we're going to go ahead to just run through the steps create a random force classifier and
compare the confusion matrix and the classification of port to the deep neural network model.
So I will go insane from S-K learn thoughts ensemble in poor random forest classifier and I didn't explain
every single step here.
Hopefully remember this from the random force class for a lecture but we'll go ahead and just say RAFC
is in the first class for and also at the number of estimators to something reasonable like 200.
If you do shift tab here to get number of estimators 10 by default that's probably too little so we'll
go on to just say 200 and then I'm going actually fit this.
They'll say R.F. see fit and we'll passen X train and white train run that let it fit should be pretty
fast and then we'll go ahead and do our predictions so we'll say R F C threads is equal to RAFC predict
X underscore test.
OK and then I'm going to go ahead and do the classification reports hault print the classification report
with why underscore a test and my RAFC predictions will run that.
And then we do get extremely accurate results we're looking at ninety nine percent precision recalling
accuracy across the board.
When you actually check the confusion matrix to see just how many we accidentally mislabelled.
So why test our spreads.
You'll notice it's pretty similar.
Three in One.
So a total of four.
So it should have also done really well not quite as good as the DNN model depending on how many hidden
layers you do.
So hopefully seen the power of the deep neural network.
But I would encourage you to do is come back up here and play around with the hidden units and the steps
of this deep neural network classifier.
As I mentioned before I got a perfect fit here on my test data.
You may or may not get a perfect fit.
And there's two reasons for that.
One is that your split is random so you may not get the exact same split I do the pending on whether
or not you pass in a random state variable there.
But more importantly remember back to the theory lecture that weights and biases are randomly initialized
so that could also affect your deep neural network.
Now if you do more steps and add more hidden units that effect goes down and eventually you'll get more
and more accurate to the training data up to a certain limit there is diminishing returns there.
So you don't want to make just a huge deep neural network especially for such a small data set.
But okay that's it.
I hope you enjoyed the project and I hope you really got to see just how much power you have using tensor
flow and with the convenience of the contrib learn library you can call this just like you would if
a normal sikat learn model.
OK thanks Ron and I'll see you at the next lecture