Datasets:
Upload bank.py
Browse files
bank.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
"""Bank Dataset"""
|
2 |
|
3 |
from typing import List
|
|
|
4 |
|
5 |
import datasets
|
6 |
|
@@ -31,7 +32,7 @@ _BASE_FEATURE_NAMES = [
|
|
31 |
"age",
|
32 |
"job",
|
33 |
"marital_status",
|
34 |
-
"
|
35 |
"has_defaulted",
|
36 |
"account_balance",
|
37 |
"has_housing_loan",
|
@@ -42,6 +43,31 @@ _BASE_FEATURE_NAMES = [
|
|
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"
|
@@ -124,77 +150,33 @@ class Bank(datasets.GeneratorBasedBuilder):
|
|
124 |
|
125 |
yield row_id, data_row
|
126 |
|
127 |
-
def preprocess(self, data: pandas.DataFrame, config: str = "
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
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 |
-
|
144 |
-
|
|
|
|
|
|
|
145 |
|
146 |
-
if config == "subscription":
|
147 |
return data
|
148 |
else:
|
149 |
raise ValueError(f"Unknown config: {config}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
def encoding_dictionaries(self):
|
152 |
-
education_dic, yes_no_dic = self.education_encoding_dic(), self.yes_no_encoding_dic()
|
153 |
-
education_data = [("education", education, code) for education, code in education_dic.items()]
|
154 |
-
loan_data = [("loan", loan, code) for loan, code in yes_no_dic.items()]
|
155 |
-
housing_data = [("housing", housing, code) for housing, code in yes_no_dic.items()]
|
156 |
-
default_data = [("default", default, code) for default, code in yes_no_dic.items()]
|
157 |
-
data = pandas.DataFrame(education_data + loan_data + housing_data + default_data,
|
158 |
-
columns=["feature", "original_value", "encoded_value"])
|
159 |
-
|
160 |
return data
|
161 |
-
|
162 |
-
def encode_education(self, education):
|
163 |
-
return self.education_encoding_dic()[education]
|
164 |
-
|
165 |
-
def decode_education(self, code):
|
166 |
-
return self.education_decoding_dic()[code]
|
167 |
-
|
168 |
-
def education_decoding_dic(self):
|
169 |
-
return {
|
170 |
-
0: "unknown",
|
171 |
-
1: "primary",
|
172 |
-
2: "secondary",
|
173 |
-
3: "tertiary"
|
174 |
-
}
|
175 |
-
|
176 |
-
def education_encoding_dic(self):
|
177 |
-
return {
|
178 |
-
"unknown": 0,
|
179 |
-
"primary": 1,
|
180 |
-
"secondary": 2,
|
181 |
-
"tertiary": 3
|
182 |
-
}
|
183 |
-
|
184 |
-
def encode_yes_no(self, yes_no):
|
185 |
-
return self.yes_no_encoding_dic()[yes_no]
|
186 |
-
|
187 |
-
def decode_yes_no(self, code):
|
188 |
-
return self.yes_no_decoding_dic()[code]
|
189 |
-
|
190 |
-
def yes_no_decoding_dic(self):
|
191 |
-
return {
|
192 |
-
0: "no",
|
193 |
-
1: "yes"
|
194 |
-
}
|
195 |
-
|
196 |
-
def yes_no_encoding_dic(self):
|
197 |
-
return {
|
198 |
-
"no": 0,
|
199 |
-
"yes": 1
|
200 |
-
}
|
|
|
1 |
"""Bank Dataset"""
|
2 |
|
3 |
from typing import List
|
4 |
+
from functools import partial
|
5 |
|
6 |
import datasets
|
7 |
|
|
|
32 |
"age",
|
33 |
"job",
|
34 |
"marital_status",
|
35 |
+
"education_level",
|
36 |
"has_defaulted",
|
37 |
"account_balance",
|
38 |
"has_housing_loan",
|
|
|
43 |
"number_of_calls_before_this_campaign",
|
44 |
"successfull_subscription"
|
45 |
]
|
46 |
+
_ENCODING_DICS = {
|
47 |
+
"education_level": {
|
48 |
+
"unknown": 0,
|
49 |
+
"primary": 1,
|
50 |
+
"secondary": 2,
|
51 |
+
"tertiary": 3
|
52 |
+
},
|
53 |
+
"has_personal_loan": {
|
54 |
+
"no": 0,
|
55 |
+
"yes": 1
|
56 |
+
},
|
57 |
+
"has_housing_loan": {
|
58 |
+
"no": 0,
|
59 |
+
"yes": 1
|
60 |
+
},
|
61 |
+
"has_defaulted": {
|
62 |
+
"no": 0,
|
63 |
+
"yes": 1
|
64 |
+
},
|
65 |
+
"successful_subscription": {
|
66 |
+
"no": 0,
|
67 |
+
"yes": 1
|
68 |
+
}
|
69 |
+
}
|
70 |
+
|
71 |
|
72 |
DESCRIPTION = "Bank dataset for subscription prediction."
|
73 |
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/bank+marketing"
|
|
|
150 |
|
151 |
yield row_id, data_row
|
152 |
|
153 |
+
def preprocess(self, data: pandas.DataFrame, config: str = "subscription") -> pandas.DataFrame:
|
154 |
+
if config == "subscription":
|
155 |
+
data.drop("day", axis="columns", inplace=True)
|
156 |
+
data.drop("contact", axis="columns", inplace=True)
|
157 |
+
data.drop("duration", axis="columns", inplace=True)
|
158 |
+
data.drop("poutcome", axis="columns", inplace=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
+
data.columns = _BASE_FEATURE_NAMES
|
161 |
+
|
162 |
+
for feature in _ENCODING_DICS:
|
163 |
+
encoding_function = partial(self.encode, feature)
|
164 |
+
data.loc[:, feature] = data[feature].apply(encoding_function)
|
165 |
|
|
|
166 |
return data
|
167 |
else:
|
168 |
raise ValueError(f"Unknown config: {config}")
|
169 |
+
|
170 |
+
def encode(self, feature, value):
|
171 |
+
if feature in _ENCODING_DICS:
|
172 |
+
return _ENCODING_DICS[feature][value]
|
173 |
+
raise ValueError(f"Unknown feature: {feature}")
|
174 |
+
|
175 |
+
def encoding_dics(self):
|
176 |
+
data = [pandas.DataFrame([(feature, original, encoded) for original, encoded in d.items()])
|
177 |
+
for feature, d in _ENCODING_DICS.items()]
|
178 |
+
data = pandas.concat(data, axis="rows").reset_index()
|
179 |
+
data.drop("index", axis="columns", inplace=True)
|
180 |
+
data.columns = ["feature", "original_value", "encoded_value"]
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|