Datasets:
File size: 6,560 Bytes
e4376c6 ab016e7 e4376c6 53a1e74 14ea993 e4376c6 2fe278c 1dcaebd 7e18e50 3ad4a79 7e18e50 3449c96 e4376c6 a5cc67d e4376c6 a5cc67d f17de41 e4376c6 14ea993 e4376c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
"""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":
data = self.encoding_dictionaries()
else:
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, yes_no_dic = self.education_encoding_dic(), self.yes_no_encoding_dic()
education_data = [("education", education, code) for education, code in education_dic.items()]
loan_data = [("loan", loan, code) for loan, code in yes_no_dic.items()]
housing_data = [("housing", housing, code) for housing, code in yes_no_dic.items()]
default_data = [("default", default, code) for default, code in yes_no_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
}
|