File size: 8,061 Bytes
4db99e8
5eedf78
 
3a7b078
5eedf78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6abc811
5eedf78
 
 
 
4db99e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5eedf78
4db99e8
5eedf78
 
4db99e8
5eedf78
 
 
976e1b8
5eedf78
 
 
 
 
 
 
 
 
 
 
 
 
ff545a7
5eedf78
 
 
2552407
5eedf78
 
 
 
 
 
 
 
 
 
2552407
5eedf78
 
 
 
 
 
 
4db99e8
5eedf78
4db99e8
5eedf78
 
 
4db99e8
5eedf78
 
 
4db99e8
5eedf78
4db99e8
5eedf78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2552407
4db99e8
 
c7f9470
 
 
 
5eedf78
 
 
 
 
 
4db99e8
 
 
 
 
a8e50bd
 
 
 
 
8d066cd
3dbffdf
5eedf78
 
 
 
2552407
 
 
5eedf78
4db99e8
901d3ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4db99e8
8d066cd
 
 
 
 
 
 
 
 
 
 
 
4db99e8
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""German Dataset"""

from typing import List
from functools import partial

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_ORIGINAL_FEATURE_NAMES = [
    "checking_account_status",
    "account_life_in_months",
    "credit_status",
    "loan_purpose",
    "current_credit",
    "current_savings",
    "employed_since",
    "installment_rate_percentage",
    "personal_status_and_sex",
    "guarantors",
    "years_living_in_current_residence",
    "property",
    "age",
    "installment_plans",
    "housing_status",
    "nr_credit_accounts_in_bank",
    "job_status",
    "number_of_people_in_support",
    "has_registered_phone_number",
    "is_foreign",
    "loan_granted",
]
_BASE_FEATURE_NAMES = [
    "checking_account_status",
    "account_life_in_months",
    "credit_status",
    "loan_purpose",
    "current_credit",
    "current_savings",
    "employed_since",
    "installment_rate_percentage",
    "sex",
    "marital_status",
    "guarantors",
    "years_living_in_current_residence",
    "age",
    "installment_plans",
    "housing_status",
    "nr_credit_accounts_in_bank",
    "job_status",
    "number_of_people_in_support",
    "has_registered_phone_number",
    "is_foreign",
    "loan_granted"
]
_ENCODING_DICS = {
    "is_foreign": {
        "A201": 0,
        "A202": 1
    },
    "has_registered_phone_number": {
        "A191": 0,
        "A192": 1
    },
    "job_status": {
        "A171": 0,
        "A172": 1,
        "A173": 2,
        "A174": 3
    },
    "housing_status": {
        "A153": 0,
        "A151": 1,
        "A152": 2
    },
    "installment_plans": {
        "A141": 0,
        "A142": 1,
        "A143": 2
    },
    "guarantors": {
        "A101": 0,
        "A102": 1,
        "A103": 2
    },
    "marital_status": {
        "A91": 0,
        "A92": 0,
        "A93": 1,
        "A94": 2,
        "A95": 1,
    },
    "sex": {
        "A91": 0,
        "A93": 0,
        "A94": 0,
        "A92": 1,
        "A95": 1,
    },
    "employed_since": {
        "A71": 0,
        "A72": 1,
        "A73": 2,
        "A74": 3,
        "A75": 4,
    },
    "current_savings": {
        "A65": 0,
        "A61": 1,
        "A62": 2,
        "A63": 3,
        "A64": 4,
    },
    "credit_status": {
        "A30": 0,
        "A31": 1,
        "A32": 2,
        "A33": 3,
        "A34": 4,
    },
    "checking_account_status": {
            "A14": 0,
            "A11": 1,
            "A12": 2,
            "A13": 3,
        }
}

DESCRIPTION = "German dataset for cancer prediction."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Statlog+%28German+Credit+Data%29"
_URLS = ("https://archive.ics.uci.edu/ml/datasets/Statlog+%28German+Credit+Data%29")
_CITATION = """"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/german/raw/main/german.data",
}
features_types_per_config = {
    "encoding": {
        "feature": datasets.Value("string"),
        "original_value": datasets.Value("string"),
        "encoded_value":  datasets.Value("int8"),
    },

    "loan": {
        "checking_account_status": datasets.Value("int8"),
        "account_life_in_months": datasets.Value("int8"),
        "credit_status": datasets.Value("int8"),
        "loan_purpose": datasets.Value("string"),
        "current_credit": datasets.Value("int32"),
        "current_savings": datasets.Value("int8"),
        "employed_since": datasets.Value("int8"),
        "installment_rate_percentage": datasets.Value("int8"),
        "is_male": datasets.Value("bool"),
        "marital_status": datasets.Value("string"),
        "guarantors": datasets.Value("int8"),
        "years_living_in_current_residence": datasets.Value("int8"),
        "age": datasets.Value("int8"),
        "installment_plans": datasets.Value("string"),
        "housing_status": datasets.Value("int8"),
        "nr_credit_accounts_in_bank": datasets.Value("int8"),
        "job_status": datasets.Value("int8"),
        "number_of_people_in_support": datasets.Value("int8"),
        "has_registered_phone_number": datasets.Value("int8"),
        "is_foreign": datasets.Value("bool"),
        "loan_granted": 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 GermanConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(GermanConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class German(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "loan"
    BUILDER_CONFIGS = [
        GermanConfig(name="encoding",
                     description="Encoding dictionaries for discrete features."),
        GermanConfig(name="loan",
                     description="Binary classification of loan approval."),
    ]


    def _info(self):
        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_dics()

        else:
            data = pandas.read_csv(filepath, sep=" ", header=None)
            data.columns=_ORIGINAL_FEATURE_NAMES
            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 = "loan") -> pandas.DataFrame:
        for feature in _ENCODING_DICS:
            if feature not in ["marital_status", "sex"]:
                encoding_function = partial(self.encode, feature)
                data.loc[:, feature] = data[feature].apply(encoding_function)
        
        encode_marital_status = partial(self.encode, "marital_status")
        encode_sex = partial(self.encode, "sex")
        data.loc[:, "marital_status"] = data.personal_status_and_sex.apply(encode_marital_status)
        data.loc[:, "sex"] = data.personal_status_and_sex.apply(encode_sex)
        data.loc[:, "loan_purpose"] = data.loan_purpose.apply(self.encode_loan_purpose)
        data.loc[:, "loan_granted"] = data.loan_granted.apply(lambda x: x - 1)
        
        data.drop("personal_status_and_sex", axis="columns", inplace=True)

        data = data[_BASE_FEATURE_NAMES]
        data = data.rename(columns={"sex": "is_male"})
        data = data.astype({"is_foreign": "bool"})
        data.loc[:, "is_foreign"] = data.is_foreign.apply(bool)

        return data
    
    def encode(self, feature, value):
        if feature in _ENCODING_DICS:
            return _ENCODING_DICS[feature][value]
        raise ValueError(f"Unknown feature: {feature}")
    
    def encoding_dics(self):
        data = [pandas.DataFrame([(feature, original, encoded) for original, encoded in d.items()])
                for feature, d in _ENCODING_DICS.items()]
        data = pandas.concat(data, axis="rows").reset_index()
        data.drop("index", axis="columns", inplace=True)
        data.columns = ["feature", "original_value", "encoded_value"]

        return data

    def encode_loan_purpose(self, code):
        return {
            "A40": "new car",
            "A41": "used car",
            "A42": "furniture/equipment",
            "A43": "radio/television",
            "A44": "domestic appliances",
            "A45": "repairs",
            "A46": "education",
            "A47": "vacation ",
            "A48": "retraining",
            "A49": "business",
            "A410": "others"
        }[code]