blob: 0033690d3f3b96376729b849b2ecf78e6627ba52 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
class SparseVector < Hash
def initialize
super
self.default = 0
end
def from_hash h
h.each_pair { |k,v| self[k] = v }
end
def sum
self.values.inject(:+)
end
def average
self.sum/self.size.to_f
end
def variance
avg = self.average
var = 0.0
self.values.each { |i| var += (avg - i)**2 }
return var
end
def stddev
Math.sqrt self.variance
end
def dot other
sum = 0.0
self.each_pair { |k,v| sum += v * other[k] }
return sum
end
def magnitude
Math.sqrt self.values.inject { |sum,i| sum+i**2 }
end
def cosinus_sim other
self.dot(other)/(self.magnitude*other.magnitude)
end
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
end
def mean_sparse_vector array_of_vectors
mean = SparseVector.new
array_of_vectors.each { |i|
i.each_pair { |k,v|
mean[k] += v
}
}
n = array_of_vectors.size.to_f
mean.each_pair { |k,v| mean[k] = v/n }
return mean
end
|