-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheigenvotes.py
More file actions
executable file
·71 lines (54 loc) · 1.75 KB
/
eigenvotes.py
File metadata and controls
executable file
·71 lines (54 loc) · 1.75 KB
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
65
66
67
68
69
70
71
#!/usr/bin/python
import sys
import numpy
import scan
def main(argv):
if len(argv) < 2:
print "usage: %s candidate_id" % argv[0]
sys.exit(1)
pivot = int(argv[1])
candidates = scan.loadCandidates()
candidate_by_rolls = {}
rolls_by_candidate = {}
voting_record = open('votes.txt')
for line in voting_record.readlines():
parts = line.replace(',', '').replace(':', '').split()
roll_number = int(parts[0])
candidate_id = int(parts[1])
vote = int(parts[2])
if not roll_number in candidate_by_rolls:
candidate_by_rolls[roll_number] = {}
if not candidate_id in rolls_by_candidate:
rolls_by_candidate[candidate_id] = {}
rolls_by_candidate[candidate_id][roll_number] = vote
candidate_by_rolls[roll_number][candidate_id] = vote
cohort = set()
for roll in rolls_by_candidate[pivot].keys():
for candidate in candidate_by_rolls[roll].keys():
cohort.add(candidate)
for candidate in cohort.copy():
if len(rolls_by_candidate[candidate].keys()) < 90:
cohort.remove(candidate)
votes = []
i = 0
pivot_idx = 0
for candidate in cohort:
votes.append([])
for roll in rolls_by_candidate[pivot].keys():
if candidate == pivot:
pivot_idx = i
if roll in rolls_by_candidate[candidate]:
votes[-1].append(rolls_by_candidate[candidate][roll])
else:
votes[-1].append(0)
i += 1
U,s,V = numpy.linalg.svd(votes)
i = 0
for candidate in cohort:
p = numpy.dot(votes[i], V)
print "%s; %f; %f; %s; 1" % (candidates[str(candidate)]['name'],
p[0], p[1],
candidates[str(candidate)]['party'])
i += 1
if __name__ == "__main__":
main(sys.argv)