So let's do this.
You can not actually really guess how we're going to do this.
So let's implement this solution together.
The first step is to create an object of the column transform a class.
And therefore we're going to create a new variable here which I'm going to call c t
which stands for you know the object of the column transform a class.
So city equals.
And now of course remember to create an instance or an object of a class.
You have to call the class itself.
So column transform or a class.
There we go.
Now since it is a class we have to add some parentheses
inside which we have to enter two arguments.
So these two arguments are first transformers where we will specify
what kind of transformation we want to do and on which indexes the columns
we want to transform.
And the second argument is remainder which will specify this.
We actually want to keep the columns
that won't be applied some transformations meaning age and salary.
So let's do this.
Let's first enter the names of these two arguments.
The first one is trend
for mirrors.
There we go. This one Transformers.
We're going to add a column here.
And then let's just add the next one.
And then we'll input them.
The next one is read main.
Do this one exactly.
And now let's input them.
So for the first one Transformers we actually have to specify three things.
First the kind of transformation which is encoding.
Second, what kind of encoding we want to do, which is one hot encoding.
And third, the indexes of the columns
we want to encode meaning country to country column.
So we have to actually input all that
in a pair of square brackets and then some parenthesis.
That's just the format expected by this Transformers argument.
We're actually going to enter here a tuple.
You know three elements inside these parenthesis.
The first element is, as we said, the kind of transformation.
And to specify that we want to do some encoding transformation,
we have to enter here in quotes and coder.
So that's the first element.
Then second element.
We simply have to enter
the exact name of the class that will proceed to this encoding.
So I'm going to copy this because this is going
to be exactly the second element expected here.
And I'm adding some parenthesis because that's a class.
And finally the third element inside
this tuple of parentheses is in a pair of new square brackets.
The indexes of the columns we want to apply one hot encoding.
You know we want to transform.
And so these indexes of course are only one index.
It is the index of the country column.
The country column is the first column of our matrix of features.
And remember that in Python indexes start at zero.
Therefore this country column has an zero.
And that's what we have to enter here.
Only zero.
All right.
So that's all good for the first argument here.
You know Transformers the Transformers argument is equal to all this.
You know in square brackets.
So great.
And now the second argument remainder.
So here we want to specify in quotes the following code name
which is base through and which is a code name that will say
that we indeed want to keep the columns that won't be applied, some transformation
that won't be one hot encoded, which are of course age and salary.
If we don't include this remainder equals path through here.
Then when we apply the transformation ex, we will only keep the,
you know, the first three columns resulting from one hot encoding.
And of course we want to keep age and salary into our matrix of features.
That's what it's used for okay.
So good. Now we have our city object.
It is of course not connected yet to our matrix features x.
And so that's exactly what we want to do.
But there is actually some good news this time.
We don't have to do it in two steps.
You know, by first calling a fit method from the object to connect our object
to the matrix of features X, and then apply as a second set
the transform method to apply the transformation.
No, this time we can do it at once because our column transformer class
actually has a method called Fit transform, which will do exactly
the process of fitting and transforming at once at the same time, you know?
So that's perfect. Let's use this.
And to use this well of course we have to call first our city object,
from which we're going to call this fit
transform method, which will get as input.
Well of course X because that's what we want to transform.
We want to transform the matrix of features x inside which 1 to 1.
How to encode the country column. Perfect.
So now two things to understand.
Well first, this fit transform method will of course return as output.
The new matrix of features
x with three columns, one hot encoding the country column.
And therefore, since that's exactly what we want to get
as the new matrix of features X, well we're just going to update
this new matrix of features x.
That's why I'm adding here x equals the result of the output of this fit
transform method.
But then we have to do one more thing,
which is related to the fact that the fit transform method
actually doesn't return the output as a numpy array,
and that is absolutely converter it to have x as a numpy array,
because this will be expected by the future machine
learning models, which we're going to build.
You know, in order to train the future machine learning models
where we're going to use a train function which is actually called fit.
And this train function will expect the matrix of features x as a numpy array.
So here we want to force
the output of this fit transform method to be a numpy array.
And to do this we simply need to call well numpy first, which has a shortcut
named np from which we're going to call this array function,
which will take as input exactly the output of the fit transform method.