Search code examples
rubynormalizing

Normalizing dataset with ruby


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!


Solution

  • 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)