I am lost in normalizing, could anyone guide me please.
I have a minimum and maximum values, say -23.89 and 7.54990767, respectively.
If I get a value of 5.6878 how can I scale this value on a scale of 0 to 1.
I am lost in normalizing, could anyone guide me please.
I have a minimum and maximum values, say -23.89 and 7.54990767, respectively.
If I get a value of 5.6878 how can I scale this value on a scale of 0 to 1.
If you want to normalize your data, you can do so as you suggest and simply calculate the following:
$$z_i=\frac{x_i-\min(x)}{\max(x)-\min(x)}$$
where $x=(x_1,...,x_n)$ and $z_i$ is now your $i^{th}$ normalized data. As a proof of concept (although you did not ask for it) here is some R
code and accompanying graph to illustrate this point:
# Example Data
x = sample(-100:100, 50)
#Normalized Data
normalized = (x-min(x))/(max(x)-min(x))
# Histogram of example data and normalized data
par(mfrow=c(1,2))
hist(x, breaks=10, xlab="Data", col="lightblue", main="")
hist(normalized, breaks=10, xlab="Normalized Data", col="lightblue", main="")
illustrate the point
of your (correct) answer?
$\endgroup$
The general one-line formula to linearly rescale data values having observed min and max into a new arbitrary range min' to max' is
newvalue= (max'-min')/(max-min)*(value-max)+max'
or
newvalue= (max'-min')/(max-min)*(value-min)+min'.
a
and b
constants, and then just apply newvalue = a * value + b
. a = (max'-min')/(max-min)
and b = max - a * max
$\endgroup$
Commented
Sep 23, 2013 at 19:18
b = max' - a * max
or b = min' - (a * min)
$\endgroup$
u = -1 + 2.*(u - min(u))./(max(u) - min(u));
.
$\endgroup$
Commented
Oct 24, 2016 at 21:41
Here is my PHP implementation for normalisation:
function normalize($value, $min, $max) {
$normalized = ($value - $min) / ($max - $min);
return $normalized;
}
But while I was building my own artificial neural networks, I needed to transform the normalized output back to the original data to get good readable output for the graph.
function denormalize($normalized, $min, $max) {
$denormalized = ($normalized * ($max - $min) + $min);
return $denormalized;
}
$int = 12;
$max = 20;
$min = 10;
$normalized = normalize($int, $min, $max); // 0.2
$denormalized = denormalize($normalized, $min, $max); //12
Denormalisation uses the following formula:
$x (\text{max} - \text{min}) + \text{min}$
One thing to keep in mind is that max - min
could equal zero. In this case, you would not want to perform that division.
The case where this would happen is when all values in the list you're trying to normalize are the same. To normalize such a list, each item would be 1 / length
.
// JavaScript
function normalize(list) {
var minMax = list.reduce((acc, value) => {
if (value < acc.min) {
acc.min = value;
}
if (value > acc.max) {
acc.max = value;
}
return acc;
}, {min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY});
return list.map(value => {
// Verify that you're not about to divide by zero
if (minMax.max === minMax.min) {
return 1 / list.length
}
var diff = minMax.max - minMax.min;
return (value - minMax.min) / diff;
});
}
normalize([3, 3, 3, 3]); // output => [0.25, 0.25, 0.25, 0.25]
normalize([12, 20, 10])
outputs [0.2, 1.0, 0.0]
, which is the same you would get with (val - min) / (max - min)
.
$\endgroup$
Commented
Jan 16, 2019 at 15:23
Try this. It is consistent with the function scale
normalize <- function(x) {
x <- as.matrix(x)
minAttr=apply(x, 2, min)
maxAttr=apply(x, 2, max)
x <- sweep(x, 2, minAttr, FUN="-")
x=sweep(x, 2, maxAttr-minAttr, "/")
attr(x, 'normalized:min') = minAttr
attr(x, 'normalized:max') = maxAttr
return (x)
}
The answer is right but I have a suggestion, what if your training data face some number out of range? you could use the squashing technique. it will be guaranteed never to go out of range. rather than this
I recommend using this
with squashing like this in min and max of the range
and the size of the expected out-of-range gap is directly proportional to the degree of confidence that there will be out-of-range values.
For more information, you can google: squashing the out-of-range numbers and refer to the data preparation book of "Dorian Pyle".
Select a cumulative probability distribution F. Then F(x) is between 0 and 1 for every x.