|
"""Breast Dataset""" |
|
|
|
from typing import List |
|
|
|
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" |
|
] |
|
|
|
DESCRIPTION = "Breast 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 = """ |
|
|
|
""" |
|
|
|
|
|
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("int8"), |
|
"current_savings": datasets.Value("int8"), |
|
"employed_since": datasets.Value("int8"), |
|
"installment_rate_percentage": datasets.Value("int8"), |
|
"sex": datasets.Value("int8"), |
|
"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("int8"), |
|
"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 BreastConfig(datasets.BuilderConfig): |
|
def __init__(self, **kwargs): |
|
super(BreastConfig, self).__init__(version=VERSION, **kwargs) |
|
self.features = features_per_config[kwargs["name"]] |
|
|
|
|
|
class Breast(datasets.GeneratorBasedBuilder): |
|
|
|
DEFAULT_CONFIG = "loan" |
|
BUILDER_CONFIGS = [ |
|
BreastConfig(name="encoding", |
|
description="Encoding dictionaries for discrete features."), |
|
BreastConfig(name="loan", |
|
description="Binary classification of loan approval."), |
|
] |
|
|
|
|
|
def _info(self): |
|
if self.config.name not in features_per_config: |
|
raise ValueError(f"Unknown configuration: {self.config.name}") |
|
|
|
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, 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 = "cancer") -> pandas.DataFrame: |
|
data.loc[:, "checking_account_status"] = data.checking_account_status.apply(self.encode_checking_account_status) |
|
data.loc[:, "credit_status"] = data.credit_status.apply(self.encode_credit_status) |
|
data.loc[:, "current_savings"] = data.current_savings.apply(self.encode_current_savings) |
|
data.loc[:, "employed_since"] = data.employed_since.apply(self.encode_employed_since) |
|
data.loc[:, "guarantors"] = data.guarantors.apply(self.encode_guarantors) |
|
data.loc[:, "has_registered_phone_number"] = data.has_registered_phone_number.apply(self.encode_has_registered_phone_number) |
|
data.loc[:, "housing_status"] = data.housing_status.apply(self.encode_housing_status) |
|
data.loc[:, "installment_plans"] = data.installment_plans.apply(self.encode_installment_plans) |
|
data.loc[:, "is_foreign"] = data.is_foreign.apply(self.encode_is_foreign) |
|
data.loc[:, "job_status"] = data.job_status.apply(self.encode_job_status) |
|
data.loc[:, "loan_granted"] = data.loan_granted.apply(self.encode_loan_granted) |
|
data.loc[:, "marital_status"] = data.marital_status.apply(self.encode_marital_status) |
|
data.loc[:, "sex"] = data.sex.apply(self.encode_sex) |
|
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] |
|
|
|
if config == "loan": |
|
return data |
|
else: |
|
raise ValueError(f"Unknown config: {config}") |
|
|
|
def encode_checking_account_status(self, status): |
|
return self.checking_account_status_encode_dic()[status] |
|
|
|
def checking_account_status_encode_dic(self): |
|
return { |
|
"A14": 0, |
|
"A11": 1, |
|
"A12": 2, |
|
"A13": 3, |
|
} |
|
|
|
def decode_checking_account_status(self, code): |
|
return self.checking_account_status_decode_dic()[code] |
|
|
|
def checking_account_status_decode_dic(self): |
|
return { |
|
0: "A14", |
|
1: "A11", |
|
2: "A12", |
|
3: "A13", |
|
} |
|
|
|
def encode_credit_status(self, status): |
|
return self.credit_status_encode_dic()[status] |
|
|
|
def credit_status_encode_dic(self): |
|
return { |
|
"A30": 0, |
|
"A31": 1, |
|
"A32": 2, |
|
"A33": 3, |
|
"A34": 4, |
|
} |
|
|
|
def decode_credit_status(self, code): |
|
return self.credit_status_decode_dic()[code] |
|
|
|
def credit_status_decode_dic(self): |
|
return { |
|
0: "A30", |
|
1: "A31", |
|
2: "A32", |
|
3: "A33", |
|
4: "A34", |
|
} |
|
|
|
def encode_current_savings(self, status): |
|
return self.current_savings_encode_dic()[status] |
|
|
|
def current_savings_encode_dic(self): |
|
return { |
|
"A65": 0, |
|
"A61": 1, |
|
"A62": 2, |
|
"A63": 3, |
|
"A64": 4, |
|
} |
|
|
|
def decode_current_savings(self, code): |
|
return self.current_savings_decode_dic()[code] |
|
|
|
def current_savings_decode_dic(self): |
|
return { |
|
0: "A65", |
|
1: "A61", |
|
2: "A62", |
|
3: "A63", |
|
4: "A64", |
|
} |
|
|
|
def encode_employed_since(self, status): |
|
return self.employed_since_encode_dic()[status] |
|
|
|
def employed_since_encode_dic(self): |
|
return { |
|
"A71": 0, |
|
"A72": 1, |
|
"A73": 2, |
|
"A74": 3, |
|
"A75": 4, |
|
} |
|
|
|
def decode_employed_since(self, code): |
|
return self.employed_since_decode_dic()[code] |
|
|
|
def employed_since_decode_dic(self): |
|
return { |
|
0: "A71", |
|
1: "A72", |
|
2: "A73", |
|
3: "A74", |
|
4: "A75", |
|
} |
|
|
|
def encode_sex(self, status): |
|
return self.sex_encode_dic()[status] |
|
|
|
def sex_encode_dic(self): |
|
return { |
|
"A91": 0, |
|
"A93": 0, |
|
"A94": 0, |
|
"A92": 1, |
|
"A95": 1, |
|
} |
|
|
|
def decode_sex(self, code): |
|
return self.sex_decode_dic()[code] |
|
|
|
def sex_decode_dic(self): |
|
return { |
|
0: "A91", |
|
1: "A92", |
|
} |
|
|
|
def encode_marital_status(self, status): |
|
return self.marital_status_encode_dic()[status] |
|
|
|
def marital_status_encode_dic(self): |
|
return { |
|
"A91": 0, |
|
"A92": 0, |
|
"A93": 1, |
|
"A94": 2, |
|
"A95": 1, |
|
} |
|
|
|
def decode_marital_status(self, code): |
|
return self.marital_status_decode_dic()[code] |
|
|
|
def marital_status_decode_dic(self): |
|
return { |
|
0: "A91", |
|
1: "A93", |
|
2: "A94", |
|
} |
|
|
|
def encode_guarantors(self, status): |
|
return self.guarantors_encode_dic()[status] |
|
|
|
def guarantors_encode_dic(self): |
|
return { |
|
"A101": 0, |
|
"A102": 1, |
|
"A103": 2, |
|
} |
|
|
|
def decode_guarantors(self, code): |
|
return self.guarantors_decode_dic()[code] |
|
|
|
def guarantors_decode_dic(self): |
|
return { |
|
0: "A101", |
|
1: "A102", |
|
2: "A103", |
|
} |
|
|
|
def encode_installment_plans(self, status): |
|
return self.installment_plans_encode_dic()[status] |
|
|
|
def installment_plans_encode_dic(self): |
|
return { |
|
"A141": 0, |
|
"A142": 1, |
|
"A143": 2, |
|
} |
|
|
|
def decode_installment_plans(self, code): |
|
return self.installment_plans_decode_dic()[code] |
|
|
|
def installment_plans_decode_dic(self): |
|
return { |
|
0: "A141", |
|
1: "A142", |
|
2: "A143", |
|
} |
|
|
|
def encode_housing_status(self, status): |
|
return self.housing_status_encode_dic()[status] |
|
|
|
def housing_status_encode_dic(self): |
|
return { |
|
"A153": 0, |
|
"A151": 1, |
|
"A152": 2, |
|
} |
|
|
|
def decode_housing_status(self, code): |
|
return self.housing_status_decode_dic()[code] |
|
|
|
def housing_status_decode_dic(self): |
|
return { |
|
0: "A153", |
|
1: "A151", |
|
2: "A152", |
|
} |
|
|
|
def encode_job_status(self, status): |
|
return self.job_status_encode_dic()[status] |
|
|
|
def job_status_encode_dic(self): |
|
return { |
|
"A171": 0, |
|
"A172": 1, |
|
"A173": 2, |
|
"A174": 3, |
|
} |
|
|
|
def decode_job_status(self, code): |
|
return self.job_status_decode_dic()[code] |
|
|
|
def job_status_decode_dic(self): |
|
return { |
|
0: "A171", |
|
1: "A172", |
|
2: "A173", |
|
3: "A174", |
|
} |
|
|
|
def encode_has_registered_phone_number(self, status): |
|
return self.has_registered_phone_number_encode_dic()[status] |
|
|
|
def has_registered_phone_number_encode_dic(self): |
|
return { |
|
"A191": 0, |
|
"A192": 1, |
|
} |
|
|
|
def decode_has_registered_phone_number(self, code): |
|
return self.has_registered_phone_number_decode_dic()[code] |
|
|
|
def has_registered_phone_number_decode_dic(self): |
|
return { |
|
0: "A191", |
|
1: "A192", |
|
} |
|
|
|
def encode_is_foreign(self, status): |
|
return self.is_foreign_encode_dic()[status] |
|
|
|
def is_foreign_encode_dic(self): |
|
return { |
|
"A191": 0, |
|
"A192": 1, |
|
} |
|
|
|
def decode_is_foreign(self, code): |
|
return self.is_foreign_decode_dic()[code] |
|
|
|
def is_foreign_decode_dic(self): |
|
return { |
|
0: "A191", |
|
1: "A192", |
|
} |
|
|
|
def encode_loan_granted(self, status): |
|
return self.loan_granted_encode_dic()[status] |
|
|
|
def loan_granted_encode_dic(self): |
|
return { |
|
"A201": 0, |
|
"A202": 1, |
|
} |
|
|
|
def decode_loan_granted(self, code): |
|
return self.loan_granted_decode_dic()[code] |
|
|
|
def loan_granted_decode_dic(self): |
|
return { |
|
0: "A201", |
|
1: "A202", |
|
} |
|
|