Hi folks, i have a data set that ranges from 1 to 30,000
I want to normalize it, so that it becomes 0.1 to 10
What is the best method/function to do that?
Would greatly appreciate it if you could give some sample code!
Hi folks, i have a data set that ranges from 1 to 30,000
I want to normalize it, so that it becomes 0.1 to 10
What is the best method/function to do that?
Would greatly appreciate it if you could give some sample code!
Here's a code snippet, assuming you want a linear normalization. It's a very simplistic version (just straight code, no methods), so you can see "how it works" and can apply it to anything.
xmin = 1.0
xmax = 30000.0
ymin = 0.1
ymax = 10.0
xrange = xmax-xmin
yrange = ymax-ymin
y = ymin + (x-xmin) * (yrange / xrange)
And here it is done as a function:
def normalise(x,xmin,xmax,ymin,ymax)
xrange = xmax-xmin
yrange = ymax-ymin
ymin + (x-xmin) * (yrange.to_f / xrange)
end
puts normalise(2000, 1, 30000, 0.1, 10)
(Note: the to_f
ensures we don't fall into the black hole of integer division)
dataset = [1,30000,15000,200,3000]
dataset.map {|n| 0.1+(n-1)*(9.9/29999)}
=> [0.1, 10.0, 5.04983499449982, 0.165672189072969, 1.08970299009967]