Datasets:
File size: 4,428 Bytes
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 122 123 |
"""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}}
}"""
# Dataset info
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):
# 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
|