so hello and welcome back to the Python course and we are now here in
the very penultimate part of this part here e-vehicle powertrain
modeling and I just picked out a paper from me where we together with
colleagues from an e-vehicle model tried to model the powertrain in
the form of a black box model that means we only have signals from the
CAN bus taken and processed a bit and based on these signals then
tried to model the entire powertrain and also the driving resistance
parameters and so on from such a vehicle and the idea was that you can
use map data for the prediction and such planned speed profiles and
with that you can then basically predict how big the electric power is
in the future that is needed to drive a certain distance and if you
just sum it up, you get to the energy that is needed and yes, we
basically published that here in the paper with different methods, you
can also take a look at it a little deeper, but here now the whole
thing as a mini-project with Seikit-Lörn at the end to model it
exactly and with such a task it takes a while until you actually get
to the actual machine learning, because at the very beginning you have
to take a closer look at the question of what you actually want to
model and yes, we first started to take in a lot of background
knowledge, we first looked at what simple models are that describe the
vehicle dynamics in the time-dependent driving resistance force that
you know from the vehicle technology lecture that is summarized here
in the first formula that is such a possible model with which you can
model it and that was just to compare which input sizes actually
determine this force and what are such parameters and rather constants
so here in the equation it is more about that you look at what is
actually changeable, what you can actually capture with sensors and
then later can also predict for the model and yes, what you are so
constant that in the model should ultimately be determined by such a
regression by a fit yes and the equation you should actually know, I
think from the vehicle technology lecture, so it consists of four
summands in this case, the first one is the acceleration force then
there is a simple approach for the rolling resistance of the tires
then there is an increase force that is recorded here and then again
the aerodynamic resistance that occurs there and if you look at the
same thing then you see there are always inputs or signals that
appear, for example the acceleration of the point or then also the
speed back here to the square, so these are interesting signals that
you can use for model formation and there are of course also quite a
few constants such as this cross-sectional area or also the cw value
of the vehicle, which changes a bit slowly, we can then leave that to
the modeling and try to fit with the model and exactly so if you look
at such equations and then want to model the context that ultimately
describes the performance that is here the little p then you can come
to such a black box form that is shown here in the picture so what we
basically did here in the mini tutorial in the mini project and also
in the paper then made a model that is very briefly written so the
performance should depend on the time and that should be modeled with
a function and this function should then be approximated by various
machine learning methods and that has the speed as an input, the time
-changing speed for a certain given route and the longitudinal
acceleration, exactly and the longitudinal acceleration there you have
to know that there are such effects as, for example, the increase
already taken into account in it that is marked down here via the
formula that would only be for increase and decline but if they have a
lot of lateral dynamics then they also have a certain influence in it
so the recordings that we have made here with the test vehicle that
also include such increase effects in the acceleration signal, so that
you have to imagine that these sensors are of course fixed in the
vehicle chassis and then have a fixed alignment and the vehicle that
or the vehicle coordinate system that changes of course depending on
how the road incline or slope is and also how the curve radii are in
relation to the world coordinate system, so there is always a certain
influence on this acceleration signal exactly and yes in the end we
did the following we then did test drives with such an electric
vehicle and recorded many knowledge signals at the beginning, of
course, you always collect a lot and then do the model modeling so
iteratively and in the end we came to this very simple model that we
basically describe the performance as a function of speed and
acceleration, as I said, such increase effects are also included in
the acceleration and that we now take a closer look at what kind of
measurement data was ultimately recorded
the data records that I now have here in the repository also made
available that we recorded at the time with 100 hertz in principle the
Kenbus signals and then finally added the brake pressure from the
mechanical brakes from the disc brakes and then we did a pre
-processing we used a so-called salvizky-goulet filter with a certain
window size to smooth the signals a bit and then sampled the
individual signals a bit, so you don't need that so finely in this
special modeling task and what we then did is now remove the
conditions where the mechanical brake was used from the data set that
is basically in the model what we just saw in front of us so this p is
equal to f of v and longitudinal acceleration ax, of course,
mechanical braking is not modeled in it that is now here in front of
you, of course, you can also consider whether you want to take
something like that into the modeling exactly so that was the step in
principle from the recording of the data and a bit of pre-processing
and then I have now made the files available here in the repository
that are now to be found in the repository from three measurement
paths there are simply with dat1, dat2, dat3 named and we will now
simply read that here in the next one in the next slide you can see a
small fix that was necessary so that it also works on the travis that
is just a bit of the path to correct that is not so important the
interesting part is that it is for matlab files so if you have matlab
files there is this scipy-io-loadmat function and you can very
conveniently read matlab files in python so that is here again this
short fix that just means that the local laptop as well as the remote
laptop of the remote pc in travis then find the files that is not so
important for you now and here the line that is in principle then such
a one-liner with which you can then matlab files in python can read
yes and then let's take a look at what is actually in this matlab file
so one of the three we have now read in and there we just take a look
at the fields, the names that are in there and the dimensions what
that actually is and you can do that with the .keys with the function
and there we see now that is basically a dictionary that is ejected
from this scipy-io-loadmat function and there are six fields in there
that is a big a, an awal, b, bwal, k and m and of course you have to
know what that means, I would explain that to you now and before we
look at the sizes, so if you now plot the shapes here in principle or
show them with print commands then you can see that the shapes of this
a and b that differ in principle in the second dimension so the number
of lines that are 2204 that are in principle always the number of
measuring points and the second dimension that are the crevices that
is then the dimension of the channels or sensor measurements and in
essence it is so that b that is always this measurement that is so
this electrical power the power of our model so the left side of the
model equation and here in this a there is then the speed and the
acceleration in it so on the right side of the model equation exactly
and then we have an awal and bwal here again that is simply smaller
than the number of measurements that is split off and here this k and
m that are then again yes ultimately the size the number of
measurements here exactly and now there is still a bit of a small
problem with this recording that are a bit older and at that time it
was configured so that you basically had from this a so from these
input data you split off the last 200 but still received the input
data completely that means ultimately the awal is exactly the same
from the numerical values ​​as the last 200 entries from the a and
then you have to be a little careful now that you don't compare that
with training and test data and these are just typical problems that
occur with such a data recording you usually have to look at the data
very carefully before you actually start any modeling where do they
come from and what do they mean and who created them and what kind of
pre -processing has already been done on it, you still have to filter
them or they are already filtered so you need a lot of knowledge about
the data so that you can use them quite typically that's why I don't
have this here in the mini project at all removed and and in super
cleanly prepared but that will now be the first the first small task
is that you basically take the a and the b so the input and output
data and split it up again in training and test data exactly so what
we want to have is in principle such a similar structure as it is also
with scikit-learn so x-train x-test and then y-train and y-test and we
do that, as I said, from the big a and the big b as it is now in the
matlab files yes and that was already the first small task that has
nothing to do with modeling at all, but it's about reading data,
dividing the data correctly, assigning it correctly and then giving it
back with the correct name, so in principle you should write a
function that loads these matlab files, so a single matlab file would
be enough for the time being, in principle, hand over the filename
that should be loaded and then x-train x-test y-train y-test should be
issued and the split-up in training and in the test area should
basically take place via an argument and the argument should simply
control the sample number of test data and yes, we make it very, very
easy, we just split it up with a cut in principle, so we don't
randomly throw the data through each other, that's time series, that
means you can't just mix the data all over again and assign new ones,
then the whole connection of the dynamics is a bit lost, but we just
do it so that we ultimately read in the vectors a or the matrices a
and b and then we do this at a certain speed and then basically cut up
the b and yes that would be the first task, try it out, see if you can
do it as a function and I will show you a possible solution for it
right away.
The solution that can be found here in a file solution load
measurements and I'll go to PyCharm
for a moment and we can take a look at it
and I programmed it here so that this function findMath is in it
again, just that it is really copy and paste from what we saw earlier
in the fooling and down here is where it actually starts, so from this
line, first you look at how long the vectors are, the matrices are,
how many lines they have and that is done here, so I called it small
m, the length and then I just read from this dot a the shape, the
first element of the shape and then it is divided up, so we have the x
-train here, then we basically take the zero element from the a to the
last element minus this argument that is passed over minus this test
size and then they have basically cut off a strip of the data, so to
speak, we do the same with b, so here we use the b and then have the
training data for the output, yes and with the test data it is very
similar, we basically start from the sample of this test size and then
go to the end and you can do that in numpy with such a minus, so it is
basically counted from the back, from the right end, so to speak, to
the left, back to the corresponding sample and then to the end, yes
and the return is also quite simple, everything is simply returned as
a tuple, so x-train, y-train, x-test and y-test, that would be the
solution to split it up and then let's see what it looks like, you can
then import it here in the notebook by simply importing all functions
from the file with the line, so from the solution load measurement.py
import star and with the star you basically import everything that is
in there, it is now just a function and then you can just call it like
that, here I have now set a test size of 400, that means the last 400
data points we will use in this case for testing our model later.
Yes and then we basically get such a structure again, which is very,
very similar to the tasks that there is also such a scikit-learn, so x
-train, y-train, x-test and y-test.
Yes, now let's try again whether that worked, so whether we snipped it
up correctly, so to speak, these matrices and of course we can compare
that with this original data, so if we basically compare this data a
again or this data 1b and that is shown here , so let's take a look at
the first five entries from the x-test and if we then compare that
with the minus 400 to minus 395 element, then the same comes out
numerically and that is also what we ultimately wanted to achieve
there.
Yes, you can do the same for the test data for the output, for the y,
you ultimately have to select the big B here .
Yes, so you have now seen that in principle everything is just data
back and forth, that actually has nothing to do with modeling, but
actually also consumes a large part of the time in practice and of
course it is also clear that you always have to be very, very careful
with such work steps, because if you make a mistake there, then of
course the models would also model a completely different thing than
you actually intended to do and there can also be many mistakes, so
that's actually a pretty critical and time-consuming step and yes, I
hope now with the little exercise you have got a little feeling for
it, so it's pretty important that you know a little bit about numpy
and all this editing and so on and yes, then you can prepare it
properly.
So in terms of the data formats, there are just all sorts of things,
you sometimes get text files, csv files, mat files, there are of
course also Python- own file types, pkl for example, these are such
pixelated files that are compressed or some other hdfi file formats,
so that's relatively normal that you have to invest some time to read
data from a certain source and then import it correctly
.
So what you usually do is plot it and see if you got the right thing
at all, so most of the time you also have a certain idea of what it
should actually look like and of course we do that now and we also
have a little info, so the first column of x-train and x-test that
includes the speed and the second column that is the acceleration and
yes, the two variables y-train and y-test, which ultimately include
the electrical power.
Exactly, and that would be the next task now, that you just write a
function, for example, or also write a script with which you can plot
these values ​​or these time lines and ideally it is also the case
that you basically plot it in such a way that first of all the
training data can be seen in one color and then directly on the back,
on the side, the test data comes.
So we have a kind of course here, there is the training data, so far
samples and then on the back the test data are connected and then it
would be ideal if the two lines differ in color so that you can see it
in the end.
This is also such a time-consuming and typical step of work that you
first have to visualize the data and that would be the second task
here, where you can work with matplotlib.
Yes, and there is also a solution to this, that is this plot data.
Yes, I use NumPy and matplotlib here, read the function x-train, y
-train, x-test, y-test as an argument, then a picture is generated,
then we get two sizes out as variables, that makes it a lot easier.
So we want to have the size of this, i.e.
the number of lines of the x-train and then the number of lines of the
x -test and then you can basically build three such subplots together.
You can also make three individual plots, of course, and then it is
done in such a way that the individual plots are built up here and the
x -vector is always put together with NumPy range, I did that now, put
together, built together.
This goes from zero to the size of these training data, then the first
column is plotted in black first and then it goes on here.
In principle, here you can already see it, now it goes on from m to m
plus kval.
This is then basically the rear part, the test data part and I marked
that in red.
Exactly, here the difference is x-train and x-test and here these are
just these indices that then move on, so to speak.
So the m, which is here on the right side, it slides to the left and
here there is then this sum.
Yes, and that is of course very, very similar for all lines.
Here we have the first column that we look at, that is then the second
column.
Sorry, Python is indicating zero, so you sometimes get a little
confused in the way of speaking.
So that's the first column and that's the second column.
So here in the second subplot the acceleration is recorded and here in
the last subplot you can see the model output, which is then the
performance in the end.
Yes, and you can also import that, so you get this from solution plot
data, import asterisk and then you can simply call up this function
and then you can now see this time table here.
With this 2204, I think, there were data points in total and then you
see now there is such a certain part here, the left part from zero to
there, that is then plotted in black and on the right this validation
part or the test data part, which is plotted in red and there you can
now see the speed, acceleration and performance.
Yes, and then we can start now and start modeling the problem.
In the paper it was the case that we basically compared different
adaptive filters with a neural network.
So it was mainly about solving the problem iteratively.
I don't want to go that deep into it now.
In short, these adaptive filters that were used in the paper, if you
want to look into it, they are basically well suited if you have a
problem where there is an almost infinite flow of data.
So it is always taken iteratively, such a measurement, so for each
step in time, such an X goes into such an adaptive filter and then
there is also the starting point, the Y for one step in time and then
it is ultimately compared to the system, such a error is formed and
the internal filter weights are then marked a bit with the arrow ,
basically adapted over and over again.
Yes, you have to imagine it that way, if you now have these, for
example, 2000 measurements, then such an adaptive filter runs almost
2000 times in a row and each time such a data point, i.e.
in terms of time, such a measurement is ultimately entered in this
adaptive filter.
There is a certain algorithm that ultimately adapts the model to these
filter weights over and over again and gets better over time.
And yes, then it just runs in such a big loop.
And that is very different from the previous training procedures that
we had, where it was ultimately about linear models or we also had
neural networks and also briefly looked at Gaussian process
regression.
In principle, these training data are always presented as a whole, so
they immediately tip the whole matrix of training data into the
algorithm, which then calculates for a certain period of time and then
they get the model ready.
I just wanted to mention that here briefly in case you look into the
paper that you are not confused about what these adaptive filters are.
There is of course also a lot of material for this, but that would
actually be almost your own lecture if you get into the field.
So recursively squares filter would be such a linear adaptive filter.
Or even if you go into state-of-the-art models, there are, for
example, linear state-of-the-art models, the most prominent example
would be the Kalman filter.
But now here in this mini-project, we don't want to go that deep into
it, but what we are doing now is, we have now pre-processed the data
and took a look at the plot and saw that there are definitely
functional correlations in the data between input and output.
So there are many correlations, as you saw in the plot in front of us.
And now we just take standard machine learning regression methods from
Scikit -learn and try a linear model and such a Gauss process model.
Yes, and the linear model, we already had that in the Scikit-learn
part.
In principle, this is very easy to set up.
You have to import it.
So from the Scikit-learn linear model we get this linear regression.
Then the object is instanced.
You don't really need to give any big arguments for that.
And then we call this fit function or this fit method, to put it
better, it's an object, and then ultimately we get what we have
generated there, x-train and y-train, and then we get such an adapted
model.
Yes, and with these three lines, this machine learning task is
actually already done here.
And that looks very, very similar to the Gauss process models.
We get Gaussian process from Scikit-learn and get this Gaussian
process regressor.
And here I have set an optional argument again.
This is the number of optimization restarts.
It's like this, you can also see it a bit ... yes, that's hidden
deeper in the kernel object.
In any case, the Gauss process model has a certain kernel and these
kernels have hyperparameters.
And these hyperparameters are tuned again by such an optimization
process.
This is set here with BFGS.
So that's a certain optimizer and it just makes sense to restart it a
few times because the optimizer does not immediately deliver the best
result in the first run.
So in principle, such a GP model is fitted 20 times here and then the
best of them is returned.
Exactly, and the pure fitting is done again with such a one-liner,
with such a one-liner.
So then they call up the fit method again and ultimately hand over the
training values.
So now let's take a look at both models in comparison.
And now we take a metric that we had before.
This is the medium square error.
And then you can import it here from Scaler and Metrix.
There is already such a function that is simply written as a function.
You don't even need to initialize that as an object .
That means, this mean squared error basically takes two arguments, the
test and then ... or the validation data set and then as a second
argument the data that comes out of the model.
And that can then be generated with this predict.
So here we take the linear regression predict once and of course it
should do that for the test data set.
And the same down here with the second medium square error.
Then we take the predict for the Gauss process model and enter the
test data here again.
Yes, and if you look at it now, just numerically, so you can't
estimate the order of magnitude exactly what is good and bad.
But in comparison with each other you can see that here with these 20
restarts the Gauss process model has a much smaller medium square
error.
And to evaluate that a little better, let's take a look at it in such
a plot.
And here again as a comment, so the linear model is also very, very
easily straightened out.
We only have two gaps that go in, i.e.
the speed and the acceleration.
That means the linear model consists only of two model parameters that
are found there.
So it's very, very easy to describe the problem from the structure.
And then let's take a look at the plot, what it looks like for the
test data set.
And that's what I've done here so that I've just opened up a new
picture.
Since the data is now all the same length, we can just plot it
directly, we don't need a first argument.
That means here with this plot y-test in black, we get the reference
data into the picture first and then this predict function comes into
play again, the predict method.
And then we do that once for the linear model and then for the Gauss
process model in red and green.
And then a little legend.
Yes, and there you can see that the goal is to get as close as
possible to the black line over the time period.
So here you can see the electrical power again and that's just these
400 samples of test data.
And there we see the linear model in red.
There are areas where it really has big problems to follow in the end.
The quality of the course is actually not that bad.
So you can already see that these jumps are going quite well here.
So the courses are not that far apart.
However, it is then from the values.
So there are clear overshadows here, but they are just not accurate.
Or there is a longer period of time where you have such a clear
offset.
That works much better with the Gauss process model, I would say.
So of course that follows much closer to the test data set.
However, there are also areas here, so around here between 250 or 270
and 320.
There are some very strange effects here where the Gauss process model
suddenly delivers very extreme values, which then also leads to a
error, which is then quite high.
And yes, these are basically areas where the dynamics seems to be a
bit higher.
So you would have to look there again.
It's basically quite simple now, just fitted first without making any
major optimizations.
Exactly, but in such a simple way you can basically pipe in, read in
data, pre-process a bit, plot and then finally create such models.
Exactly, that's the summary of the plot interpretation again.
So that the linear model doesn't get along so well and the Gauss
process model is more accurate.
But still has a bit of a problem with these higher frequencies, as it
looks like.
You would have to look again and improve.
And yes, then I would like to come to a small summary of this mini
-project and then also to a larger task for you.
It was mainly about showing that you actually spend a lot of time or
need in practice to understand the modeling task and to consider which
inputs are actually relevant for the problem that you want to model.
So we put a lot of pre-knowledge in here at the beginning, so that we
ultimately already had driving dynamics equations in the back of our
heads and then roughly knew which signals you wanted to look at.
A CAN bus has a lot more signals than the 3-4 that we used there.
And of course it is also important for all the machine learning
algorithms that you choose useful inputs that ultimately describe the
variation from the beginning.
So you need a lot of modeling expertise and then we still needed a lot
of time to re-sort these data, re-divide it, rename it.
The whole thing with filtering and downsampling wasn't there yet.
That would actually also be part of this task if you get any CAN
notes.
And yes, that should simply show that if you have such modeling tasks
that this part is sometimes much larger to think about the problem and
to choose the right one than to ultimately use and apply these
optimization algorithms.
Of course, you can also adjust and tune a lot with these algorithms.
So I have left everything to default values ​​and of course you can
also try out completely different regression models with the data
sets.
But in practice it is often the case that these first steps are
actually a big part of the work.
Yes, and ultimately it is also a bit like the quintessence of this.
So if you are quite good in abstracting problems and can imagine which
ones have a certain systemic understanding, then you can model
problems pretty well.
And yes, I just want to give you that on the way, also as engineers,
so that you don't have any fear of these machine learning fields in
the end and just try to apply that and go in with it.
Because they are sometimes just very good, have a systemic
understanding and can also come to very good results with standard
algorithms.
Exactly, and now there would be a bigger task here.
I haven't really estimated how long it will take in terms of time, but
if you want, you can of course also take a look at this theme, this
adaptive filter.
You can take a look at the paper.
These are the basic algorithms briefly shown in such a small literary
review.
And as I said, the big difference is that you train the filter in such
a loop.
So you do this here, for example, that's a bit of a pseudocode, here
this pre-loop, that you go over all the training data in principle
again and again, sample by sample.
And the filter is always adapted to each single sample with such a
method, for example FIT.
And these filter coefficients are improved over and over again.
This means that you will also get a temporary course of these filter
coefficients, for example.
Yes, and then you can see if you can find a Python toolbox, for
example, to try out such adaptive filters for the problem.
There is certainly something there.
And then you can also try to apply that to the problem.
And if you then make the prediction, then you just take the very last
state of the training of this adaptive filter, so to speak, with the
last training sample.
There you can then take the model structure and then make the
prediction for the comparison with the test data.
Yes, and then of course you can also use the two other data sets.
We have not used them at all here in the slides.
The DAT2.mat and DAT3.mat to compare the models with later recordings,
how they work.
And of course, as a last option, you can also look at a few other
regression methods in Scikit-Learn and just challenge a bit which
model for this problem, or sorry, which regression method for this
problem then delivers the best results.
Exactly, so I think this is a big playground here.
You can get a little excited and then gain a little more experience.
Exactly, yes, that would be it for this part.
Thank you for listening and then it goes on with the last part, which
is about deep learning.
And then my colleague Maurizio Fernandes does that.