|
"""Uscensus Dataset""" |
|
|
|
from typing import List |
|
from functools import partial |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
_ENCODING_DICS = {} |
|
_BASE_FEATURE_NAMES = [ |
|
"caseid", |
|
"dAge", |
|
"dAncstry1", |
|
"dAncstry2", |
|
"iAvail", |
|
"iCitizen", |
|
"iClass", |
|
"dDepart", |
|
"iDisabl1", |
|
"iDisabl2", |
|
"iEnglish", |
|
"iFeb55", |
|
"iFertil", |
|
"dHispanic", |
|
"dHour89", |
|
"dHours", |
|
"iImmigr", |
|
"dIncome1", |
|
"dIncome2", |
|
"dIncome3", |
|
"dIncome4", |
|
"dIncome5", |
|
"dIncome6", |
|
"dIncome7", |
|
"dIncome8", |
|
"dIndustry", |
|
"iKorean", |
|
"iLang1", |
|
"iLooking", |
|
"iMarital", |
|
"iMay75880", |
|
"iMeans", |
|
"iMilitary", |
|
"iMobility", |
|
"iMobillim", |
|
"dOccup", |
|
"iOthrserv", |
|
"iPerscare", |
|
"dPOB", |
|
"dPoverty", |
|
"dPwgt1", |
|
"iRagechld", |
|
"dRearning", |
|
"iRelat1", |
|
"iRelat2", |
|
"iRemplpar", |
|
"iRiders", |
|
"iRlabor", |
|
"iRownchld", |
|
"dRpincome", |
|
"iRPOB", |
|
"iRrelchld", |
|
"iRspouse", |
|
"iRvetserv", |
|
"iSchool", |
|
"iSept80", |
|
"iSex", |
|
"iSubfam1", |
|
"iSubfam2", |
|
"iTmpabsnt", |
|
"dTravtime", |
|
"iVietnam", |
|
"dWeek89", |
|
"iWork89", |
|
"iWorklwk", |
|
"iWWII", |
|
"iYearsch", |
|
"iYearwrk", |
|
"dYrsserv", |
|
] |
|
|
|
DESCRIPTION = "Uscensus dataset." |
|
_HOMEPAGE = "" |
|
_URLS = ("") |
|
_CITATION = """""" |
|
|
|
|
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/uscensus/resolve/main/uscensus1990.csv" |
|
} |
|
features_types_per_config = { |
|
"uscensus": { |
|
"dAge": datasets.Value("int64"), |
|
"dAncstry1": datasets.Value("int64"), |
|
"dAncstry2": datasets.Value("int64"), |
|
"iAvail": datasets.Value("int64"), |
|
"iCitizen": datasets.Value("int64"), |
|
"iClass": datasets.Value("int64"), |
|
"dDepart": datasets.Value("int64"), |
|
"iDisabl1": datasets.Value("int64"), |
|
"iDisabl2": datasets.Value("int64"), |
|
"iEnglish": datasets.Value("int64"), |
|
"iFeb55": datasets.Value("int64"), |
|
"iFertil": datasets.Value("int64"), |
|
"dHispanic": datasets.Value("int64"), |
|
"dHour89": datasets.Value("int64"), |
|
"dHours": datasets.Value("int64"), |
|
"iImmigr": datasets.Value("int64"), |
|
"dIncome1": datasets.Value("int64"), |
|
"dIncome2": datasets.Value("int64"), |
|
"dIncome3": datasets.Value("int64"), |
|
"dIncome4": datasets.Value("int64"), |
|
"dIncome5": datasets.Value("int64"), |
|
"dIncome6": datasets.Value("int64"), |
|
"dIncome7": datasets.Value("int64"), |
|
"dIncome8": datasets.Value("int64"), |
|
"dIndustry": datasets.Value("int64"), |
|
"iKorean": datasets.Value("int64"), |
|
"iLang1": datasets.Value("int64"), |
|
"iLooking": datasets.Value("int64"), |
|
"iMarital": datasets.Value("int64"), |
|
"iMay75880": datasets.Value("int64"), |
|
"iMeans": datasets.Value("int64"), |
|
"iMilitary": datasets.Value("int64"), |
|
"iMobility": datasets.Value("int64"), |
|
"iMobillim": datasets.Value("int64"), |
|
"dOccup": datasets.Value("int64"), |
|
"iOthrserv": datasets.Value("int64"), |
|
"iPerscare": datasets.Value("int64"), |
|
"dPOB": datasets.Value("int64"), |
|
"dPoverty": datasets.Value("int64"), |
|
"dPwgt1": datasets.Value("int64"), |
|
"iRagechld": datasets.Value("int64"), |
|
"dRearning": datasets.Value("int64"), |
|
"iRelat1": datasets.Value("int64"), |
|
"iRelat2": datasets.Value("int64"), |
|
"iRemplpar": datasets.Value("int64"), |
|
"iRiders": datasets.Value("int64"), |
|
"iRlabor": datasets.Value("int64"), |
|
"iRownchld": datasets.Value("int64"), |
|
"dRpincome": datasets.Value("int64"), |
|
"iRPOB": datasets.Value("int64"), |
|
"iRrelchld": datasets.Value("int64"), |
|
"iRspouse": datasets.Value("int64"), |
|
"iRvetserv": datasets.Value("int64"), |
|
"iSchool": datasets.Value("int64"), |
|
"iSept80": datasets.Value("int64"), |
|
"iSex": datasets.Value("int64"), |
|
"iSubfam1": datasets.Value("int64"), |
|
"iSubfam2": datasets.Value("int64"), |
|
"iTmpabsnt": datasets.Value("int64"), |
|
"dTravtime": datasets.Value("int64"), |
|
"iVietnam": datasets.Value("int64"), |
|
"dWeek89": datasets.Value("int64"), |
|
"iWork89": datasets.Value("int64"), |
|
"iWorklwk": datasets.Value("int64"), |
|
"iWWII": datasets.Value("int64"), |
|
"iYearsch": datasets.Value("int64"), |
|
"iYearwrk": datasets.Value("int64"), |
|
"dYrsserv": datasets.ClassLabel(num_classes=3) |
|
} |
|
} |
|
|
|
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} |
|
|
|
|
|
class UscensusConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(UscensusConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Uscensus(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "uscensus" |
|
BUILDER_CONFIGS = [UscensusConfig(name="uscensus", description="Uscensus 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[feature] = data[feature].apply(encoding_function) |
|
|
|
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}") |
|
|