mstz commited on
Commit
88f4676
·
1 Parent(s): 12f6038

Upload bank.py

Browse files
Files changed (1) hide show
  1. bank.py +197 -0
bank.py ADDED
@@ -0,0 +1,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("int64"),
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
+ if self.config.name == "encoding":
117
+ data = self.encoding_dictionaries()
118
+ else:
119
+ data = pandas.read_csv(filepath, sep=";")
120
+ data = self.preprocess(data, config=self.config.name)
121
+
122
+ for row_id, row in data.iterrows():
123
+ data_row = dict(row)
124
+
125
+ yield row_id, data_row
126
+
127
+ def preprocess(self, data: pandas.DataFrame, config: str = "subscription") -> pandas.DataFrame:
128
+ data.drop("day", axis="columns", inplace=True)
129
+ data.drop("contact", axis="columns", inplace=True)
130
+ data.drop("duration", axis="columns", inplace=True)
131
+ data.drop("poutcome", axis="columns", inplace=True)
132
+
133
+ # discretize features
134
+ data.loc[:, "education"] = data.education.apply(self.encode_education)
135
+ data.loc[:, "loan"] = data.loan.apply(self.encode_yes_no)
136
+ data.loc[:, "housing"] = data.housing.apply(self.encode_yes_no)
137
+ data.loc[:, "default"] = data.default.apply(self.encode_yes_no)
138
+
139
+ data.columns = _BASE_FEATURE_NAMES
140
+
141
+ data.loc[:, "successfull_subscription"] = data.successfull_subscription.apply(lambda x: 0 if x == "no" else 1)
142
+
143
+ if config == "subscription":
144
+ return data
145
+ else:
146
+ raise ValueError(f"Unknown config: {config}")
147
+
148
+ def encoding_dictionaries(self):
149
+ education_dic, yes_no_dic = self.education_encoding_dic(), self.yes_no_encoding_dic()
150
+ education_data = [("education", education, code) for education, code in education_dic.items()]
151
+ loan_data = [("loan", loan, code) for loan, code in yes_no_dic.items()]
152
+ housing_data = [("housing", housing, code) for housing, code in yes_no_dic.items()]
153
+ default_data = [("default", default, code) for default, code in yes_no_dic.items()]
154
+ data = pandas.DataFrame(education_data + loan_data + housing_data + default_data,
155
+ columns=["feature", "original_value", "encoded_value"])
156
+
157
+ return data
158
+
159
+ def encode_education(self, education):
160
+ return self.education_encoding_dic()[education]
161
+
162
+ def decode_education(self, code):
163
+ return self.education_decoding_dic()[code]
164
+
165
+ def education_decoding_dic(self):
166
+ return {
167
+ 0: "unknown",
168
+ 1: "primary",
169
+ 2: "secondary",
170
+ 3: "tertiary"
171
+ }
172
+
173
+ def education_encoding_dic(self):
174
+ return {
175
+ "unknown": 0,
176
+ "primary": 1,
177
+ "secondary": 2,
178
+ "tertiary": 3
179
+ }
180
+
181
+ def encode_yes_no(self, yes_no):
182
+ return self.yes_no_encoding_dic()[yes_no]
183
+
184
+ def decode_yes_no(self, code):
185
+ return self.yes_no_decoding_dic()[code]
186
+
187
+ def yes_no_decoding_dic(self):
188
+ return {
189
+ 0: "no",
190
+ 1: "yes"
191
+ }
192
+
193
+ def yes_no_encoding_dic(self):
194
+ return {
195
+ "no": 0,
196
+ "yes": 1
197
+ }