-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVector.py
37 lines (29 loc) · 974 Bytes
/
Vector.py
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
"""
This borrows a lot from http://inst.eecs.berkeley.edu/~cs188/fa11/projects/search/docs/util.html
This is currently incomplete. Will add functionality as needed.
"""
class Vector(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.default = 0
def __getitem__(self, key):
if key in self:
return dict.__getitem__(self, key)
else:
return self.default
def normalize(self):
total = self.total_value() * 1.0
normalized = Vector()
for k in self:
normalized[k] = self[k]/total
return normalized
def total_value(self):
return sum(self.values())
def arg_min(self):
if not self:
return None
return sorted(self.items(), key=lambda x: x[1])[0][0]
def arg_max(self):
if not self:
return None
return sorted(self.items(), key=lambda x: -x[1])[0][0]