All right all right all right.
To finish off this prove a concept that we're working through of predicting whether or not someone has
heart disease.
We're going to fulfill the last requirement that our boss asked of us and that is feature importance
so let's make a little heading here feature important and beautiful.
Now what is feature important will feature importance let's just put a little note to ourselves feature
importance is another way of asking which features contributed most to the outcomes of the model.
You could also extend it.
And how did they contribute.
This is useful to know since we're trying to predict heart disease using a patient's medical characteristics
which characteristics for a refresher.
We've seen this a few times now but we'll refresh ourselves.
We're using this data here so which one of these characteristics of age sex C.P. all these other ones
cholesterol black eggs sang.
And if you want to a breakdown of what they are we can revisit our data dictionary up the top.
We're looking at here will feature important which one of these contributes.
And how do they contribute or how much do they contribute to predicting the target.
So how would you exactly find it out.
Well finding feature importance is different for each machine learning model now as we saw before with
how to tune height parameters of a certain model.
What you might want to look at and since we're using logistic regression to calculate all of these metrics
and logistic regression is the model that we found by grid search by tuning the hard parameters to get
the best results so far.
What you might look up is something like this how to find feature important using logistic regression
if we go here I'll look into feature importance using logistic regression model based feature importance
how to find the importance of features of a logistic regression model.
If you went through this you could insert almost any model here.
So random forest or cane and classifiers or something like that and you'd find a bunch of different
methods.
So what we're going to do again is pretend that we've done our research.
Our boss is gone can you get the feature importance of that model we've gone.
Hold on.
I'm not entirely sure what feature importance is.
So we've gone away for overnight or for a couple of hours done our research got our model figured out
Okay this is how we find feature importance beautiful.
So that's what we're going to do here.
We've retained we've done this research but remember part of being a machine learning engineer part
of being a data scientist.
The most important thing is researching and experimenting.
It's built into our framework that we're using experiments.
That's what we're doing here where we're in the modeling phase really that we've created the heading
for it but really we're experimenting.
So let's figure it out let's find the feature important for now logistic regression model I'll put here
a little note one line to find feature important is to search for so you can put it in here I'll put
it in brackets
model name feature importance.
That's one way to do it.
That's what we just saw up in this little search here.
That's what you should try a few curious to have a fun feature importance with there but whichever model
that you're using.
So first of all we're going to fit an instance of logistic regression will create one with the best
parameter GC log rig based programs.
We'll see what that is.
Shifting into wonderful and then we'll go see a laugh equals logistic regression.
We're going to posit a C value of this this long long long decimal.
Here we go.
And then we're gonna pass it here solve solver live linear beautiful so go to our CSF so we'll instantiate
that that's our classifier and then we'll go see a left off it X train y train.
Wonderful and now through our research we've found that there's an attribute to our fitted logistic
regression model called co F which stands for coefficient.
Okay.
And the way I can remember this for logistic regression is coefficient kind of reminds me I may have
correlation but let's see what happens
remember we found this through our research we've looked up how to find the feature importance for a
logistic regression model and it's told us if we're using psychic learn specifically logistic regression
and if we fit the model we can call the CO f attribute which gives us the coefficient the value or how
each parameter.
Let's look at our dataset again
how each of these independent variables.
So the X train data set the coefficient contributes to our labels here or the target labels.
So that was a bit of a mouthful but what we're gonna do is manipulate this coefficient array so that
it looks like it makes sense because right now it's just a just a list of numbers.
But if you can add them up they're actually going to be the same length as what's happening here.
These Amana columns.
But let's just twist them together.
So what are we going to match the features to columns Coates of features two columns.
So this is going to make sense.
That's what I'm trying to get at.
This will make it make sense dict so feature dict we're going to create a dictionary and we're gonna
zip together def columns and we're going to list along with C a pro EF 0 and let's say
featured in Boehm.
Look at that how good is that.
So if go here.
We've met all of the different variables here to the right column.
Let's remind ourselves of what's happening here.
Go ahead.
So all we've done is we've taken the call where for Ray right which is an attribute of our classifier
and we've taken the columns from our data frame and we've mapped them to each other.
So what this is telling us is how much and what why.
See we've got some negative values here.
Whether it's a negative or positive correlation how much each of these contribute to predicting the
target variable.
Well that's a bit of a mouthful then.
Another way
visualize phage are important actually.
I shouldn't have told you that I should have told you.
That was our little magic trick that I had magician never reveals his secrets.
What we're doing is we're visualizing this so we can kind of get an idea of what's going on.
So index it zero.
Front line feature ADF don't transpose a lot of transposing plots here.
But that's a long title Eagles final feature important.
Go legend.
No thank you well.
There we go.
So does this make sense.
Well what is happening here.
So this in essence is how much each feature here contributes to predicting the target very well whether
someone has heart disease or not.
You can see that some are negative and some are positive and now you might be thinking we have seen
something similar to this right.
Whether one feature is negative and one feature is positive.
It's not coming to you right now it's okay.
Took me a while to connect these two but if we go right back up here right back up to our correlation
matrix.
So this is where I remember for logistic regression if we want to figure out feature importance we use
coed because correlation matrix coefficient kind of kind of the same thing kind of not really but you
get my picture so we can see here some different values.
So if we got C P equals zero point four three.
Okay foul AK equals zero point for two.
Then we got.
What else do we have.
Exam equals negative point four four.
Mm hmm.
If we go down here
now we've got the same thing.
So got P.
Because point six six is a high number here and then we've got foul AK okay which is zero point 0 2
we've got X Ang which is negative zero point six.
So now what we're doing is we're doing model driven exploratory data analysis.
These values have come from building a machine learning model which has found patterns in the data and
it's telling us how it contributes right or how it correlates to our target variable.
That's what we're looking at here.
So what can we do with these values.
Well let's explore them a little bit and just see if they make sense.
So we can see that sex is fairly negatively correlated like it's almost right here at the bottom.
Mm hmm.
If the value is negative we saw before with the correlation matrix that means that there's a negative
correlation.
Okay.
When the value for sex increases the target value decreases because of the negative coefficient.
Let's look at this see if this actually reflects the data.
Let's go sex let's compare the F target.
What do we say.
So which means as a value for sex increases the target value decrease because negative coefficient has
a value for sex increases so as it goes up the target value decreases.
That doesn't really make sense when we look at this look.
So sex goes up increases but so does the target value.
Oh you know what.
After looking at this you can see that the ratio is what we're thinking about here.
So as sex goes up the target value the ratio decreases so you can see here if the sex is zero for female
there's almost a three to one ratio here.
So seventy two divided by 24 is almost three.
Look at that.
Seventy two divided by right.
Exactly.
Twenty four so it's a three to one ratio here.
And then as sex increases the target goes down to about a one to 2 ratio C there's roughly a 50/50 here.
Okay so that's a negative correlation which means that sex is strongly negatively or a negative coefficient
sorry because we're using the coefficient co f we're using co f up here the CO f attribute.
Now let's have a look at a positive one.
C P Well maybe slope because we've already explored c p before.
So let's do that paid a cross tab.
I'm not actually sure what slope is so we might have to revisit our data dictionary and again this is
model driven exploratory data analysis here on it.
We're trying to figure out what's going on using all the results from our model and seeing if what our
models learned holds water.
So it's saying here that as slope increases because it's a positive coefficient that means that the
target should also increase as well.
So let's have a look.
So slope we're going to value 0 1 2.
So as it increases from 0 to 1 to 2 does the number of samples here increase.
Well I think it does because here we've got slightly more here we've got slightly more there or we've
got almost double there.
But then as it gets really high we've got basically triple.
People with heart disease.
So let's go back up to the top and see what slope is in our data dictionary
slope.
What you wanna do is copy this so we can come back down.
Copy that will come right back down to where we were.
Now let's see if this makes sense right.
Got a positive correlation according to our model of positive coefficient.
So I'll put here we'll change that to markdown.
That doesn't look very nice we'll put these as dot point slope the slope of the peak exercise s t segment
so zero is unloading.
So better heart rate with exercise uncommon.
Yeah that that is pretty uncommon.
Flat sloping minimal change typical healthy heart.
Okay.
Yeah.
And then two signs downslope and so signs of an unhealthy heart are okay.
That's making sense right.
This correlation here.
You want a medical expert.
But you know a little bit about about how things go if you say someone had an unhealthy heart you would
are someone while you know that they potentially have an unhealthy heart because their slope value is
too would you say they're more likely to have heart disease.
Okay.
Having a value of one or not have heart disease well according to our model it's giving it a positive
coefficient to slope.
So as the slope value increases it means the model is more likely to predict a higher value of the target.
And what's our higher value other target.
Because we're only predicting zero or one the higher value is one.
I know we could keep going with this but this is something that you could probably share.
What is the importance of having something like this.
First of all you find out more.
So if some of the correlations in feature importance is here confusing a subject matter expert may be
able to shed some light on the situation so this is something you can might be out to take to one of
your one of your partners one of your colleagues and go Hey I'm not sure what's actually going on here
with chest pain having a positive coefficient are you able to help me out a number or two you could
redirect your efforts.
So if some of these features offer more value than others this may change how you collect data for different
problems to see here.
These don't really influence these ones yes age cholesterol dressed.
These are all really low coefficients here.
Now that might influence how you you go about collecting data in the future maybe finding someone's
cholesterol level is really hard to do if it's not contributing much to the patterns a model is finding
you might scrap that in future data collection and then again this is the third point it's less but
better right.
So if some features are offering far more values and others you could reduce the number of features
your model tries to find patterns in as well as improving these ones so that's what we just continuation
really point to and three are kind of the same thing.
You could combine these in some way or improve these or just make the ones that are offering the most
better.
This would not only save you on computation by having a model find patterns across less features you
could still achieve the same performance with using the features that offer the most value.
So that's something to keep in mind.
All right so we've fulfilled all of the requirements in our project that our boss was asking for we
got feature importance we got cross validation classification metrics yes we've got a confusion matrix
many different pages here.
My goodness we've got a ROIC curve we've got area under the curve.
Beautiful.
I wonder if there's wonder if there's anything we're missing out.
Well let's let's figure that out in the next video.