"""Bank Dataset""" from typing import List import datasets import pandas VERSION = datasets.Version("1.0.0") _ORIGINAL_FEATURE_NAMES = [ "age", "job", "marital", "education", "default", "balance", "housing", "loan", "contact", "day", "month", "duration", "campaign", "pdays", "previous", "poutcome", "y" ] _BASE_FEATURE_NAMES = [ "age", "job", "marital_status", "education", "has_defaulted", "account_balance", "has_housing_loan", "has_personal_loan", "month_of_last_contact", "number_of_calls_in_ad_campaign", "days_since_last_contact_of_previous_campaign", "number_of_calls_before_this_campaign", "successfull_subscription" ] DESCRIPTION = "Bank dataset for subscription prediction." _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/bank+marketing" _URLS = ("https://huggingface.co./datasets/mstz/bank/raw/main/bank-full.csv") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://huggingface.co./datasets/mstz/bank/raw/main/bank-full.csv", } features_types_per_config = { "encoding": { "feature": datasets.Value("string"), "original_value": datasets.Value("string"), "encoded_value": datasets.Value("int8"), }, "subscription": { "age": datasets.Value("int64"), "job": datasets.Value("string"), "marital_status": datasets.Value("string"), "education": datasets.Value("int8"), "has_defaulted": datasets.Value("int8"), "account_balance": datasets.Value("int64"), "has_housing_loan": datasets.Value("int8"), "has_personal_loan": datasets.Value("int8"), "month_of_last_contact": datasets.Value("string"), "number_of_calls_in_ad_campaign": datasets.Value("string"), "days_since_last_contact_of_previous_campaign": datasets.Value("int16"), "number_of_calls_before_this_campaign": datasets.Value("int16"), "successfull_subscription": 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 BankConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(BankConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Bank(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "subscription" BUILDER_CONFIGS = [ BankConfig(name="encoding", description="Encoding dictionaries for discrete features."), BankConfig(name="subscription", description="Bank binary classification for client subscription."), ] 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): if self.config.name == "encoding": return self.encoding_dictionaries() data = pandas.read_csv(filepath, sep=";") 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 = "income") -> pandas.DataFrame: data.drop("day", axis="columns", inplace=True) data.drop("contact", axis="columns", inplace=True) data.drop("duration", axis="columns", inplace=True) data.drop("poutcome", axis="columns", inplace=True) # discretize features data.loc[:, "education"] = data.education.apply(self.encode_education) data.loc[:, "loan"] = data.loan.apply(self.encode_yes_no) data.loc[:, "housing"] = data.housing.apply(self.encode_yes_no) data.loc[:, "default"] = data.default.apply(self.encode_yes_no) data.columns = _BASE_FEATURE_NAMES data.loc[:, "successfull_subscription"] = data.successfull_subscription.apply(lambda x: 0 if x == "no" else 1) for f in _BASE_FEATURE_NAMES: print(f, data[f].max(), data.dtypes[f]) if config == "subscription": return data else: raise ValueError(f"Unknown config: {config}") def encoding_dictionaries(self): education_dic, binary_dic = self.education_encoding_dic(), self.binary_encoding_dic() education_data = [("education", education, code) for education, code in education_dic.items()] loan_data = [("loan", loan, code) for loan, code in binary_dic.items()] housing_data = [("housing", housing, code) for housing, code in binary_dic.items()] default_data = [("default", default, code) for default, code in binary_dic.items()] data = pandas.DataFrame(education_data, loan_data + housing_data + default_data, columns=["feature", "original_value", "encoded_value"]) return data def encode_education(self, education): return self.education_encoding_dic()[education] def decode_education(self, code): return self.education_decoding_dic()[code] def education_decoding_dic(self): return { 0: "unknown", 1: "primary", 2: "secondary", 3: "tertiary" } def education_encoding_dic(self): return { "unknown": 0, "primary": 1, "secondary": 2, "tertiary": 3 } def encode_yes_no(self, yes_no): return self.yes_no_encoding_dic()[yes_no] def decode_yes_no(self, code): return self.yes_no_decoding_dic()[code] def yes_no_decoding_dic(self): return { 0: "no", 1: "yes" } def yes_no_encoding_dic(self): return { "no": 0, "yes": 1 }