Hello and welcome to this art tutorial.
So in this tutorial we are going to apply PCA and actually I have prepared
you the required package to apply this first
dimensionality reduction technique principal Component Analysis.
So these packages are carrot which I think we already installed.
But if that's not the case then you can check it here in packages.
You can see if you have carrot available in this list of packages.
If you don't see it here, you can execute this line without the comment
and this will install carrot.
So that's the first package.
Then this is to actually import this carrot package.
So we'll execute that as well.
And we also need this other package that we installed
in part three classification the E10 71 package.
So normally you should have it installed.
But that's not the case.
You can select this line and install the package.
And don't forget to execute this line as well to select it.
And now we are ready to start applying PCA.
So the first thing that we're going to do is create a new variable
that we're going to call PCA that we will use afterwards
to transform our original data set composed of our 13 features,
into this new data set with the new extracted features.
So now to create this object we are going to use a function.
This is the pre process function.
Here it is from the carrot package.
And let's now press F1 here
to see all the info of this preprocess function.
Because you're going to see that you have some very useful parameters
that allow you to apply PCA according to your goals.
For example, you can specify the minimum ratio of explained variance
you want to get.
That means, for example,
if you want to reduce the dimensionality of your data set down
to a number of features, that will explain at least 60% of the variance,
well, you can specify this with one of the parameters of this preprocess function.
So let's have a look at the info.
That's the info.
And let's jump to the arguments.
So right the first argument is x a matrix or a data frame.
This is actually the data of which we want to reduce the dimensionality.
So this is going to be our training set.
So x will be the training set.
Then the next argument is method.
So method is your dimensionality reduction technique.
So as you can see you have several techniques of dimensionality
reduction PCA ICA.
So these are all the methods.
But of course the one that we want to use is PCA principal component analysis.
So we will use method equals PCA here.
Then thresh.
Thresh is a very important parameter.
That's what I've just told you.
If you want to reduce your dimensionality of your data set with at least
a minimum amount
of explained variance, well, you can do it by using the stress parameter.
And as you can see, it's a cut off the cumulative percent of variance
to be retained by PCA.
So for example,
if you want your new extracted features to explain at least 60% of the variance,
well you need to specify here thresh equals 0.6 60%.
But we're not going to use this thresh
parameter here because we already know what we want.
What we want is two independent variables, because we want to be able
to visualize the training set results and the test results,
and that we will be able to get with the next parameter PCA comp.
That is the specific number of PCA components to keep.
So that's exactly the number of extracted features you want to obtain in the end.
So here we will input PCA comp equals to so that our training set,
our original training set will go from having 13 independent variables.
The 13 original independent variables that we had in our data set to having
two new extracted features that will explain the most the variance.
And as you can see, if we specify the second parameter, this overrides thresh.
So that's why we don't need to specify the stress parameter to specify
a minimum cumulative percent of explained variance.
All right.
And then you have other parameters but we won't use them.
We actually only need our x to specify the data we want to transform
to extract the new features, the method PCA
and the number of extracted features we want to get.
Eventually that is two new extracted features.
So let's input the arguments.
Let's start with the first one x equals.
So that's training set.
Here we go.
And actually we need to specify the features.
And actually that's not the whole training set.
Because remember PCA is an unsupervised dimensionality reduction technique.
That means that we don't consider the dependent variable
to extract the new features.
So we actually need to remove here the dependent variable.
And remember this has index 14.
So the way we can do that is the same as we did for feature scaling.
That means we just add here -14.
All right. So now PCA will be applied
on all the features the 13 features of our training set.
Perfect.
Now next argument next argument is method.
And as we said method equals and quotes PCA.
All right then comma next argument and last argument.
And as we said that's PCA comp.
So PCA
and what we want is two new extracted features.
All right.
So that creates the PCA object
that we will then use on our training set
to transform our original training set composed of our 13 independent variables
to this new training set of reduced dimensionality.
And that will contain
the two new extracted features that will explain the most variance.
So let's do it.
Let's take our training set because we are going to call this
new training set training set as well.
Because you know then we have
all our templates and we use this training set variable name.
So we want to keep this training set name.
But of course if you want to keep your original training set and test set,
you can use other names like training set underscore PCA.
But then if you do that, don't forget to change training
set here by training set PCA and here test set PCA as well.
And the same for the confusion matrix section.
And especially here visualizing the training set results.
You will need to replace training set here by training set PCA.
All right. So that's why we are keeping the name.
It's in order not to have to change everything.
So let's go back to training set equals.
And now let's transform this original training set
into our new training set composed of our new extracted features.
And to do this it's very simple.
We use the predict function.
And inside we take our PCA object, come up,
and we apply this PCA transformation object
on the original training set that is named training set as well.
And so by doing
this, this original training set will become this
new training set composed of the two new extracted features.
So let's do it.
Let's start by creating this object.
And then we will transform our training set.
So I'm going to select this line and execute perfect.
The PCA object is ready to be used
on the original training set to transform it
into our new training set, composed of the two new extracted features.
So let's execute this as well. Here we go.
Our new training set is now created.
We can have a look.
As you can see when I'm clicking on this, well I have a new training
set composed of two new extracted features.
And remember these two new extracted features are called
the principal components.
So that's why you have PC1 and PC2.
And of course we still have our dependent variable vector,
the customer segment dependent variable with its
three labels one, two and three.
All right perfect.
But now as you can clearly notice, the dependent variable vector
just went in the first position.
And since then we're going to use a template on data sets
I mean the training set
and the test set where the dependent variable is in last position.
We will need to put this dependent variable in last position.
Here. And that's actually very easy.
What we only need to do is play with the indexes.
To put this customer segment dependent variable in last position.
So the method is really easy.
We're going to take our training set again.
Here we go.
And then equals.
And then we take again our training set then brackets.
And then inside these brackets we're going to take the indexes
of the columns of our training set in the correct order we want to get.
So you're going to understand that now we're going to take a vector.
So remember in our vector it's taken with C and then parenthesis.
All right.
And inside these parentheses we put the correct order of the indexes
we want to get.
So let's go back to our training set.
The first column we want to get is PC1.
That should be the first column of our new training set.
And this has index two.
So here we input the first index which is two.
Then comma.
And then we input the second index we want to get.
That is the second column.
And the second column is PC2.
And this has index three.
So here we input three and then here you input the index of the last column.
You want to have your training set.
And the last column
you want to have in your training set is this customer segment column.
Because that's the dependent variable.
And so far this customer segment has index one.
So you need to specify here this index that is one.
And by doing this our new training set here will be the same training set
that we have here but with a new order of the columns.
And that is given by this order here.
First, the first independent variable that has index two,
then the second independent variable that has index three,
and eventually the dependent variable column that has index one.
You're going to see if I select this line now and execute.
And if I go back to training set.
Now I have my first two columns as the new extracted features
x1 and x2, and the last column customer segment in last position
as our code templates that we're going to use is expecting it.
So that's perfect.
We can go back to PCA and now we need to do the same for the test set.
So what we're going to do is select these two lines, copy them
and just replace training set here by test set.
Same here test set and same here as well.
Test set and eventually test set.
All right.
And that's of course the same indexes for the order you want to have.
We can check it out I'm going to select this line.
As you can see so far the test set has its 13 original features.
Then if I execute this line it now has two new
extracted features the principal components one and two.
But the customer segment is in first position.
We want to put it in the last position.
And so to do this we execute this line.
And that will do it.
If I go back to test set now the customer segment is in this position
and we are ready to use the following parts of the template.
Predicted test results make the confusion matrix.
And eventually that's the most exciting part.
We will now be able to visualize the training set results,
because we now have two dimensions in our training set and test set.
So I look forward to visualizing these results in the next tutorial.
And until then, enjoy machine learning.