"""German Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ORIGINAL_FEATURE_NAMES = [ "checking_account_status", "account_life_in_months", "credit_status", "loan_purpose", "current_credit", "current_savings", "employed_since", "installment_rate_percentage", "personal_status_and_sex", "guarantors", "years_living_in_current_residence", "property", "age", "installment_plans", "housing_status", "nr_credit_accounts_in_bank", "job_status", "number_of_people_in_support", "has_registered_phone_number", "is_foreign", "loan_granted", ] _BASE_FEATURE_NAMES = [ "checking_account_status", "account_life_in_months", "credit_status", "loan_purpose", "current_credit", "current_savings", "employed_since", "installment_rate_percentage", "sex", "marital_status", "guarantors", "years_living_in_current_residence", "age", "installment_plans", "housing_status", "nr_credit_accounts_in_bank", "job_status", "number_of_people_in_support", "has_registered_phone_number", "is_foreign", "loan_granted" ] _ENCODING_DICS = { "is_foreign": { "A201": 0, "A202": 1 }, "has_registered_phone_number": { "A191": 0, "A192": 1 }, "job_status": { "A171": 0, "A172": 1, "A173": 2, "A174": 3 }, "housing_status": { "A153": 0, "A151": 1, "A152": 2 }, "installment_plans": { "A141": 0, "A142": 1, "A143": 2 }, "guarantors": { "A101": 0, "A102": 1, "A103": 2 }, "marital_status": { "A91": 0, "A92": 0, "A93": 1, "A94": 2, "A95": 1, }, "sex": { "A91": 0, "A93": 0, "A94": 0, "A92": 1, "A95": 1, }, "employed_since": { "A71": 0, "A72": 1, "A73": 2, "A74": 3, "A75": 4, }, "current_savings": { "A65": 0, "A61": 1, "A62": 2, "A63": 3, "A64": 4, }, "credit_status": { "A30": 0, "A31": 1, "A32": 2, "A33": 3, "A34": 4, }, "checking_account_status": { "A14": 0, "A11": 1, "A12": 2, "A13": 3, } } DESCRIPTION = "German dataset for cancer prediction." _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Statlog+%28German+Credit+Data%29" _URLS = ("https://archive.ics.uci.edu/ml/datasets/Statlog+%28German+Credit+Data%29") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/german/raw/main/german.data", } features_types_per_config = { "encoding": { "feature": datasets.Value("string"), "original_value": datasets.Value("string"), "encoded_value": datasets.Value("int8"), }, "loan": { "checking_account_status": datasets.Value("int8"), "account_life_in_months": datasets.Value("int8"), "credit_status": datasets.Value("int8"), "loan_purpose": datasets.Value("string"), "current_credit": datasets.Value("int32"), "current_savings": datasets.Value("int8"), "employed_since": datasets.Value("int8"), "installment_rate_percentage": datasets.Value("int8"), "is_male": datasets.Value("bool"), "marital_status": datasets.Value("string"), "guarantors": datasets.Value("int8"), "years_living_in_current_residence": datasets.Value("int8"), "age": datasets.Value("int8"), "installment_plans": datasets.Value("string"), "housing_status": datasets.Value("int8"), "nr_credit_accounts_in_bank": datasets.Value("int8"), "job_status": datasets.Value("int8"), "number_of_people_in_support": datasets.Value("int8"), "has_registered_phone_number": datasets.Value("int8"), "is_foreign": datasets.Value("bool"), "loan_granted": 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} class GermanConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(GermanConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class German(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "loan" BUILDER_CONFIGS = [ GermanConfig(name="encoding", description="Encoding dictionaries for discrete features."), GermanConfig(name="loan", description="Binary classification of loan approval."), ] 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): if self.config.name == "encoding": data = self.encoding_dics() else: data = pandas.read_csv(filepath, sep=" ", header=None) data.columns=_ORIGINAL_FEATURE_NAMES 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 = "loan") -> pandas.DataFrame: for feature in _ENCODING_DICS: if feature not in ["marital_status", "sex"]: encoding_function = partial(self.encode, feature) data.loc[:, feature] = data[feature].apply(encoding_function) encode_marital_status = partial(self.encode, "marital_status") encode_sex = partial(self.encode, "sex") data.loc[:, "marital_status"] = data.personal_status_and_sex.apply(encode_marital_status) data.loc[:, "sex"] = data.personal_status_and_sex.apply(encode_sex) data.loc[:, "loan_purpose"] = data.loan_purpose.apply(self.encode_loan_purpose) data.loc[:, "loan_granted"] = data.loan_granted.apply(lambda x: x - 1) data.drop("personal_status_and_sex", axis="columns", inplace=True) data = data[_BASE_FEATURE_NAMES] data = data.rename(columns={"sex": "is_male"}) data = data.astype({"is_foreign": "bool"}) data.loc[:, "is_foreign"] = data.is_foreign.apply(bool) return data def encode(self, feature, value): if feature in _ENCODING_DICS: return _ENCODING_DICS[feature][value] raise ValueError(f"Unknown feature: {feature}") def encoding_dics(self): data = [pandas.DataFrame([(feature, original, encoded) for original, encoded in d.items()]) for feature, d in _ENCODING_DICS.items()] data = pandas.concat(data, axis="rows").reset_index() data.drop("index", axis="columns", inplace=True) data.columns = ["feature", "original_value", "encoded_value"] return data def encode_loan_purpose(self, code): return { "A40": "new car", "A41": "used car", "A42": "furniture/equipment", "A43": "radio/television", "A44": "domestic appliances", "A45": "repairs", "A46": "education", "A47": "vacation ", "A48": "retraining", "A49": "business", "A410": "others" }[code]