"""NBFI: A Census Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ORIGINAL_FEATURE_NAMES = [ "ID", "Client_Income", "Car_Owned", "Bike_Owned", "Active_Loan", "House_Own", "Child_Count", "Credit_Amount", "Loan_Annuity", "Accompany_Client", "Client_Income_Type", "Client_Education", "Client_Marital_Status", "Client_Gender", "Loan_Contract_Type", "Client_Housing_Type", "Population_Region_Relative", "Age_Days", "Employed_Days", "Registration_Days", "ID_Days", "Own_House_Age", "Mobile_Tag", "Homephone_Tag", "Workphone_Working", "Client_Occupation", "Client_Family_Members", "Cleint_City_Rating", "Application_Process_Day", "Application_Process_Hour", "Client_Permanent_Match_Tag", "Client_Contact_Work_Tag", "Type_Organization", "Score_Source_1", "Score_Source_2", "Score_Source_3", "Social_Circle_Default", "Phone_Change", "Credit_Bureau", "Default" ] _BASE_FEATURE_NAMES = [ "income", "owns_a_car", "owns_a_bike", "has_an_active_loan", "owns_a_house", "nr_children", "credit", "loan_annuity", "accompanied_by", "income_type", "education_level", "marital_status", "is_male", "type_of_contract", "type_of_housing", "residence_density", "age_in_days", "consecutive_days_of_employment", "nr_days_since_last_registration_change", "nr_days_since_last_document_change", "has_provided_a_mobile_number", "has_provided_a_home_number", "was_reachable_at_work", "job", "nr_family_members", "city_rating", "weekday_of_application", "hour_of_application", "same_residence_and_home", "same_work_and_home", "score_1", "score_2", "score_3", "nr_defaults_in_social_circle", "inquiries_in_last_year", "has_defaulted" ] features_types_per_config = { "default": { "income": datasets.Value("float32"), "owns_a_car": datasets.Value("bool"), "owns_a_bike": datasets.Value("bool"), "has_an_active_loan": datasets.Value("bool"), "owns_a_house": datasets.Value("bool"), "nr_children": datasets.Value("int8"), "credit": datasets.Value("float32"), "loan_annuity": datasets.Value("float32"), "accompanied_by": datasets.Value("string"), "income_type": datasets.Value("string"), "education_level": datasets.Value("float32"), "marital_status": datasets.Value("float32"), "is_male": datasets.Value("bool"), "type_of_contract": datasets.Value("string"), "type_of_housing": datasets.Value("string"), "residence_density": datasets.Value("float32"), "age_in_days": datasets.Value("int32"), "consecutive_days_of_employment": datasets.Value("int16"), "nr_days_since_last_registration_change": datasets.Value("int32"), "nr_days_since_last_document_change": datasets.Value("int32"), "has_provided_a_mobile_number": datasets.Value("bool"), "has_provided_a_home_number": datasets.Value("bool"), "was_reachable_at_work": datasets.Value("bool"), "job": datasets.Value("string"), "nr_family_members": datasets.Value("int8"), "city_rating": datasets.Value("int8"), "weekday_of_application": datasets.Value("int8"), "hour_of_application": datasets.Value("float32"), "same_residence_and_home": datasets.Value("bool"), "same_work_and_home": datasets.Value("bool"), "score_1": datasets.Value("float32"), "score_2": datasets.Value("float32"), "score_3": datasets.Value("float32"), "nr_defaults_in_social_circle": datasets.Value("int8"), "inquiries_in_last_year": datasets.Value("float32"), "has_defaulted": datasets.ClassLabel(num_classes=2, names=("no", "yes")) } } _ENCODING_DICS = {} _EDUCATION_ENCODING = { "Junior secondary": 0, "Secondary": 1, "Graduation dropout": 2, "Graduation": 2, "Post Grad": 4 } DESCRIPTION = "NBFI dataset from default prediction." _HOMEPAGE = "https://www.kaggle.com/datasets/meastanmay/nbfi-vehicle-loan-repayment-dataset" _URLS = ("https://www.kaggle.com/datasets/meastanmay/nbfi-vehicle-loan-repayment-dataset") _CITATION = """""" # Dataset info urls_per_split = { "train": "https://gist.githubusercontent.com/msetzu/6c83dc3b7092d428ae2f08dc91e1020c/raw/9fc3171b293d0dc29963357450308eb4c7e3a15b/Train_Dataset.csv", "test": "https://gist.githubusercontent.com/msetzu/f0032b855008f579299d7ad78d9dd9c2/raw/ba42badeb10b505cb283bdb16d3de581ffe7a332/Test_Dataset.csv" } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class NBFIConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(NBFIConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class NBFI(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "default" BUILDER_CONFIGS = [ NBFIConfig(name="default", description="NBFI for default binary classification.") ] def _info(self): if self.config.name not in features_per_config: raise ValueError(f"Unknown configuration: {self.config.name}") 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"]}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloads["test"]}), ] def _generate_examples(self, filepath: str): if self.config.name == "default": data = pandas.read_csv(filepath) data = self.preprocess(data, config=self.config.name) for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row else: raise ValueError(f"Unknown config: {self.config.name}") def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame: data.drop("ID", axis="columns", inplace=True) data.drop("Own_House_Age", axis="columns", inplace=True) data.drop("Type_Organization", axis="columns", inplace=True) data.drop("Phone_Change", axis="columns", inplace=True) data = data[~data.Client_Education.isna()] data = data[~data.Child_Count.isna()] data = data[~data.Client_Marital_Status.isna()] data = data[~data.Client_Gender.isna()] data = data[~data.Loan_Contract_Type.isna()] data = data[~data.Client_Housing_Type.isna()] data = data[~data.Age_Days.isna()] data = data[~data.Employed_Days.isna()] data = data[~data.Registration_Days.isna()] data = data[~data.ID_Days.isna()] data = data[~data.Cleint_City_Rating.isna()] data = data[~data.Application_Process_Day.isna()] data = data[~data.Application_Process_Hour.isna()] data = data[~data.Client_Permanent_Match_Tag.isna()] data = data[~data.Client_Contact_Work_Tag.isna()] data = data[~data.Score_Source_1.isna()] data = data[~data.Score_Source_2.isna()] data = data[~data.Score_Source_3.isna()] data = data[~data.Credit_Bureau.isna()] data.columns = _BASE_FEATURE_NAMES data["education_level"] = data["education_level"].apply(lambda x: _EDUCATION_ENCODING[x]) data["is_male"] = data["is_male"].apply(lambda x: x == "M") data["owns_a_car"] = data["owns_a_car"].apply(bool) data["owns_a_bike"] = data["owns_a_bike"].apply(bool) data["has_an_active_loan"] = data["has_an_active_loan"].apply(bool) data["owns_a_house"] = data["owns_a_house"].apply(bool) data["is_male"] = data["is_male"].apply(bool) data["has_provided_a_mobile_number"] = data["has_provided_a_mobile_number"].apply(bool) data["has_provided_a_home_number"] = data["has_provided_a_home_number"].apply(bool) data["was_reachable_at_work"] = data["was_reachable_at_work"].apply(bool) data["same_residence_and_home"] = data["same_residence_and_home"].apply(bool) data["same_work_and_home"] = data["same_work_and_home"].apply(bool) data = data.astype({ "is_male": "bool", "nr_children": "int8" }) print(data.isna().any()) print(data.is_male) print([f for f in data.columns if "M" in data[f]]) return data[list(features_types_per_config[config].keys())]