mstz commited on
Commit
e4376c6
1 Parent(s): 38cc945

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +18 -0
  2. bank.py +194 -0
README.md ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ tags:
5
+ - compas
6
+ - tabular_classification
7
+ - binary_classification
8
+ pretty_name: Bank
9
+ size_categories:
10
+ - 1K<n<10K
11
+ task_categories: # Full list at https://github.com/huggingface/hub-docs/blob/main/js/src/lib/interfaces/Types.ts
12
+ - tabular-classification
13
+ configs:
14
+ - encoding
15
+ - subscription
16
+ ---
17
+ # Bank
18
+ The [Bank dataset](https://archive.ics.uci.edu/ml/datasets/bank+marketing) is cool.
bank.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Bank Dataset"""
2
+
3
+ from typing import List
4
+
5
+ import datasets
6
+
7
+ import pandas
8
+
9
+
10
+ VERSION = datasets.Version("1.0.0")
11
+ _ORIGINAL_FEATURE_NAMES = [
12
+ "age",
13
+ "job",
14
+ "marital",
15
+ "education",
16
+ "default",
17
+ "balance",
18
+ "housing",
19
+ "loan",
20
+ "contact",
21
+ "day",
22
+ "month",
23
+ "duration",
24
+ "campaign",
25
+ "pdays",
26
+ "previous",
27
+ "poutcome",
28
+ "y"
29
+ ]
30
+ _BASE_FEATURE_NAMES = [
31
+ "age",
32
+ "job",
33
+ "marital_status",
34
+ "education",
35
+ "has_defaulted",
36
+ "account_balance",
37
+ "has_housing_loan",
38
+ "has_personal_loan",
39
+ "month_of_last_contact",
40
+ "number_of_calls_in_ad_campaign",
41
+ "days_since_last_contact_of_previous_campaign",
42
+ "number_of_calls_before_this_campaign",
43
+ "successfull_subscription"
44
+ ]
45
+
46
+ DESCRIPTION = "Bank dataset for subscription prediction."
47
+ _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/bank+marketing"
48
+ _URLS = ("https://huggingface.co/datasets/mstz/bank/raw/main/bank-full.csv")
49
+ _CITATION = """"""
50
+
51
+ # Dataset info
52
+ urls_per_split = {
53
+ "train": "https://huggingface.co/datasets/mstz/bank/raw/main/bank-full.csv",
54
+ }
55
+ features_types_per_config = {
56
+ "encoding": {
57
+ "feature": datasets.Value("string"),
58
+ "original_value": datasets.Value("string"),
59
+ "encoded_value": datasets.Value("int8"),
60
+ },
61
+
62
+ "subscription": {
63
+ "age": datasets.Value("int64"),
64
+ "job": datasets.Value("string"),
65
+ "marital_status": datasets.Value("string"),
66
+ "education": datasets.Value("int8"),
67
+ "has_defaulted": datasets.Value("int8"),
68
+ "account_balance": datasets.Value("int16"),
69
+ "has_housing_loan": datasets.Value("int8"),
70
+ "has_personal_loan": datasets.Value("int8"),
71
+ "month_of_last_contact": datasets.Value("string"),
72
+ "number_of_calls_in_ad_campaign": datasets.Value("string"),
73
+ "days_since_last_contact_of_previous_campaign": datasets.Value("int16"),
74
+ "number_of_calls_before_this_campaign": datasets.Value("int16"),
75
+ "successfull_subscription": datasets.ClassLabel(num_classes=2, names=("no", "yes")),
76
+ }
77
+
78
+ }
79
+ features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}
80
+
81
+
82
+ class BankConfig(datasets.BuilderConfig):
83
+ def __init__(self, **kwargs):
84
+ super(BankConfig, self).__init__(version=VERSION, **kwargs)
85
+ self.features = features_per_config[kwargs["name"]]
86
+
87
+
88
+ class Bank(datasets.GeneratorBasedBuilder):
89
+ # dataset versions
90
+ DEFAULT_CONFIG = "subscription"
91
+ BUILDER_CONFIGS = [
92
+ BankConfig(name="encoding",
93
+ description="Encoding dictionaries for discrete features."),
94
+ BankConfig(name="subscription",
95
+ description="Bank binary classification for client subscription."),
96
+ ]
97
+
98
+
99
+ def _info(self):
100
+ if self.config.name not in features_per_config:
101
+ raise ValueError(f"Unknown configuration: {self.config.name}")
102
+
103
+ info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
104
+ features=features_per_config[self.config.name])
105
+
106
+ return info
107
+
108
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
109
+ downloads = dl_manager.download_and_extract(urls_per_split)
110
+
111
+ return [
112
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
113
+ ]
114
+
115
+ def _generate_examples(self, filepath: str):
116
+ data = pandas.read_csv(filepath, sep=";")
117
+ data = self.preprocess(data, config=self.config.name)
118
+
119
+ for row_id, row in data.iterrows():
120
+ data_row = dict(row)
121
+
122
+ yield row_id, data_row
123
+
124
+ def preprocess(self, data: pandas.DataFrame, config: str = "income") -> pandas.DataFrame:
125
+ data.drop("day", axis="columns", inplace=True)
126
+ data.drop("contact", axis="columns", inplace=True)
127
+ data.drop("duration", axis="columns", inplace=True)
128
+ data.drop("poutcome", axis="columns", inplace=True)
129
+
130
+ data.columns = _BASE_FEATURE_NAMES
131
+
132
+ # discretize features
133
+ data.loc[:, "education"] = data.education.apply(self.encode_education)
134
+ data.loc[:, "loan"] = data.loan.apply(self.encode_yes_no)
135
+ data.loc[:, "housing"] = data.housing.apply(self.encode_yes_no)
136
+ data.loc[:, "default"] = data.default.apply(self.encode_yes_no)
137
+
138
+ if config == "encoding":
139
+ return self.encoding_dictionaries()
140
+ elif config == "subscription":
141
+ return data
142
+ else:
143
+ raise ValueError(f"Unknown config: {config}")
144
+
145
+ def encoding_dictionaries(self):
146
+ education_dic, binary_dic = self.education_encoding_dic(), self.binary_encoding_dic()
147
+ education_data = [("education", education, code) for education, code in education_dic.items()]
148
+ loan_data = [("loan", loan, code) for loan, code in binary_dic.items()]
149
+ housing_data = [("housing", housing, code) for housing, code in binary_dic.items()]
150
+ default_data = [("default", default, code) for default, code in binary_dic.items()]
151
+ data = pandas.DataFrame(education_data, loan_data + housing_data + default_data,
152
+ columns=["feature", "original_value", "encoded_value"])
153
+
154
+ return data
155
+
156
+ def encode_education(self, education):
157
+ return self.education_encoding_dic()[education]
158
+
159
+ def decode_education(self, code):
160
+ return self.education_decoding_dic()[code]
161
+
162
+ def education_decoding_dic(self):
163
+ return {
164
+ 0: "unknown",
165
+ 1: "primary",
166
+ 2: "secondary",
167
+ 3: "tertiary"
168
+ }
169
+
170
+ def education_encoding_dic(self):
171
+ return {
172
+ "unknown": 0,
173
+ "primary": 1,
174
+ "secondary": 2,
175
+ "tertiary": 3
176
+ }
177
+
178
+ def encode_yes_no(self, yes_no):
179
+ return self.yes_no_encoding_dic()[yes_no]
180
+
181
+ def decode_yes_no(self, code):
182
+ return self.yes_no_decoding_dic()[code]
183
+
184
+ def yes_no_decoding_dic(self):
185
+ return {
186
+ 0: "no",
187
+ 1: "yes"
188
+ }
189
+
190
+ def yes_no_encoding_dic(self):
191
+ return {
192
+ "no": 0,
193
+ "yes": 1
194
+ }