diff options
author | Patrick Simianer <p@simianer.de> | 2014-02-12 18:11:33 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-02-12 18:11:33 +0100 |
commit | 64dd24e1698e11ce461048f30e3367fe33f626fa (patch) | |
tree | 3afd3233aa9a02c25ab7559dee6f62c2627fec87 /lib | |
parent | 27bc315543a4e3002e5d4ec0e37be3dcc2e3114e (diff) |
SparseVector: from_a
Diffstat (limited to 'lib')
-rw-r--r-- | lib/nlp_ruby/SparseVector.rb | 4 | ||||
-rw-r--r-- | lib/nlp_ruby/Vector.rb | 43 |
2 files changed, 4 insertions, 43 deletions
diff --git a/lib/nlp_ruby/SparseVector.rb b/lib/nlp_ruby/SparseVector.rb index 964ef4e..a4e2bce 100644 --- a/lib/nlp_ruby/SparseVector.rb +++ b/lib/nlp_ruby/SparseVector.rb @@ -5,6 +5,10 @@ class SparseVector < Hash self.default = 0 end + def from_a a + a.each_with_index { |i,j| self[j] = i } + end + def from_h h h.each_pair { |k,v| self[k] = v } end diff --git a/lib/nlp_ruby/Vector.rb b/lib/nlp_ruby/Vector.rb deleted file mode 100644 index c749ed9..0000000 --- a/lib/nlp_ruby/Vector.rb +++ /dev/null @@ -1,43 +0,0 @@ -class Vector < Array - - def sum - self.inject(:+) - end - - def average - self.sum/self.size.to_f - end - - def variance - avg = self.average - var = 0.0 - self.each { |i| var += (avg - i)**2 } - var - end - - def stddev list_of_values, decimal_places=-1 - Math.sqrt self.variance - end - - def dot other - return nil if self.size!=other.size - sum = 0.0 - self.each_with_index { |i,j| sum += i*other[j] } - return sum - end - - def magnitude - Math.sqrt self.inject { |sum,i| sum+i**2 } - end - - def cosinus_sim other - return self.dot(other)/(self.mag*other.mag) - end - - def euclidian_dist other - sum = 0.0 - self.each_with_index { |i,j| sum += (i - other[j])**2 } - return Math.sqrt(sum) - end -end - |