views:

250

answers:

3

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!

+1  A: 

x = x / 3030.3031 + 0.1

DigitalRoss
+4  A: 

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)

Brent.Longborough
Thanks brent! that is a nice and elegant way of doing this =)
ming yeow
+1  A: 
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]

Jonas Elfström