hello welcome back in this video we'll see how we can use features that we have
extracted before to train a classification model so we'll use this model to detect different fall
conditions in our flow pack machine so we will use the model to see whether we have
failures and electrical side or maybe on the mechanical side of the plant or maybe
both so let's see how we can do this in MATLAB and how we can use these features
so to do this we will open a different live script
let's go here and let's make this a bit bigger okay so the first thing that we do now is loading
the table that we have built before and you will recognize the same table that we had before so we
have a few features here we have a large number of columns each column for each feature and then
we also have a full code column so we use this column to describe six different fold conditions
that we can have in our flow pack machine so we can have mechanical failures we can have
electrical failures we can have a combination of both with different levels of severity
so let's go now and let's normalize the data so once the data is normalized we still have
to deal with them a large number of features so in particular we have more than 30 features so
what we want to do now is actually reduce the number of features that we will work with and
this is very useful if we need to finally deploy the algorithm on an embedded processor we want
to make sure that the model is not too big so what we do here we evaluate the correlation
matrix and then we can take a look at how the features are correlated to each other so with
one we plot features which are correlated and the correlation is higher than 80% so thanks to
this information we just remove some features and then after we remove features we want to see which
ones of these features that are left actually do a good job at separating the six different
fault conditions that we have simulated in the flock back machine so to do this we will use the
nca so the neighbour component analysis and this analysis will give us a weight for each feature
and the weight essentially represents how well the feature can separate the different classes
so in this plot you can see on the x-axis the feature index and then on the y-axis the
weight so in summary the higher the way to the better the feature so we need to
select features which allows us to separate six different fault conditions well and to
do this for example we can choose to select only the features that I have weight higher
than one and this basically means that we are still and further reducing the number
of features that we'll work with and we collect this in a table and the table is
called feature data training and as you can see these feature data training has only ten columns
so now we can use this table to train a much learning model so we use the classification
learner app and from this app what we can do we can import the table that contains both
the predictors which are features and the label which is the class that we need to distinguish so
we get this table here we can see that we have more than two thousand rows which is number of
simulations but only ten columns and then we can here choose how we want to validate
our model so in this case we choose to keep 80% of the data for training and 20% for validation
okay so now from the classification learner app you can see how the features that we have
extracted before actually do a good job at clustering and separating different classes
so the classes that we have are represented here with different colors and here just by
switching the predictor you can see how this effects the clustering so here the
aim is actually really to just quickly see if you can individuate any possible combination
of features that will actually separate the class as well and for example here with these
two features we seem to have a good separation between the classes and you know thanks to this
information you can further reduce the number of features that you will use for training so
for example in this case we decide to keep the parents of the position error
and then the maximum value of the current and the kurtosis for the speed of the motor
so we could also do PCA principle component analysis but we will skip this for now okay
so from here we have a large number of models that we can use for training here really you know you
can either choose one of these models if you have knowledge of your system or if you have already
hint on which one could be good or maybe you know you could just go on and train all the models
and you know with the parallel computing option enabled you can train multiple models at the
same time and here you can see that you have an accuracy result which basically is telling us
how well the algorithm is distinguishing the different faults so now we can stop some of
this and we can take a look at the SVM so the SVM in this case is an accuracy of around 93%
but you know let's see what this mean exactly so we can take a look at the confusion matrix
and the confusion matrix is a good measure of understanding how well or badly our algorithm
is performing so with this confusion matrix what we see is predicted classes on the x-axis
and then the true classes on the y-axis a good model will have high values on the main diagonal
and then low values on the of terminal elements but you know for example here we see a 23% so
this basically means that when the true class was gearbox warning we were actually saying
that the class was okay and only 75% of the times we're predicting this in a correct way
so you know maybe we want to try and fix this so we can use the miss classification costs
from the classification learner app and you know what we can do from here we can penalize
some of these errors okay so once we do this we can retrain the model but it's very important
to keep in mind that all we're doing here is just shifting and distributing the errors in
the matrix and indeed what you see here is that now we have an error in a different situation
when the class was okay we were actually predicting it as a gearbox warning seventy-two
percent of the times and maybe that's okay because here now we're being a bit
more conservative so this is just to say that you can use the image classification costs to
optimize the way that your algorithm works but you can further optimize your algorithm
by performing hyper parameter optimization so for instance you can choose an optimizable SVM model
then you can select a few options so for example here you can decide what parameters
you want to optimize and which ones you want to get fixed and then you can also choose the
optimizer options so if you want to use a bayesian optimization a grid search random
search and so on and then you can also specify the training time limits so for example in this
case I'm setting the time for optimization to 60 seconds okay so once we set this we
can carry on training and what we're doing now we are actually watching how the optimization
goes on and this is a very powerful way to perform hyper parameter optimization because
it can be done directly from the classification learning app without writing any MATLAB code
so once we're done we can see how Ben algorithm has improved so now we have
an accuracy of eighty-nine percent compared to eighty-four percent in the previous case
and then in particular we can take a look at the confusion matrix so as you can see now the error
has decreased to 52 percent compared to 72 percent in the previous case and also you can see that the
classification accuracy for the gearbox warning is still pretty high okay so at this point we
just need to use this model that we have trained in the classification learning app and we have a
couple of ways to do so so the first way is to export the model in the MATLAB workspace so we
can give it a name and click OK or maybe we can actually generate a MATLAB function directly from
the app and this contains all the necessary lines of code to replicate what we did interactively and
this is very useful if we want to automate the process next time so now let's take a look at
the model that we have exported and this is what we will use in the next video to first
test our classification the desktop and then we will see how we can generate code from this and
deploy it on the PLC so if you want to have more details stay tuned and I'll see you next time