File size: 5,525 Bytes
d458ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe455ac
d458ca1
 
edbc126
d458ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edbc126
d458ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edbc126
 
 
d458ca1
edbc126
 
d458ca1
edbc126
 
fe455ac
ff49b46
d458ca1
33e26ce
 
ee71034
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
"""Heloc Dataset"""

from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_ORIGINAL_FEATURE_NAMES = [
    "RiskPerformance",
    "ExternalRiskEstimate",
    "MSinceOldestTradeOpen",
    "MSinceMostRecentTradeOpen",
    "AverageMInFile",
    "NumSatisfactoryTrades",
    "NumTrades60Ever2DerogPubRec",
    "NumTrades90Ever2DerogPubRec",
    "PercentTradesNeverDelq",
    "MSinceMostRecentDelq",
    "MaxDelq2PublicRecLast12M",
    "MaxDelqEver",
    "NumTotalTrades",
    "NumTradesOpeninLast12M",
    "PercentInstallTrades",
    "MSinceMostRecentInqexcl7days",
    "NumInqLast6M",
    "NumInqLast6Mexcl7days",
    "NetFractionRevolvingBurden",
    "NetFractionInstallBurden",
    "NumRevolvingTradesWBalance",
    "NumInstallTradesWBalance",
    "NumBank2NatlTradesWHighUtilization",
    "PercentTradesWBalance",
]
_BASE_FEATURE_NAMES = [
    "is_at_risk",
    "estimate_of_risk",
    "months_since_first_trade",
    "months_since_last_trade",
    "average_duration_of_resolution",
    "number_of_satisfactory_trades",
    "nr_trades_insolvent_for_over_60_days",
    "nr_trades_insolvent_for_over_90_days",
    "percentage_of_legal_trades",
    "months_since_last_illegal_trade",
    "maximum_illegal_trades_over_last_year",
    "maximum_illegal_trades",
    "nr_total_trades",
    "nr_trades_initiated_in_last_year",
    "percentage_of_installment_trades",
    "months_since_last_inquiry_not_recent",
    "nr_inquiries_in_last_6_months",
    "nr_inquiries_in_last_6_months_not_recent",
    "net_fraction_of_revolving_burden",
    "net_fraction_of_installment_burden",
    "nr_revolving_trades_with_balance",
    "nr_installment_trades_with_balance",
    "nr_banks_with_high_ratio",
    "percentage_trades_with_balance"
]

DESCRIPTION = "Heloc dataset for trade insolvency risk prediction."
_HOMEPAGE = "https://community.fico.com/s/explainable-machine-learning-challenge?tabset-158d9=ca01a"
_URLS = ("https://community.fico.com/s/explainable-machine-learning-challenge?tabset-158d9=ca01a")
_CITATION = """"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/heloc/raw/main/heloc.csv",
}
features_types_per_config = {
    "risk": {
        "estimate_of_risk": datasets.Value("int8"),
        "months_since_first_trade": datasets.Value("int32"),
        "months_since_last_trade": datasets.Value("int32"),
        "average_duration_of_resolution": datasets.Value("int32"),
        "number_of_satisfactory_trades": datasets.Value("int16"),
        "nr_trades_insolvent_for_over_60_days": datasets.Value("int16"),
        "nr_trades_insolvent_for_over_90_days": datasets.Value("int16"),
        "percentage_of_legal_trades": datasets.Value("int16"),
        "months_since_last_illegal_trade": datasets.Value("int32"),
        "maximum_illegal_trades_over_last_year": datasets.Value("int8"),
        "maximum_illegal_trades": datasets.Value("int16"),
        "nr_total_trades": datasets.Value("int16"),
        "nr_trades_initiated_in_last_year": datasets.Value("int16"),
        "percentage_of_installment_trades": datasets.Value("int16"),
        "months_since_last_inquiry_not_recent": datasets.Value("int16"),
        "nr_inquiries_in_last_6_months": datasets.Value("int16"),
        "nr_inquiries_in_last_6_months_not_recent": datasets.Value("int16"),
        "net_fraction_of_revolving_burden": datasets.Value("int32"),
        "net_fraction_of_installment_burden": datasets.Value("int32"),
        "nr_revolving_trades_with_balance": datasets.Value("int16"),
        "nr_installment_trades_with_balance": datasets.Value("int16"),
        "nr_banks_with_high_ratio": datasets.Value("int16"),
        "percentage_trades_with_balance": datasets.Value("int16"),
        "is_at_risk": 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 HelocConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(HelocConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Heloc(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "risk"
    BUILDER_CONFIGS = [
        HelocConfig(name="risk",
                     description="Binary classification of trade risk.")
    ]


    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):
        data = pandas.read_csv(filepath)
        data.columns = _BASE_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 = "risk") -> pandas.DataFrame:
        data = data[list(features_types_per_config["risk"].keys())]

        data.loc[:, "is_at_risk"] = data.is_at_risk.apply(lambda x: 1 if x == "Bad" else 0)

        return data