|
15 | 15 | VoteValue = float |
16 | 16 |
|
17 | 17 | # Load votes arranged by topic and accumulate votes by senator |
18 | | -vote_value: Dict[str, VoteValue] = {'Nay': -1, 'Not Voting': 0, 'Yea': 1} |
19 | | -accumulated_record: DefaultDict[Senator, List[VoteValue]] = defaultdict(list) |
| 18 | +vote_value = {'Nay': -1, 'Not Voting': 0, 'Yea': 1} # type: Dict[str, VoteValue] |
| 19 | +accumulated_record = defaultdict(list) # type: DefaultDict[Senator, List[VoteValue]] |
20 | 20 | for filename in glob.glob('congress_data/*.csv'): |
21 | 21 | with open(filename, encoding='utf-8') as f: |
22 | 22 | reader = csv.reader(f) |
|
27 | 27 | accumulated_record[senator].append(vote_value[vote]) |
28 | 28 |
|
29 | 29 | # Transform record into plain dict mapping a senator to a tuple of vote values |
30 | | -record: Dict[Senator, Tuple[VoteValue, ...]] = {senator: tuple(votes) for senator, votes in accumulated_record.items() } |
| 30 | +record = {senator: tuple(votes) for senator, votes in accumulated_record.items() } # type: Dict[Senator, Tuple[VoteValue, ...]] |
31 | 31 |
|
32 | 32 | # Use k-means to locate the cluster centroids and assign senators to the nearest cluster |
33 | 33 | centroids = k_means(record.values(), k=3, iterations=50) |
34 | 34 | clustered_votes = assign_data(centroids, record.values()) |
35 | 35 |
|
36 | 36 | # Build a reverse mapping from a pattern of votes to senators who voted that way |
37 | | -votes_to_senators: DefaultDict[Tuple[VoteValue, ...], List[Senator]] = defaultdict(list) |
| 37 | +votes_to_senators = defaultdict(list) # type: DefaultDict[Tuple[VoteValue, ...], List[Senator]] |
38 | 38 | for senator, votes in record.items(): |
39 | 39 | votes_to_senators[votes].append(senator) |
40 | | -assert sum(len(cluster) for cluster in clustered_votes.values()) == 100 |
| 40 | +assert sum(map(len, clustered_votes.values())) == 100 |
41 | 41 |
|
42 | 42 | # Display the clusters and the members of each cluster |
43 | 43 | for i, votes_in_cluster in enumerate(clustered_votes.values(), start=1): |
44 | 44 | print(f'=========== Voting Cluster #{i} ===========') |
45 | | - party_totals: Counter = Counter() |
| 45 | + party_totals = Counter() # type: Counter |
46 | 46 | for votes in set(votes_in_cluster): |
47 | 47 | for senator in votes_to_senators[votes]: |
48 | 48 | party_totals[senator.party] += 1 |
|
0 commit comments