|
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}} |
|
}""" |
|
|
|
|
|
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): |
|
|
|
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 |
|
|