"""Ipums 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 } } DESCRIPTION = "Ipums dataset." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/127/ipums+census+database" _URLS = ("https://archive-beta.ics.uci.edu/dataset/127/ipums+census+database") _CITATION = """ @misc{misc_ipums_census_database_127, author = {Ruggles,Steven & Sobek,Matthew}, title = {{IPUMS Census Database}}, year = {1999}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C5BG63}} } """ # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/ipums/resolve/main/ipums.csv" } features_types_per_config = { "ipums": { "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"), # "instance_weight": datasets.Value("int64"), "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("string"), "fill_inc_questionnaire_for_veteran_admin": datasets.Value("string"), "veterans_benefits": datasets.Value("string"), "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 IpumsConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(IpumsConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Ipums(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "ipums" BUILDER_CONFIGS = [IpumsConfig(name="ipums", description="Ipums 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): print("\n\n\nfilepath") print(filepath) data = pandas.read_csv(filepath) 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: for feature in _ENCODING_DICS: encoding_function = partial(self.encode, feature) data.loc[:, feature] = data[feature].apply(encoding_function) data.drop("instance_weight", axis="columns", inplace=True) 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}")