|
"""Congress""" |
|
|
|
from typing import List |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
_BASE_FEATURE_NAMES = [ |
|
"party", |
|
"pro_handicapped_infants", |
|
"pro_water_project_cost_sharing", |
|
"pro_adoption_of_the_budget_resolution", |
|
"pro_physician_fee_freeze", |
|
"pro_el_salvador_aid", |
|
"pro_religious_groups_in_schools", |
|
"pro_anti_satellite_test_ban", |
|
"pro_aid_to_nicaraguan_contras", |
|
"pro_mx_missile", |
|
"pro_immigration", |
|
"pro_synfuels_corporation_cutback", |
|
"pro_education_spending", |
|
"pro_superfund_right_to_sue", |
|
"pro_crime", |
|
"pro_duty_free_exports", |
|
"pro_export_administration_act_south_africa", |
|
] |
|
|
|
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}} |
|
}""" |
|
|
|
|
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/congress/raw/main/house-votes-84.data" |
|
} |
|
features_types_per_config = { |
|
"voting": { |
|
"pro_handicapped_infants": datasets.Value("bool"), |
|
"pro_water_project_cost_sharing": datasets.Value("bool"), |
|
"pro_adoption_of_the_budget_resolution": datasets.Value("bool"), |
|
"pro_physician_fee_freeze": datasets.Value("bool"), |
|
"pro_el_salvador_aid": datasets.Value("bool"), |
|
"pro_religious_groups_in_schools": datasets.Value("bool"), |
|
"pro_anti_satellite_test_ban": datasets.Value("bool"), |
|
"pro_aid_to_nicaraguan_contras": datasets.Value("bool"), |
|
"pro_mx_missile": datasets.Value("bool"), |
|
"pro_immigration": datasets.Value("bool"), |
|
"pro_synfuels_corporation_cutback": datasets.Value("bool"), |
|
"pro_education_spending": datasets.Value("bool"), |
|
"pro_superfund_right_to_sue": datasets.Value("bool"), |
|
"pro_crime": datasets.Value("bool"), |
|
"pro_duty_free_exports": datasets.Value("bool"), |
|
"pro_export_administration_act_south_africa": datasets.Value("bool"), |
|
"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): |
|
|
|
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 |
|
|