Long-Term-Care-Aggregated-Data / Long-Term-Care-Aggregated-Data.py
mastergopote44's picture
Set my folder as the main source of truth
75b2dfc
raw
history blame
8.37 kB
# -*- coding: utf-8 -*-
"""Long-Term-Care-Aggregated-Data.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/14YdgB8b4TtNetbHpTstGH7W4DLa3Yyxq
"""
from datasets import GeneratorBasedBuilder, DownloadManager, DatasetInfo, Array3D, BuilderConfig, SplitGenerator, Version
from datasets.features import Features, Value, Sequence
import datasets
import pandas as pd
import json
import zipfile
from PIL import Image
import numpy as np
import io
import csv
import json
import os
from typing import List
import datasets
import logging
_CITATION = """\
@misc{long_term_care_aggregated_dataset,
title = {Long-Term Care Aggregated Dataset},
author = {Kao, Hsuan-Chen (Justin)},
year = {2024},
publisher = {Hugging Face},
url = {https://github.com/justinkao44/STA663_Project_1},
}
"""
_DESCRIPTION = """\
The Long-Term Care Aggregated Dataset is a collection of insurance data for 'incidence' and 'termination' categories.
It is compiled from Long Term Care insurance policies data, providing insights into trends and patterns in insurance claims
and terminations. This dataset can be used for actuarial analysis, risk assessment, and to inform insurance product development.
"""
_HOMEPAGE = "https://github.com/justinkao44/STA663_Project_1"
_LICENSE = "Apache-2.0"
_URLS = {
"train_incidence": "https://github.com/justinkao44/STA663_Project_1/raw/main/train_filtered_incidence_df.zip",
"train_termination": "https://github.com/justinkao44/STA663_Project_1/raw/main/train_filtered_termination_df.zip",
"validation_incidence": "https://github.com/justinkao44/STA663_Project_1/raw/main/validation_filtered_incidence_df.zip",
"validation_termination": "https://github.com/justinkao44/STA663_Project_1/raw/main/validation_filtered_termination_df.zip",
}
class LongTermCareAggregatedData(datasets.GeneratorBasedBuilder):
"""Dataset for insurance 'incidence' and 'termination' data."""
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="incidence", version=datasets.Version("1.0.0"), description="This part of the dataset includes incidence features"),
datasets.BuilderConfig(name="termination", version=datasets.Version("1.0.0"), description="This part of the dataset includes termination features"),
]
def _info(self):
if self.config.name == "incidence":
features = datasets.Features({
"Group_Indicator": datasets.Value("string"),
"Gender": datasets.Value("string"),
"Issue_Age_Bucket": datasets.Value("string"),
"Incurred_Age_Bucket": datasets.Value("string"),
"Issue_Year_Bucket": datasets.Value("string"),
"Policy_Year": datasets.Value("string"),
"Marital_Status": datasets.Value("string"),
"Premium_Class": datasets.Value("string"),
"Underwriting_Type": datasets.Value("string"),
"Coverage_Type_Bucket": datasets.Value("string"),
"Tax_Qualification_Status": datasets.Value("string"),
"Inflation_Rider": datasets.Value("string"),
"Rate_Increase_Flag": datasets.Value("string"),
"Restoration_of_Benefits": datasets.Value("string"),
"NH_Orig_Daily_Ben_Bucket": datasets.Value("string"),
"ALF_Orig_Daily_Ben_Bucket": datasets.Value("string"),
"HHC_Orig_Daily_Ben_Bucket": datasets.Value("string"),
"NH_Ben_Period_Bucket": datasets.Value("string"),
"ALF_Ben_Period_Bucket": datasets.Value("string"),
"HHC_Ben_Period_Bucket": datasets.Value("string"),
"NH_EP_Bucket": datasets.Value("string"),
"ALF_EP_Bucket": datasets.Value("string"),
"HHC_EP_Bucket": datasets.Value("string"),
"Region": datasets.Value("string"),
"Active_Exposure": datasets.Value("float64"),
"Total_Exposure": datasets.Value("float64"),
"Claim_Count": datasets.Value("int32"),
"Count_NH": datasets.Value("int32"),
"Count_ALF": datasets.Value("int32"),
"Count_HHC": datasets.Value("int32"),
"Count_Unk": datasets.Value("int32"),
})
elif self.config.name == "termination":
features = datasets.Features({
"Gender": datasets.Value("string"),
"Incurred_Age_Bucket": datasets.Value("string"),
"Incurred_Year_Bucket": datasets.Value("string"),
"Claim_Type": datasets.Value("string"),
"Region": datasets.Value("string"),
"Diagnosis_Category": datasets.Value("string"),
"Claim_Duration": datasets.Value("string"),
"Exposure": datasets.Value("string"),
"Deaths": datasets.Value("string"),
"Recovery": datasets.Value("string"),
"Terminations": datasets.Value("string"),
"Benefit_Expiry": datasets.Value("string"),
"Others_Terminations": datasets.Value("string"),
})
else:
raise ValueError(f"BuilderConfig name not recognized: {self.config.name}")
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
homepage="https://www.soa.org/resources/experience-studies/2020/2000-2016-ltc-aggregate-database/",
citation="Please cite this dataset as: Society of Actuaries (SOA). (2020). Long Term Care Insurance Aggregate Experience Data, 2000-2016."
)
def _split_generators(self, dl_manager):
downloaded_files = dl_manager.download(_URLS)
train_file = downloaded_files[f"train_{self.config.name}"]
validation_file = downloaded_files[f"validation_{self.config.name}"]
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"data_file": train_file,
"split": self.config.name,
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"data_file": validation_file,
"split": self.config.name,
},
),
]
def _generate_examples(self, data_file, split):
with zipfile.ZipFile(data_file, 'r') as zip_ref:
# Assume each zip contains only one CSV file
csv_filename = zip_ref.namelist()[0]
with zip_ref.open(csv_filename) as csv_file:
dataframe = pd.read_csv(io.TextIOWrapper(csv_file))
feature_columns = self._get_feature_columns_by_config()
for idx, row in dataframe.iterrows():
feature_dict = {column: row[column] for column in feature_columns if column in dataframe.columns}
yield idx, feature_dict
def _get_feature_columns_by_config(self):
if self.config.name == "incidence":
return [
"Group_Indicator", "Gender", "Issue_Age_Bucket", "Incurred_Age_Bucket",
"Issue_Year_Bucket", "Policy_Year", "Marital_Status", "Premium_Class",
"Underwriting_Type", "Coverage_Type_Bucket", "Tax_Qualification_Status",
"Inflation_Rider", "Rate_Increase_Flag", "Restoration_of_Benefits",
"NH_Orig_Daily_Ben_Bucket", "ALF_Orig_Daily_Ben_Bucket", "HHC_Orig_Daily_Ben_Bucket",
"NH_Ben_Period_Bucket", "ALF_Ben_Period_Bucket", "HHC_Ben_Period_Bucket",
"NH_EP_Bucket", "ALF_EP_Bucket", "HHC_EP_Bucket", "Region",
"Active_Exposure", "Total_Exposure", "Claim_Count", "Count_NH", "Count_ALF", "Count_HHC", "Count_Unk",
]
elif self.config.name == "termination":
return [
"Gender", "Incurred_Age_Bucket", "Incurred_Year_Bucket", "Claim_Type",
"Region", "Diagnosis_Category", "Claim_Duration", "Exposure", "Deaths",
"Recovery", "Terminations", "Benefit_Expiry", "Others_Terminations",
]
else:
raise ValueError(f"Config name not recognized: {self.config.name}")