mastergopote44 commited on
Commit
7b6eb96
1 Parent(s): 5296e3b
Files changed (1) hide show
  1. Long-Term-Care-Aggregated-Data.py +181 -0
Long-Term-Care-Aggregated-Data.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Long-Term-Care-Aggregated-Data.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/14YdgB8b4TtNetbHpTstGH7W4DLa3Yyxq
8
+ """
9
+
10
+ from datasets import GeneratorBasedBuilder, DownloadManager, DatasetInfo, Array3D, BuilderConfig, SplitGenerator, Version
11
+ from datasets.features import Features, Value, Sequence
12
+ import datasets
13
+ import pandas as pd
14
+ import json
15
+ import zipfile
16
+ from PIL import Image
17
+ import numpy as np
18
+ import io
19
+ import csv
20
+ import json
21
+ import os
22
+ from typing import List
23
+ import datasets
24
+ import logging
25
+
26
+ _CITATION = """\
27
+ @misc{long_term_care_aggregated_dataset,
28
+ title = {Long-Term Care Aggregated Dataset},
29
+ author = {Kao, Hsuan-Chen (Justin)},
30
+ year = {2024},
31
+ publisher = {Hugging Face},
32
+ url = {https://github.com/justinkao44/STA663_Project_1},
33
+ }
34
+ """
35
+
36
+ _DESCRIPTION = """\
37
+ The Long-Term Care Aggregated Dataset is a collection of insurance data for 'incidence' and 'termination' categories.
38
+ It is compiled from Long Term Care insurance policies data, providing insights into trends and patterns in insurance claims
39
+ and terminations. This dataset can be used for actuarial analysis, risk assessment, and to inform insurance product development.
40
+ """
41
+
42
+ _HOMEPAGE = "https://github.com/justinkao44/STA663_Project_1"
43
+
44
+ _LICENSE = "Apache-2.0"
45
+
46
+ _URLS = {
47
+ "train_incidence": "https://github.com/justinkao44/STA663_Project_1/raw/main/train_filtered_incidence_df.zip",
48
+ "train_termination": "https://github.com/justinkao44/STA663_Project_1/raw/main/train_filtered_termination_df.zip",
49
+ "validation_incidence": "https://github.com/justinkao44/STA663_Project_1/raw/main/validation_filtered_incidence_df.zip",
50
+ "validation_termination": "https://github.com/justinkao44/STA663_Project_1/raw/main/validation_filtered_termination_df.zip",
51
+ }
52
+
53
+
54
+ class LongTermCareAggregatedData(datasets.GeneratorBasedBuilder):
55
+ """Dataset for insurance 'incidence' and 'termination' data."""
56
+
57
+ BUILDER_CONFIGS = [
58
+ datasets.BuilderConfig(name="incidence", version=datasets.Version("1.0.0"), description="This part of the dataset includes incidence features"),
59
+ datasets.BuilderConfig(name="termination", version=datasets.Version("1.0.0"), description="This part of the dataset includes termination features"),
60
+ ]
61
+
62
+ def _info(self):
63
+ if self.config.name == "incidence":
64
+ features = datasets.Features({
65
+ "Group_Indicator": datasets.Value("string"),
66
+ "Gender": datasets.Value("string"),
67
+ "Issue_Age_Bucket": datasets.Value("string"),
68
+ "Incurred_Age_Bucket": datasets.Value("string"),
69
+ "Issue_Year_Bucket": datasets.Value("string"),
70
+ "Policy_Year": datasets.Value("string"),
71
+ "Marital_Status": datasets.Value("string"),
72
+ "Premium_Class": datasets.Value("string"),
73
+ "Underwriting_Type": datasets.Value("string"),
74
+ "Coverage_Type_Bucket": datasets.Value("string"),
75
+ "Tax_Qualification_Status": datasets.Value("string"),
76
+ "Inflation_Rider": datasets.Value("string"),
77
+ "Rate_Increase_Flag": datasets.Value("string"),
78
+ "Restoration_of_Benefits": datasets.Value("string"),
79
+ "NH_Orig_Daily_Ben_Bucket": datasets.Value("string"),
80
+ "ALF_Orig_Daily_Ben_Bucket": datasets.Value("string"),
81
+ "HHC_Orig_Daily_Ben_Bucket": datasets.Value("string"),
82
+ "NH_Ben_Period_Bucket": datasets.Value("string"),
83
+ "ALF_Ben_Period_Bucket": datasets.Value("string"),
84
+ "HHC_Ben_Period_Bucket": datasets.Value("string"),
85
+ "NH_EP_Bucket": datasets.Value("string"),
86
+ "ALF_EP_Bucket": datasets.Value("string"),
87
+ "HHC_EP_Bucket": datasets.Value("string"),
88
+ "Region": datasets.Value("string"),
89
+ "Active_Exposure": datasets.Value("float64"),
90
+ "Total_Exposure": datasets.Value("float64"),
91
+ "Claim_Count": datasets.Value("int32"),
92
+ "Count_NH": datasets.Value("int32"),
93
+ "Count_ALF": datasets.Value("int32"),
94
+ "Count_HHC": datasets.Value("int32"),
95
+ "Count_Unk": datasets.Value("int32"),
96
+ })
97
+ elif self.config.name == "termination":
98
+ features = datasets.Features({
99
+ "Gender": datasets.Value("string"),
100
+ "Incurred_Age_Bucket": datasets.Value("string"),
101
+ "Incurred_Year_Bucket": datasets.Value("string"),
102
+ "Claim_Type": datasets.Value("string"),
103
+ "Region": datasets.Value("string"),
104
+ "Diagnosis_Category": datasets.Value("string"),
105
+ "Claim_Duration": datasets.Value("int64"),
106
+ "Exposure": datasets.Value("int64"),
107
+ "Deaths": datasets.Value("int64"),
108
+ "Recovery": datasets.Value("int64"),
109
+ "Terminations": datasets.Value("int64"),
110
+ "Benefit_Expiry": datasets.Value("int64"),
111
+ "Others_Terminations": datasets.Value("int64"),
112
+ })
113
+ else:
114
+ raise ValueError(f"BuilderConfig name not recognized: {self.config.name}")
115
+
116
+ return datasets.DatasetInfo(
117
+ description=_DESCRIPTION,
118
+ features=features,
119
+ supervised_keys=None,
120
+ homepage="https://www.soa.org/resources/experience-studies/2020/2000-2016-ltc-aggregate-database/",
121
+ citation="Please cite this dataset as: Society of Actuaries (SOA). (2020). Long Term Care Insurance Aggregate Experience Data, 2000-2016."
122
+ )
123
+
124
+
125
+ def _split_generators(self, dl_manager):
126
+ downloaded_files = dl_manager.download(_URLS)
127
+
128
+ # 基于配置名确定文件路径
129
+ train_file = downloaded_files[f"train_{self.config.name}"]
130
+ validation_file = downloaded_files[f"validation_{self.config.name}"]
131
+
132
+ return [
133
+ datasets.SplitGenerator(
134
+ name=datasets.Split.TRAIN,
135
+ gen_kwargs={
136
+ "data_file": train_file,
137
+ "split": self.config.name, # 使用配置名作为 split 参数(如果需要)
138
+ },
139
+ ),
140
+ datasets.SplitGenerator(
141
+ name=datasets.Split.VALIDATION,
142
+ gen_kwargs={
143
+ "data_file": validation_file,
144
+ "split": self.config.name, # 使用配置名作为 split 参数(如果需要)
145
+ },
146
+ ),
147
+ ]
148
+
149
+ def _generate_examples(self, data_file, split):
150
+ with zipfile.ZipFile(data_file, 'r') as zip_ref:
151
+ # Assume each zip contains only one CSV file
152
+ csv_filename = zip_ref.namelist()[0]
153
+ with zip_ref.open(csv_filename) as csv_file:
154
+ dataframe = pd.read_csv(io.TextIOWrapper(csv_file))
155
+
156
+ feature_columns = self._get_feature_columns_by_config()
157
+
158
+ for idx, row in dataframe.iterrows():
159
+ feature_dict = {column: row[column] for column in feature_columns if column in dataframe.columns}
160
+ yield idx, feature_dict
161
+
162
+ def _get_feature_columns_by_config(self):
163
+ if self.config.name == "incidence":
164
+ return [
165
+ "Group_Indicator", "Gender", "Issue_Age_Bucket", "Incurred_Age_Bucket",
166
+ "Issue_Year_Bucket", "Policy_Year", "Marital_Status", "Premium_Class",
167
+ "Underwriting_Type", "Coverage_Type_Bucket", "Tax_Qualification_Status",
168
+ "Inflation_Rider", "Rate_Increase_Flag", "Restoration_of_Benefits",
169
+ "NH_Orig_Daily_Ben_Bucket", "ALF_Orig_Daily_Ben_Bucket", "HHC_Orig_Daily_Ben_Bucket",
170
+ "NH_Ben_Period_Bucket", "ALF_Ben_Period_Bucket", "HHC_Ben_Period_Bucket",
171
+ "NH_EP_Bucket", "ALF_EP_Bucket", "HHC_EP_Bucket", "Region",
172
+ "Active_Exposure", "Total_Exposure", "Claim_Count", "Count_NH", "Count_ALF", "Count_HHC", "Count_Unk",
173
+ ]
174
+ elif self.config.name == "termination":
175
+ return [
176
+ "Gender", "Incurred_Age_Bucket", "Incurred_Year_Bucket", "Claim_Type",
177
+ "Region", "Diagnosis_Category", "Claim_Duration", "Exposure", "Deaths",
178
+ "Recovery", "Terminations", "Benefit_Expiry", "Others_Terminations",
179
+ ]
180
+ else:
181
+ raise ValueError(f"Config name not recognized: {self.config.name}")