heloc / heloc.py
mstz's picture
Update heloc.py
edbc126
"""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