Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 744
meow Avatar asked Oct 02 '09 05:10

meow


1 Answers

Here's the Ruby Way for the common case of setting an array's min to 0.0 and max to 1.0.

class Array
  def normalize!
    xMin,xMax = self.minmax
    dx = (xMax-xMin).to_f
    self.map! {|x| (x-xMin) / dx }
  end
end

a = [3.0, 6.0, 3.1416]
a.normalize!
=> [0.0, 1.0, 0.047199999999999985]

For a min and max other than 0 and 1, add arguments to normalize! in the manner of Elfstrom's answer.

like image 194
Camille Goudeseune Avatar answered Sep 19 '22 15:09

Camille Goudeseune