Okay, then.
Almost last step.
Now remember, this is just an object.
We haven't connected anything yet to our matrix of features.
So the next step is indeed to apply this input object on the matrix of features.
So how are we going to do that.
Well remember that a class contains an assemble of instructions
but also some operations and actions which you can apply
to other objects or variables.
And these are called methods.
You know they're like functions.
And one of them is exactly the fit method.
The fit method will exactly connect this input to the matrix of features.
In other words, what this fit method will do is it will
look at the missing values in, you know, the salary column.
And also it will compute the average of the salaries.
All right. So that that simply what it will do.
And then that will not be enough to do the and we want to do
the replacement will have to call another method called transform time
and which will this time apply the transformation meaning
it will replace the missing salary here by the average of the salaries.
Okay, so let's do this.
Let's first call the fit method.
In order to do this well of course we have to call first our object Imputer.
And then from this object you know adding a dot,
we will call the fit method
which has some parentheses because it's like a function inside a class.
And what does this function expect as arguments.
Well, it simply expects all the columns of X
with numerical values, but only the ones with numerical values.
Not the ones with text or strings categories.
And so. Well, how do we get this column?
Well first let's get
the matrix of features X because that's where we want to replace the missing data.
And from this matrix of features x.
Well first we're going to look at all the rows.
You know this fit
method will read the whole column that we specify inside this method.
But then for the columns here you know we could specify all the columns
where to look for some missing data.
However, this first column has danger.
You know it is a column with strings, and therefore this might cause
a warning or an error when looking for some missing data here.
Therefore, we're only going to specify these columns with only real numbers
age and salary.
And therefore here we're going to enter the range from one to be careful
not to because remember the upper bound of a range in Python is excluded.
So if we exclude two this will list through the salary.
Therefore we have to go up to three okay.
Up to three.
So that well this fit method
will look for all the missing values in the h column and the salary column.
So here we're specifying specific columns which are the h column in
the salary column.
And that's because we know that there is a missing salary.
And by the way there is also a missing H.
However, I recommend on a general rule to select all the numerical columns,
because in your career you will actually work with huge data sets
and you won't be able to see where the missing values are.
So just include all the numerical columns to make sure to replace any missing data.
Remember to exclude these ones the string columns okay,
so that's just something important to consider.
And then here we go.
This will connect our imputer to our matrix of features.
And now final step we have to call the transform method
once again from our Imputer object.
And so this transform method will exactly do that
replacement of the missing salary here by the mean of the salaries.
And same for the missing age.
It will be replaced by the mean of all the ages in the H column.
And so according to you, what do we have to input here?
Well, there is no trap here.
We of course have to input the columns of X where we want to replace missing data.
And so these are the h column and the salary column.
And therefore we simply have to input exactly
the same as what was input in the fit method.
So we just going to take this copy this and paste that inside the transform method.
However be
careful this transform method actually returns the new updated version
of the matrix of features
x with the two replacements of the missing salary and the missing H,
and therefore what we want to do now, and that's the last thing we have to do,
is to indeed update our matrix of features X, and to do this well, since
this exactly returns these two columns here, with that replacement done
well, what we want to do to update X is actually to take this,
you know, take the second and third column of X matrix features and
change it by what will be returned by this transform function
of the input object, so that the second and third columns of x
will be replaced with that average age and average salary.
And therefore the whole matrix of features X will be exactly the same.
But with these new average age and average salary.
Okay, we're going to check that right away.
Now to do so, we're going to create a new code cell
where we're going to print the new matrix of features X.
And let's see if indeed that missing value that we can clearly see
here as once again Nan is replaced in this new version of X.
So let's not forget to run these two cells here,
this first cell to indeed replace the missing data.
And now this cell to print the new matrix X.
And there you go.
As we can clearly see that missing salary in the previous matrix of features
X was indeed replaced by the average salaries of this column.
Okay. Check that if you want.
But there you go.
Now you have another tool in your data preprocessing toolkit.
So congratulations.
And now we're going to proceed to a new tool which is to encode categorical data.