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
- #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
- #stddev ⇒ Object
- #sum ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
- #to_kv(sep = '=', join = ' ') ⇒ Object
- #variance ⇒ Object
- #zeros(n) ⇒ Object
Constructor Details
#initialize(arg = nil) ⇒ SparseVector
Returns a new instance of SparseVector.
6 7 8 9 10 11 12 |
# File 'lib/zipf/SparseVector.rb', line 6 def initialize arg=nil super self.default = 0 if arg.is_a? Array from_a arg end end |
Class Method Details
.from_a(a) ⇒ Object
18 19 20 21 22 |
# File 'lib/zipf/SparseVector.rb', line 18 def self.from_a a v = SparseVector.new v.from_a a return v end |
.from_file(fn, sep = '=') ⇒ Object
94 95 96 97 98 |
# File 'lib/zipf/SparseVector.rb', line 94 def self.from_file fn, sep='=' v = SparseVector.new v.from_file fn, sep return v end |
.from_h(h) ⇒ Object
34 35 36 37 38 |
# File 'lib/zipf/SparseVector.rb', line 34 def self.from_h h v = SparseVector.new v.from_h h return v end |
.from_json(s) ⇒ Object
73 74 75 76 77 |
# File 'lib/zipf/SparseVector.rb', line 73 def self.from_json s v = SparseVector.new v.from_json s return v end |
.from_kv(s, sep = '=', join = /\s/) ⇒ Object
79 80 81 82 83 |
# File 'lib/zipf/SparseVector.rb', line 79 def self.from_kv s, sep='=', join=/\s/ v = SparseVector.new v.from_kv s, sep, join return v end |
.from_s(s) ⇒ Object
44 45 46 47 48 |
# File 'lib/zipf/SparseVector.rb', line 44 def self.from_s s v = SparseVector.new v.from_s s return v end |
.mean(a) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/zipf/SparseVector.rb', line 183 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
174 175 176 177 178 179 180 181 |
# File 'lib/zipf/SparseVector.rb', line 174 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
158 159 160 161 162 163 164 |
# File 'lib/zipf/SparseVector.rb', line 158 def + other new = SparseVector.new join_keys(other).each { |k| new[k] = self[k]+other[k] } return new end |
#-(other) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/zipf/SparseVector.rb', line 166 def - other new = SparseVector.new join_keys(other).each { |k| new[k] = self[k]-other[k] } return new end |
#approx_eql?(other, p = 10**-10) ⇒ Boolean
108 109 110 111 112 113 114 115 116 |
# File 'lib/zipf/SparseVector.rb', line 108 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
118 119 120 |
# File 'lib/zipf/SparseVector.rb', line 118 def average self.sum/self.size.to_f end |
#cosinus_sim(other) ⇒ Object
147 148 149 |
# File 'lib/zipf/SparseVector.rb', line 147 def cosinus_sim other self.dot(other)/(self.magnitude*other.magnitude) end |
#dot(other) ⇒ Object
133 134 135 136 137 |
# File 'lib/zipf/SparseVector.rb', line 133 def dot other sum = 0.0 self.each_pair { |k,v| sum += v * other[k] } return sum end |
#euclidian_dist(other) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/zipf/SparseVector.rb', line 151 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
14 15 16 |
# File 'lib/zipf/SparseVector.rb', line 14 def from_a a a.each_with_index { |i,j| self[j] = i } end |
#from_file(fn, sep = '=') ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/zipf/SparseVector.rb', line 85 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
30 31 32 |
# File 'lib/zipf/SparseVector.rb', line 30 def from_h h h.each_pair { |k,v| self[k] = v } end |
#from_json(s) ⇒ Object
69 70 71 |
# File 'lib/zipf/SparseVector.rb', line 69 def from_json s from_h JSON.load(s) end |
#from_kv(s, sep = '=', join = /\s/) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/zipf/SparseVector.rb', line 58 def from_kv s, sep='=', join=/\s/ s.split(join).each { |i| k,v = i.split(sep) self[k] = v.to_f } end |
#from_s(s) ⇒ Object
40 41 42 |
# File 'lib/zipf/SparseVector.rb', line 40 def from_s s from_h eval(s) end |
#join_keys(other) ⇒ Object
100 101 102 |
# File 'lib/zipf/SparseVector.rb', line 100 def join_keys other self.keys + other.keys end |
#magnitude ⇒ Object
143 144 145 |
# File 'lib/zipf/SparseVector.rb', line 143 def magnitude Math.sqrt self.values.inject { |sum,i| sum+i**2 } end |
#stddev ⇒ Object
129 130 131 |
# File 'lib/zipf/SparseVector.rb', line 129 def stddev Math.sqrt self.variance end |
#sum ⇒ Object
104 105 106 |
# File 'lib/zipf/SparseVector.rb', line 104 def sum self.values.inject(:+) end |
#to_h ⇒ Object
24 25 26 27 28 |
# File 'lib/zipf/SparseVector.rb', line 24 def to_h h = {} self.each_pair { |k,v| h[k] = v } return h end |
#to_json ⇒ Object
65 66 67 |
# File 'lib/zipf/SparseVector.rb', line 65 def to_json JSON.dump self.to_h end |
#to_kv(sep = '=', join = ' ') ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/zipf/SparseVector.rb', line 50 def to_kv sep='=', join=' ' a = [] self.each_pair { |k,v| a << "#{k}#{sep}#{v}" } return a.join join end |
#variance ⇒ Object
122 123 124 125 126 127 |
# File 'lib/zipf/SparseVector.rb', line 122 def variance avg = self.average var = 0.0 self.values.each { |i| var += (avg - i)**2 } return var end |
#zeros(n) ⇒ Object
139 140 141 |
# File 'lib/zipf/SparseVector.rb', line 139 def zeros n (0).upto(n-1) { |i| self[i] = 0.0 } end |