Class: SparseVector
- Inherits:
-
Hash
- Object
- Hash
- SparseVector
- Defined in:
- lib/zipf/SparseVector.rb
Class Method Summary collapse
- .from_a(a) ⇒ Object
- .from_file(fn, sep = '=') ⇒ Object
- .from_h(h) ⇒ Object
- .from_json(s) ⇒ Object
- .from_kv(s, sep = '=', join = /\s/) ⇒ Object
- .from_s(s) ⇒ Object
- .mean(a) ⇒ Object
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(scalar) ⇒ Object
- #approx_eql?(other, p = 10**-10) ⇒ Boolean
- #average ⇒ Object
- #cosinus_sim(other) ⇒ Object
- #dot(other) ⇒ Object
- #euclidian_dist(other) ⇒ Object
- #from_a(a) ⇒ Object
- #from_file(fn, sep = '=') ⇒ Object
- #from_h(h) ⇒ Object
- #from_json(s) ⇒ Object
- #from_kv(s, sep = '=', join = /\s/) ⇒ Object
- #from_s(s) ⇒ Object
-
#initialize(arg = nil) ⇒ SparseVector
constructor
A new instance of SparseVector.
- #join_keys(other) ⇒ Object
- #magnitude ⇒ Object
- #norm ⇒ Object
- #stddev ⇒ Object
- #sum ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
- #to_kv(sep = '=', join = ' ') ⇒ Object
- #unit ⇒ Object
- #unit! ⇒ Object
- #variance ⇒ Object
- #zeros(n) ⇒ Object
Constructor Details
#initialize(arg = nil) ⇒ SparseVector
Returns a new instance of SparseVector.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/zipf/SparseVector.rb', line 5 def initialize arg=nil super self.default = 0 if arg.is_a? Array from_a arg elsif arg.is_a? Hash from_h arg elsif arg.is_a? String from_s arg end end |
Class Method Details
.from_a(a) ⇒ Object
21 22 23 24 25 |
# File 'lib/zipf/SparseVector.rb', line 21 def self.from_a a v = SparseVector.new v.from_a a return v end |
.from_file(fn, sep = '=') ⇒ Object
97 98 99 100 101 |
# File 'lib/zipf/SparseVector.rb', line 97 def self.from_file fn, sep='=' v = SparseVector.new v.from_file fn, sep return v end |
.from_h(h) ⇒ Object
37 38 39 40 41 |
# File 'lib/zipf/SparseVector.rb', line 37 def self.from_h h v = SparseVector.new v.from_h h return v end |
.from_json(s) ⇒ Object
82 83 84 85 86 |
# File 'lib/zipf/SparseVector.rb', line 82 def self.from_json s v = SparseVector.new v.from_json s return v end |
.from_kv(s, sep = '=', join = /\s/) ⇒ Object
68 69 70 71 72 |
# File 'lib/zipf/SparseVector.rb', line 68 def self.from_kv s, sep='=', join=/\s/ v = SparseVector.new v.from_kv s, sep, join return v end |
.from_s(s) ⇒ Object
47 48 49 50 51 |
# File 'lib/zipf/SparseVector.rb', line 47 def self.from_s s v = SparseVector.new v.from_s s return v end |
.mean(a) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/zipf/SparseVector.rb', line 195 def self.mean a mean = SparseVector.new a.each { |i| i.each_pair { |k,v| mean[k] += v } } n = a.size.to_f mean.each_pair { |k,v| mean[k] = v/n } return mean end |
Instance Method Details
#*(scalar) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/zipf/SparseVector.rb', line 177 def * scalar raise ArgumentError, "Arg is not numeric #{scalar}" unless scalar.is_a? Numeric new = SparseVector.new self.keys.each { |k| new[k] = self[k] * scalar } return new end |
#+(other) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/zipf/SparseVector.rb', line 161 def + other new = SparseVector.new join_keys(other).each { |k| new[k] = self[k]+other[k] } return new end |
#-(other) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/zipf/SparseVector.rb', line 169 def - other new = SparseVector.new join_keys(other).each { |k| new[k] = self[k]-other[k] } return new end |
#/(scalar) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/zipf/SparseVector.rb', line 186 def / scalar raise ArgumentError, "Arg is not numeric #{scalar}" unless scalar.is_a? Numeric new = SparseVector.new self.keys.each { |k| new[k] = self[k] / scalar } return new end |
#approx_eql?(other, p = 10**-10) ⇒ Boolean
111 112 113 114 115 116 117 118 119 |
# File 'lib/zipf/SparseVector.rb', line 111 def approx_eql? other, p=10**-10 return false if !other return false if other.size!=self.size return false if other.keys.sort!=self.keys.sort self.keys.each { |k| return false if (self[k]-other[k]).abs>p } return true end |
#average ⇒ Object
121 122 123 |
# File 'lib/zipf/SparseVector.rb', line 121 def average self.sum/self.size.to_f end |
#cosinus_sim(other) ⇒ Object
150 151 152 |
# File 'lib/zipf/SparseVector.rb', line 150 def cosinus_sim other self.dot(other)/(self.magnitude*other.magnitude) end |
#dot(other) ⇒ Object
136 137 138 139 140 |
# File 'lib/zipf/SparseVector.rb', line 136 def dot other sum = 0.0 self.each_pair { |k,v| sum += v * other[k] } return sum end |
#euclidian_dist(other) ⇒ Object
154 155 156 157 158 159 |
# File 'lib/zipf/SparseVector.rb', line 154 def euclidian_dist other dims = [self.keys, other.keys].flatten.uniq sum = 0.0 dims.each { |d| sum += (self[d] - other[d])**2 } return Math.sqrt(sum) end |
#from_a(a) ⇒ Object
17 18 19 |
# File 'lib/zipf/SparseVector.rb', line 17 def from_a a a.each_with_index { |i,j| self[j] = i } end |
#from_file(fn, sep = '=') ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/zipf/SparseVector.rb', line 88 def from_file fn, sep='=' f = ReadFile.new(fn) while line = f.gets key, value = line.strip.split sep value = value.to_f self[key] = value end end |
#from_h(h) ⇒ Object
33 34 35 |
# File 'lib/zipf/SparseVector.rb', line 33 def from_h h h.each_pair { |k,v| self[k] = v } end |
#from_json(s) ⇒ Object
78 79 80 |
# File 'lib/zipf/SparseVector.rb', line 78 def from_json s from_h JSON.load(s) end |
#from_kv(s, sep = '=', join = /\s/) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/zipf/SparseVector.rb', line 61 def from_kv s, sep='=', join=/\s/ s.strip.split(join).each { |i| k,v = i.split(sep) self[k] = v.to_f } end |
#from_s(s) ⇒ Object
43 44 45 |
# File 'lib/zipf/SparseVector.rb', line 43 def from_s s from_h eval(s.strip) end |
#join_keys(other) ⇒ Object
103 104 105 |
# File 'lib/zipf/SparseVector.rb', line 103 def join_keys other self.keys + other.keys end |
#magnitude ⇒ Object
146 147 148 |
# File 'lib/zipf/SparseVector.rb', line 146 def magnitude Math.sqrt self.values.inject { |sum,i| sum+i**2 } end |
#norm ⇒ Object
207 208 209 |
# File 'lib/zipf/SparseVector.rb', line 207 def norm return Math.sqrt(self.dot(self)) end |
#stddev ⇒ Object
132 133 134 |
# File 'lib/zipf/SparseVector.rb', line 132 def stddev Math.sqrt self.variance end |
#sum ⇒ Object
107 108 109 |
# File 'lib/zipf/SparseVector.rb', line 107 def sum self.values.inject(:+) end |
#to_h ⇒ Object
27 28 29 30 31 |
# File 'lib/zipf/SparseVector.rb', line 27 def to_h h = {} self.each_pair { |k,v| h[k] = v } return h end |
#to_json ⇒ Object
74 75 76 |
# File 'lib/zipf/SparseVector.rb', line 74 def to_json JSON.dump self.to_h end |
#to_kv(sep = '=', join = ' ') ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/zipf/SparseVector.rb', line 53 def to_kv sep='=', join=' ' a = [] self.each_pair { |k,v| a << "#{k}#{sep}#{v}" } return a.join join end |
#unit ⇒ Object
218 219 220 221 222 |
# File 'lib/zipf/SparseVector.rb', line 218 def unit v = SparseVector.new(self) v.unit! return v end |
#unit! ⇒ Object
211 212 213 214 215 216 |
# File 'lib/zipf/SparseVector.rb', line 211 def unit! n = self.norm self.each_pair { |k,v| self[k] /= n } end |
#variance ⇒ Object
125 126 127 128 129 130 |
# File 'lib/zipf/SparseVector.rb', line 125 def variance avg = self.average var = 0.0 self.values.each { |i| var += (avg - i)**2 } return var end |
#zeros(n) ⇒ Object
142 143 144 |
# File 'lib/zipf/SparseVector.rb', line 142 def zeros n (0).upto(n-1) { |i| self[i] = 0.0 } end |