"""Pums Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ENCODING_DICS = { "class": { "- 50000.": 0, "50000+.": 1 } } _BASE_FEATURE_NAMES = [ "age", "class_of_worker", "detailed_industry_recode", "detailed_occupation_recode", "education", "wage_per_hour", "enroll_in_edu_inst_last_wk", "marital_stat", "major_industry_code", "major_occupation_code", "race", "hispanic_origin", "sex", "member_of_a_labor_union", "reason_for_unemployment", "full_or_part_time_employment_stat", "capital_gains", "capital_losses", "dividends_from_stocks", "tax_filer_stat", "region_of_previous_residence", "state_of_previous_residence", "detailed_household_and_family_stat", "detailed_household_summary_in_household", "migration_code_change_in_msa", "migration_code_change_in_reg", "migration_code_move_within_reg", "live_in_this_house_1_year_ago", "migration_prev_res_in_sunbelt", "num_persons_worked_for_employer", "family_members_under_18", "country_of_birth_father", "country_of_birth_mother", "country_of_birth_self", "citizenship", "own_business_or_self_employed", "fill_inc_questionnaire_for_veteran_admin", "veterans_benefits", "weeks_worked_in_year", "year", "class", ] DESCRIPTION = "Pums dataset." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/116/us+census+data+1990" _URLS = ("https://archive-beta.ics.uci.edu/dataset/116/us+census+data+1990") _CITATION = """ @misc{misc_us_census_data_(1990)_116, author = {Meek,Meek, Thiesson,Thiesson & Heckerman,Heckerman}, title = {{US Census Data (1990)}}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C5VP42}} } """ # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/pums/resolve/main/pums.csv" } features_types_per_config = { "pums": { "age": datasets.Value("int64"), "class_of_worker": datasets.Value("string"), "detailed_industry_recode": datasets.Value("string"), "detailed_occupation_recode": datasets.Value("string"), "education": datasets.Value("string"), "wage_per_hour": datasets.Value("int64"), "enroll_in_edu_inst_last_wk": datasets.Value("string"), "marital_stat": datasets.Value("string"), "major_industry_code": datasets.Value("string"), "major_occupation_code": datasets.Value("string"), "race": datasets.Value("string"), "hispanic_origin": datasets.Value("string"), "sex": datasets.Value("string"), "member_of_a_labor_union": datasets.Value("string"), "reason_for_unemployment": datasets.Value("string"), "full_or_part_time_employment_stat": datasets.Value("string"), "capital_gains": datasets.Value("int64"), "capital_losses": datasets.Value("int64"), "dividends_from_stocks": datasets.Value("int64"), "tax_filer_stat": datasets.Value("string"), "region_of_previous_residence": datasets.Value("string"), "state_of_previous_residence": datasets.Value("string"), "detailed_household_and_family_stat": datasets.Value("string"), "detailed_household_summary_in_household": datasets.Value("string"), "migration_code_change_in_msa": datasets.Value("string"), "migration_code_change_in_reg": datasets.Value("string"), "migration_code_move_within_reg": datasets.Value("string"), "live_in_this_house_1_year_ago": datasets.Value("string"), "migration_prev_res_in_sunbelt": datasets.Value("string"), "num_persons_worked_for_employer": datasets.Value("int64"), "family_members_under_18": datasets.Value("string"), "country_of_birth_father": datasets.Value("string"), "country_of_birth_mother": datasets.Value("string"), "country_of_birth_self": datasets.Value("string"), "citizenship": datasets.Value("string"), "own_business_or_self_employed": datasets.Value("int64"), "fill_inc_questionnaire_for_veteran_admin": datasets.Value("string"), "veterans_benefits": datasets.Value("int64"), "weeks_worked_in_year": datasets.Value("int64"), "year": datasets.Value("int64"), "class": datasets.ClassLabel(num_classes=2) } } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class PumsConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(PumsConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Pums(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "pums" BUILDER_CONFIGS = [PumsConfig(name="pums", description="Pums for binary classification.")] 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) for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame: data.columns = _BASE_FEATURE_NAMES for feature in _ENCODING_DICS: encoding_function = partial(self.encode, feature) data.loc[:, feature] = data[feature].apply(encoding_function) data = data.rename(columns={"instance migration_code_change_in_msa": "migration_code_change_in_msa"}) return data[list(features_types_per_config[self.config.name].keys())] def encode(self, feature, value): if feature in _ENCODING_DICS: return _ENCODING_DICS[feature][value] raise ValueError(f"Unknown feature: {feature}")