File size: 4,732 Bytes
d95feba
 
 
 
 
 
 
 
 
 
64370c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d95feba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d391924
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d95feba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
    "party",
    "vote_on_handicapped_infants_bill",
    "vote_on_water_project_cost_sharing_bill",
    "vote_on_adoption_of_the_budget_resolution_bill",
    "vote_on_physician_fee_freeze_bill",
    "vote_on_el_salvador_aid_bill",
    "vote_on_religious_groups_in_schools_bill",
    "vote_on_anti_satellite_test_ban_bill",
    "vote_on_aid_to_nicaraguan_contras_bill",
    "vote_on_mx_missile_bill",
    "vote_on_immigration_bill",
    "vote_on_synfuels_corporation_cutback_bill",
    "vote_on_education_spending_bill",
    "vote_on_superfund_right_to_sue_bill",
    "vote_on_crime_bill",
    "vote_on_duty_free_exports_bill",
    "vote_on_export_administration_act_south_africa_bill",
]

DESCRIPTION = "Congress dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Congress"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/105/congressional+voting+records")
_CITATION = """
@misc{misc_congressional_voting_records_105,
  title        = {{Congressional Voting Records}},
  year         = {1987},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C5C01P}}
}"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/congress/raw/main/house-votes-84.data"
}
features_types_per_config = {
    "voting": {
        "vote_on_handicapped_infants_bill": datasets.Value("string"),
        "vote_on_water_project_cost_sharing_bill": datasets.Value("string"),
        "vote_on_adoption_of_the_budget_resolution_bill": datasets.Value("string"),
        "vote_on_physician_fee_freeze_bill": datasets.Value("string"),
        "vote_on_el_salvador_aid_bill": datasets.Value("string"),
        "vote_on_religious_groups_in_schools_bill": datasets.Value("string"),
        "vote_on_anti_satellite_test_ban_bill": datasets.Value("string"),
        "vote_on_aid_to_nicaraguan_contras_bill": datasets.Value("string"),
        "vote_on_mx_missile_bill": datasets.Value("string"),
        "vote_on_immigration_bill": datasets.Value("string"),
        "vote_on_synfuels_corporation_cutback_bill": datasets.Value("string"),
        "vote_on_education_spending_bill": datasets.Value("string"),
        "vote_on_superfund_right_to_sue_bill": datasets.Value("string"),
        "vote_on_crime_bill": datasets.Value("string"),
        "vote_on_duty_free_exports_bill": datasets.Value("string"),
        "vote_on_export_administration_act_south_africa_bill": datasets.Value("string"),
        "party": datasets.ClassLabel(num_classes=2, names=("democrat", "republican")),
    }
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}


class CongressConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(CongressConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Congress(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "voting"
    BUILDER_CONFIGS = [
        CongressConfig(name="voting",
                    description="Binary classification of politician, either democrat or republican.")
    ]


    def _info(self):
        info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
                                    features=features_per_config[self.config.name])

        return info
    
    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        downloads = dl_manager.download_and_extract(urls_per_split)

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]})
        ]
    
    def _generate_examples(self, filepath: str):
        data = pandas.read_csv(filepath, header=None)
        data = self.preprocess(data, config=self.config.name)

        for row_id, row in data.iterrows():
            data_row = dict(row)

            yield row_id, data_row


    def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame:
        data.columns = _BASE_FEATURE_NAMES
        vote_dictionary = {
            "y": "pro",
            "n": "against",
            "?": "did_not_vote",
        }
        for feature in _BASE_FEATURE_NAMES[1:]:
            data.loc[:, feature] = data[feature].apply(lambda x: vote_dictionary[x])
        data.loc[:, "party"] = data["party"].apply(lambda x: 0 if x == "democrat" else 1)
        data = data.astype({"party": "int8"})

        data = data[list(features_types_per_config[config].keys())]

        return data