Coding Math Mini #1: Normalization

Coding Math Mini #1: Normalization


This is Coding Math Mini Number One, Normalization I’ve been thinking about increasing the number
of videos I do per week, but the 10-15 minute videos do take a while to record and edit.
So I’m going to see if I can do a shorter video once a week – 5 minutes or less on a
quick and simple topic. For those of you supporting Coding Math on
Patreon, this will be a free video. So what is normalization? Simply put, it’s
taking a range of values, and a value within that range and converting that value to a
number from 0 to 1 that indicates where it lies in that range. For example, say you counted the number of
minutes you watch coding math videos each day and you find that it ranges from 0 to
33 minutes a day. Then, on a particular day you watch 23 minutes.
and you want to know where that lies within that range, expressed so that 0 would be the
minimum and 1 would be the maximum. It’s kind of like a percentage of that range. Well, in this case it’s pretty easy, you divide
the value, 23, by the maximum, 33, and you get 0.696969 repeating or 0.69 with a bar
over it. But the problem is minimum doesn’t always
have to be 0. In a much more realistic example, say you always watch at least 5 minutes of
coding math videos per day. Now the range is 5 to 33, with a size of 28. but you can’t
just divide 23 by 28. You want to find out how much of this value falls into this range.
So first you need to chop off this part of it. Now you can divide what’s left here, 18,
by the range amount, 28. and you get about .643. In mathematical terms, that means (value – min)
/ (max – min). Maximum minus minimum is the range and value
minus minimum is the amount of the value that lies within that range. So we can code a norm function. It takes a value, and minimum and maximum
values that define the range. Really simple. And what’s awesome about this function is
its flexibility. The minimum and maximum can be any values, positive or negative. You can
even reverse them and have a max that’s less than the min if you had some kind of reverse
range and it will still work. And the value can even lie outside the range. In that case
it will give you a number that is greater than one or less than zero, but will still
be logically relevant. Now this has many uses, but one I’ll highlight
is drawing graphs of a set of values. Here i’ve pre built a project with the normal canvas
template files. I have an array of values and do a bit of
hackery to dynamically get the min and max of those values. then there’s our norm function. I begin a path and then loop through the values
in the array. I get the normalized value for each element and compute and x and y value.
x is just a percentage of the screen width. For y, I multiply norm value by the screen
height, then subtract it from height to reverse the y axis. ThenI just move to or line to that point and
stroke the path when its done. And bingo, you have a graph of the values, scaled so
that the minimum value is at the bottom of the screen and the max is at the top. You
can throw any numbers in that array and it will work. So that’s normalize. Throw that in a utility
library somewhere and keep it in mind and i’m sure you’ll find many uses for it.

5 thoughts to “Coding Math Mini #1: Normalization”

  1. Hi, have you ever thought about using Processing (or actually p5.js, which is Processing adapted to Javascript instead of Java) for your examples? It's more or less like Javascript but has a lot of neat predefined functions that make it really great for producing visual output …

  2. I learn what normalization is from this video within 1:50min. It took me 3 semesters to barely understood what it is in Linear Algebra courses…

Leave a Reply

Your email address will not be published. Required fields are marked *