‫Now I know at the end of the last video, we said we'd make sure all the data was filled up, like there
‫were no missing values.
‫But after reviewing it, I think it makes a little bit more sense to make sure it's all numerical first.
‫So let's make a little heading.
‫We'll call this 1.1.
‫Make sure it's all numerical.
‫Wonderful.
‫And now, to begin with, we kind of need another data set.
‫Now, why is that?
‫Well, because our heart disease data set.
‫This one here is already all numerical.
‫So.
‫That being said, let's import another data set, which is our trusty car sales data.
‫Now, if you remember this right at the start of kind of revealed a little bit here, but this one only
‫has ten rows.
‫Now, what I've done is I've gone and manufactured a little extended version of this, which has the
‫same headings make color, odometer, doors and price.
‫But this time we've got a thousand rows.
‫I'm not going to scroll through them because that would take too long.
‫You're just going to have to trust me on that anyway.
‫I can prove it.
‫Car sales.
‫Let's import it paid.
‫Don't read CSB.
‫Remember, I've moved all of our CSV files into a data file which is kind of best practice.
‫Keep your working directory tidy.
‫Car sales extended dot csv.
‫Wonderful.
‫Then we're going to have a look at it.
‫Car sales dot head.
‫Wonderful.
‫And then we'll figure out how many there are a thousand.
‫Beautiful.
‫Told you so.
‫And then we'll look at the data types make is an object, color is an object a dominar doors and price
‫are all integers.
‫And now these are objects because they're strings and they are categories, whereas these are numerical
‫values.
‫Now what we have to do because this section is make sure it's all numerical, is before we can run a
‫machine learning model, we have to convert these to numbers.
‫But just to prove it, let's try anyway and see what happens.
‫So first of all, what we're going to do is split the data into X and Y.
‫So we'll use these four columns, make color odometer doors, we're not price but these for to try and
‫predict the price of a car.
‫Now have a thing to yourself.
‫Does this actually make sense?
‫Could you do that in real life?
‫Do we expect our machine learning model to do well on this kind of problem if you were given only the
‫make so Honda the color a white Honda with 35,431 kilometers on the odometer and four doors, could
‫you predict the price was $15,323?
‫Maybe.
‫Maybe not, but you probably need a little bit more information.
‫The same probably goes with our machine learning model, but nonetheless, let's try and make one.
‫So split into x, y, which is we want the feature matrix, so we're going to go x equals car sales
‫drop.
‫Now we don't want the price column.
‫Oh what have I done actually.
‫Press shift enter.
‫I'm getting very trigger happy in this noble price.
‫Access equals one and we're going to do y equals car sales.
‫Now this is going to be just the price column because we want to use the X to predict the Y.
‫And then we'll also split into.
‫Training and test set split into training.
‫And test.
‫So the x train x test y train y test equals we saw this before train test split.
‫We'll pass it x, we'll pass it y and we'll pass it test size equals.
‫We use 20% of the rows for the test data set.
‫Wonderful.
‫Well, let's try to build a machine learning model that will learn on the training data.
‫So the X train and the Y train and then predict on the test data.
‫All right.
‫Build a machine learning model.
‫Now, this time, we're going to use still a random forest.
‫But I want you to guess what this random forest can be used for.
‫So we're going from cyclone ensemble import, random forest regression.
‫And if you remember, back in our workflow, we were predicting a classification problem.
‫Now, this random forest regress is the same as a classifier random forest, but this time it can predict
‫a number, which is what we're trying to do.
‫We're trying to predict the price of a car, given some attributes about it.
‫So we're going to import a regression model and we're going to set it up this time.
‫Instead of calling it self, we'll call it model, regress, beautiful, and then we'll go model fit.
‫We want to train it on the training data so it's going to learn the patterns, the relationships between
‫the X variables and the price, the label, and then we're going to score it evaluated on the test data.
‫Why test now?
‫How beautiful is that?
‫This is the power of scale learn right?
‫We can train, create a model trainer and run it.
‫Evaluate it in four lines of code.
‫We could really do it in less, but four lines of code is pretty -- good to build a full blown machine
‫learning model.
‫So let's do it.
‫Oh, no.
‫We got an error.
‫What's happened?
‫Value error.
‫We kind of expected this, though, right?
‫Value error could not convert string to float Nissan.
‫Now we see what's happened is that now machine learning model can't deal with strings.
‫So as we kind of knew, we have to convert these into numbers.
‫So let's see how we do that with SRC learn.
‫Now we're going to kind of go through this little chunk of turning the categories, making color into
‫numbers with SRC loan and it might seem like a bit to take in to begin with, but we'll ride it out,
‫we'll see the code in action and then we'll go back through it and see what it's actually doing.
‫So from SRC, learn dot preprocessing src learn has a fair few modules that that each do a a number
‫of different things.
‫One hot encoder.
‫Oh what's that.
‫We haven't seen that before.
‫We'll put a little comment on here what we're going to do turn the categories into numbers.
‫Wonderful.
‫From SRC learn dot compose.
‫Import column transform.
‫I wonder what that does.
‫You can probably kind of guess what column transformer does, but we'll see it in action first.
‫Then we're going to define our categorical features.
‫So in our case, our categorical features are make color.
‫And a tricky one is doors.
‫Now you might be thinking, why?
‫Doors?
‫Let's go back up.
‫Have a look at it.
‫Doors for five, four, four, three.
‫Hmm.
‫Why is this?
‫Well, let's see it.
‫Car sales.
‫Doors don't value counts.
‫It's safe to assume that doors is numerical.
‫Right.
‫Because we've got four, five and three as a values.
‫But it's also categorical because you could go cars with four doors fit into this category.
‫Cars with five doors fit into this category.
‫So the 79 of them and cars with three doors fit into this category.
‫So there's 65 of those.
‫So we're going to, in our case, treat doors as categorical.
‫And I'm just continually spamming commands.
‫So the notebook saves.
‫It's like I've ingrained it into habit, just like pressing shift and enter to run itself.
‫So we've got three categorical features, beautiful.
‫And then we're going to make this little variable called one hot.
‫We're going to take an advantage of the one hot encoder class that we've imported.
‫So we'll instantiate that there and we're going to make a little variable called transformer, and then
‫this one is going to use the column transformer.
‫This is going to accept a list of tuples with a name.
‫So in this case, we'll call it one hot and then we'll pass the actual transformer that we want to use,
‫which is the one hot variable that we've just instantiated.
‫And we're going to format this to make sure it's nice spacing and we're going to pass it the list of
‫features we'd like to transform.
‫And then finally, we don't need that.
‫Let's bring that back up.
‫I've got stuff all over the place here.
‫Bring that back up.
‫Wonderful.
‫And then here, we're going to pass one final variable or parameter to column transformer called reminder
‫equals pass through.
‫Beautiful.
‫And then we're going to have a little variable here.
‫We hold transformed X, which is basically going to be the version of our X data.
‫Let's have a look at what X is basically transformed x we don't want all of those is going to be.
‫The version of this except converted into numbers.
‫So let's see that.
‫So we've got our transformer here.
‫And this is where we're going to go.
‫Transform, fit, transform on X, and then we're going to have a look at transformed X.
‫Well, we got some invalid syntax.
‫We've got a comma here.
‫Name error transform is not defined classic.
‫Transformer.
‫There we go.
‫Wonderful.
‫So this is spitting out an array.
‫Maybe if we put this into a data frame, that would make sense.
‫So let's go PD dot dataframe see if we can actually transformed x.
‫Wonderful.
‫So you might be looking at this going, What the hell is this?
‫But if we bring back X, what we're going to see.
‫Dot head work in a bit zoomed in.
‫So we're going to have to kind of keep bouncing up and down a little.
‫But what has happened is that because we've told second line that, hey, make color and doors are categories,
‫what this code here has done has transformed it specifically one hot and coded it.
‫So now we've converted the make color and doors columns into one hot and coded variables.
‫But the odometer column so which is here you can see the numbers are actually the same still.
‫So 3543 one.
‫Yep.
‫192714.
‫Yep.
‫Correct, correct.
‫So the odometer column hasn't been changed.
‫So if we look back up here.
‫The reason why make color and doors have been changed is because we define them here.
‫We instantiated a one hot encoder.
‫We'll see what that is in a moment.
‫Then we've created a transformer using column transformer.
‫So basically this is saying, hey, column transformer, take the one hot encoder and apply it to the
‫categorical features.
‫And for the remainder of the columns that you find pass through, don't do anything to those.
‫And then what we've done is created, transformed X and fit our transformer.
‫So fit transform to our X data.
‫Now that's a kind of a big mouthful there, and you might be thinking, what does one hot encoding even
‫mean?
‫Well, let's have a look.
‫We'll go to our keynote.
‫So this is one hot encoding in a nutshell, if we have four cars.
‫So cars zero one, two, three, and that colors are red, green, blue and red.
‫If we want it to one hot, encode this.
‫So basically turn the categories into numbers.
‫It would look something like this.
‫This is what one hot encoding does.
‫We've got car zero and because the color of car zero is red, this little arrow here could be the code
‫that we've used to transform our data frame.
‫So to transform our categories into numbers.
‫So this is one hot encoding.
‫So because zero, the color is red, it gets a one and it gets a zero for the other two categories and
‫the same for Green.
‫But this time because it's green, it gets a zero for red and a one for green and a zero for blue.
‫Blue exactly the same.
‫Zero for red, zero for green and a one for blue because it's a blue car.
‫And then finally, because car three is also red, it has a one just like car zero and there's zero
‫and zero for green and blue.
‫Now, that's what we've done here, except not only with the color column.
‫We've done it with the Make and the Doors column.
‫So that's why we have 012 all the way up to 11 features with zeros and ones.
‫Now what we've done is we've encoded the different parameters of each sample, so Honda, White and
‫a number of doors into zeros or ones.
‫So now the beautiful thing is our data.
‫Is all numerical.
‫So what can we do?
‫Well, we should be able to fit a model.
‫But just in case that wasn't clear.
‫We've got one more way that we can do this.
‫And now.
‫So that we can go dummy's equals.
‫There's a function in pandas called Get Dummies, which is kind of like one hot encoding.
‫But I'm not sure.
‫I'm actually not sure why it's called dummies.
‫I just know that the function is get dummies.
‫So if we go dummies, PD don't get dummies and we want to transform car sales.
‫We pass a list of the columns that we want to transform or turn into dummies.
‫So here, doors.
‫Now let's have a look at dummies.
‫Here we go.
‫So I think because doors.
‫Dawes is numerical.
‫It hasn't worked on here.
‫But this is this is what we can see what's happening.
‫So we've got zero.
‫Mike.
‫BMW is zero.
‫So it's a Honda.
‫It's got zero for Nissan, zero for Toyota, and it's got zeros for black, blue, green, red and white.
‫So if we went through this, all it's done is turn the make and the colour into zeros and ones.
‫Beautiful.
‫So now that our data is all in zeros and ones, let's try and refit the model.
‫So we want to go.
‫Nope random don't seed when we got 42 just so our results are reproducible.
‫We're going to use X transformed up here.
‫So this one here instead of X.
‫So let's do that x train.
‫We'll set up a new new training and test data equals train test split.
‫We're going to pass it transformed X and Y can stay the same because Y is already numerical.
‫And then we're going to have tests.
‫Size equals 0.2.
‫Excellent.
‫And then we can go model fit, x train.
‫Y train.
‫Beautiful.
‫It worked.
‫And now let's evaluate it.
‫Model score.
‫X test.
‫Y test.
‫Wonderful.
‫Now it worked, right?
‫So because we use transformed X and because up here we did this little bit of code here to to transform
‫our data from being categorical into numerical.
‫And previously our model could not convert floats to or strings to floats for Nissan.
‫So when we used our first X variable up here.
‫Our data was still in categorical form, so still using strings to represent a sample.
‫What happened?
‫It error it out.
‫Got a value error.
‫But now right down here when we run the same code model fit x train, y, train and score, we don't
‫get any errors y because our data is all numerical.
‫Now, as for this score here, if we imagine the best score you can get is 1.0.
‫The model probably hasn't done it as well as it could possibly do.
‫But remember we talked about that if you were to look at this data here.
‫The Exciter.
‫And you were trying to trying to use this to predict the car's price.
‫It will probably be pretty hard.
‫So maybe the model has found some patterns, but they just weren't that great because there wasn't too
‫much information about each sample.
‫But that's not the point.
‫We can look into evaluation metrics later.
‫The point here is that we've converted our model, our data story from being non numerical to completely
‫numerical, and that has allowed us to fit a machine learning model on it.
‫Now we've covered that.
‫We've converted all our data to numbers.
‫What happens if there was missing values?
‫Let's check it out in the next video.