‫Alright.
‫Alright, alright.
‫To finish off this proof of 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 importance.
‫It would be beautiful.
‫Now what is feature importance?
‫Well, 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,
‫and 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 keep all these other ones?
‫Cholesterol, falak x ng and if you want a breakdown of what they are, we can revisit our data dictionary
‫up the top.
‫We're looking at here a 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, we've had a tune hyper parameters of a certain model.
‫What you might want to look up.
‫And since we're using logistic regression to calculate all of these metrics, a logistic regression
‫is the model that we found by grid search, by tuning the high 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 endpoints using logistic regression model base feature important
‫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 cayenne 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 has gone.
‫Can you get the feature important to that model?
‫And we've gone, hold on.
‫I'm not entirely sure what the feature importance is.
‫So we've gone away for overnight or for a couple of hours, done our research, got our model and figured
‫out, okay, this is how we find feature and Portland's beautiful.
‫So that's what we're going to do here.
‫We've pretend 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.
‫We're 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 our logistic.
‫Regression model.
‫I'll put here a little note.
‫One way to find feature importance is to search for so you can put in here.
‫I'll put it in brackets.
‫Model name feature important.
‫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 out if you're curious to how to find feature importance with 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 params.
‫So gc log reg and best params.
‫We'll see what that is shifting into.
‫Wonderful.
‫And then we'll go see life equals logistic regression.
‫We're going to pass it a C value of this this long, long, long decimal.
‫Here we go.
‫And then we're going to pass it here.
‫Solver lib linear.
‫Beautiful.
‫So good.
‫Our cliff.
‫So we'll instantiate that.
‫That's our classifier and then we'll go CLF dot fit 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 kind of remember this for logistic regression is coefficient kind of reminds me of 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 cyclone specifically logistic regression and if we fit the model, we can call the co
‫f attribute which gives us the coefficient, right?
‫The value or how each parameter.
‫Let's look at our data set again.
‫How each of these independent variables so the X train data set the coefficient contributes to our labels
‫here or the target labels.
‫Whew.
‫So that was a bit of a mouthful.
‫But what we're going to 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 count them up, they're actually going to be the same length as what's happening here, these
‫amount of columns here, but let's just twist them together.
‫So we're going to match the features to columns of features to 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 going to zip together DF columns and
‫we're going to list along with self echo f.
‫Zero.
‫And let's say.
‫They did it.
‫Boom.
‫Look at that.
‫How good is that?
‫So if we go here.
‫We've mapped all of the different variables here.
‫To the right column.
‫Let's remind ourselves of what's happening here.
‫They have had.
‫So all we've done is we've taken the co f or a 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?
‫Wow, that's a bit of a mouthful.
‫Then another way.
‫Visualize feature importance.
‫Actually, I shouldn't.
‫Shouldn't have told you that.
‫I should have told you that was a 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 equals zero.
‫Front line feature def dot transpose.
‫A lot of transposing plots here, but that's all right.
‫Title equals Final Feature Importance.
‫Go legend.
‫No, thank you.
‫Whoa.
‫There we go.
‫So.
‫Does this make sense?
‫What is happening here?
‫So this, in essence, is how much each feature here contributes to predicting the target variable,
‫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.
‫It took me a while to 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 CO F 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've got CP equals 0.43, okay, for black equals 0.42.
‫And then we got what else do we have.
‫X ng equals -0.44.
‫Hmm.
‫If we go down here.
‫Now we've got the same thing.
‫So you've got CP equals 0.66.
‫Oh, it's a high number here.
‫And then we got fell.
‫Ach.
‫Okay.
‫Which is 0.02.
‫We've got x ang which is -0.6.
‫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 is 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.
‫Hmm.
‫If the value is negative we saw before with the correlation matrix, it 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.
‫So let's go sex.
‫Let's compare the target and what do we say?
‫So which means as a value for sex increases, the target value decreases because it's negative coefficient.
‫There's 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 3 to 1 ratio here.
‫So 72 divided by 24 is almost three.
‫We look at that 72 divided by.
‫Three.
‫Oh, exactly.
‫24.
‫So it's a 3 to 1 ratio here.
‫And then as sex increases, the target goes down to about a 1 to 2 ratio.
‫So there's roughly the 5050 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.
‫If we're using co f up here, the co f attribute.
‫Now let's have a look at a positive one, C, P or maybe slope because we've already explored CP before,
‫so let's do that.
‫PD 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, right?
‫We're trying to figure out what's going on using our the results from our model and seeing if what our
‫models learnt 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've got a value of 012.
‫So as it increases from zero 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.
‫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 we might do is copy this so we can come back down.
‫Copy that.
‫We'll come right back down to where we were.
‫Now, let's see if this makes sense, right?
‫We've got a positive correlation according to our model, a positive coefficient.
‫So I'll put here we'll change that to mark down.
‫That doesn't look very nice.
‫We'll put these as dot points.
‫Slope the slope of the peak exercise segment.
‫So zero is sloping.
‫So better heart rate with exercise uncommon.
‫Yeah, that is pretty uncommon.
‫Flat sloping, minimal change.
‫Typical healthy heart.
‫Okay.
‫Yeah.
‫And then to signs down slope.
‫And so signs of an unhealthy heart are okay.
‫That's making sense, right?
‫This correlation here, if you weren't a medical expert.
‫Right.
‫But you know a little bit about about how things go.
‫If you were to say someone had an unhealthy heart and you would ask someone, well, you know, that
‫they potentially have an unhealthy heart because their slope value is two.
‫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 of the target?
‫Because we're only predicting zero or one.
‫The higher value is one.
‫Wow.
‫Now 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 might be able to take to one of your one of your partners or 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?
‫Well, number, well 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.
‫So see here, these don't really influence these ones.
‫Here is age, cholesterol, stress.
‫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 of model.
‫This 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 than 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 got a confusion matrix.
‫So many different matrices.
‫Yeah.
‫My goodness.
‫We've got a rock curve.
‫We've got area under the curve.
‫Beautiful.
‫Hmm.
‫I wonder if there's.
‫I wonder if there's anything we're missing out.
‫Well, let's.
‫Let's figure that out in the next video.