|
from typing import List |
|
from functools import partial |
|
|
|
import datasets |
|
|
|
import pandas |
|
|
|
|
|
VERSION = datasets.Version("1.0.0") |
|
|
|
|
|
DESCRIPTION = "Contraceptive dataset from the UCI repository." |
|
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/30/contraceptive+method+choice" |
|
_URLS = ("https://archive-beta.ics.uci.edu/dataset/30/contraceptive+method+choice") |
|
_CITATION = """ |
|
@misc{misc_contraceptive_method_choice_30, |
|
author = {Lim,Tjen-Sien}, |
|
title = {{Contraceptive Method Choice}}, |
|
year = {1997}, |
|
howpublished = {UCI Machine Learning Repository}, |
|
note = {{DOI}: \\url{10.24432/C59W2D}} |
|
}""" |
|
|
|
|
|
_BASE_FEATURE_NAMES = [ |
|
"age_of_wife", |
|
"education_level_of_wife", |
|
"education_level_of_husband", |
|
"number_of_born_children", |
|
"is_wife_muslim", |
|
"is_wife_a_housekeeper", |
|
"job_of_husband", |
|
"standard_of_living", |
|
"is_media_exposure_negative", |
|
"use_contraceptives" |
|
] |
|
urls_per_split = { |
|
"train": "https://huggingface.co/datasets/mstz/contraceptive/raw/main/cmc.data" |
|
} |
|
features_types_per_config = { |
|
"contraceptive": { |
|
"age_of_wife": datasets.Value("int8"), |
|
"education_level_of_wife": datasets.Value("int8"), |
|
"education_level_of_husband": datasets.Value("int8"), |
|
"number_of_born_children": datasets.Value("int8"), |
|
"is_wife_muslim": datasets.Value("bool"), |
|
"is_wife_a_housekeeper": datasets.Value("bool"), |
|
"job_of_husband": datasets.Value("string"), |
|
"standard_of_living": datasets.Value("int8"), |
|
"is_media_exposure_negative": datasets.Value("bool"), |
|
"use_contraceptives": datasets.ClassLabel(num_classes=2, names=("no", "yes")) |
|
} |
|
|
|
} |
|
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} |
|
|
|
_ENCODING_DICS = { |
|
"use_contraceptives": { |
|
1: 0, |
|
2: 1, |
|
3: 1 |
|
} |
|
} |
|
|
|
class ContraceptiveConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(ContraceptiveConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Contraceptive(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "contraceptive" |
|
BUILDER_CONFIGS = [ |
|
ContraceptiveConfig(name="contraceptive", |
|
description="Contraceptive 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, config: str = DEFAULT_CONFIG) -> 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) |
|
|
|
|
|
return data |
|
|
|
def encode(self, feature, value): |
|
if feature in _ENCODING_DICS: |
|
return _ENCODING_DICS[feature][value] |
|
raise ValueError(f"Unknown feature: {feature}") |
|
|