{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# STA 663 Project 1: \n", "- Huggingface: mastergopote44/Long-Term-Care-Aggregated-Data\n", "- Name: Justin Kao\n", "- School: Duke University" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## I. Load Incidence & Termination dataset from SOA website" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "import requests\n", "from io import BytesIO\n", "from zipfile import ZipFile\n", "import pandas as pd\n", "\n", "# Directory to store the extracted files\n", "desired_directory = '/Users/justinkao/Desktop/Durham NC/Duke University/Courses/S_2024_STA 663 Statistical Computing and Computation(Dr. Ouwen Huang)/Project 1/STA663_Project_1' # Replace with your actual path\n", "\n", "# URLs of the zip files\n", "zip_files = {\n", " 'incidence': 'https://www.soa.org/4a33aa/globalassets/assets/files/resources/experience-studies/2020/2000-2016-ltc-incidence.zip',\n", " 'termination': 'https://www.soa.org/4a2e5d/globalassets/assets/files/resources/experience-studies/2020/2000-2016-ltc-termination.zip'\n", "}\n", "\n", "# DataFrames dictionary\n", "dataframes = {}\n", "\n", "# Download and unzip the files\n", "for name, zip_url in zip_files.items():\n", " response = requests.get(zip_url)\n", " # Check if the request was successful\n", " if response.ok:\n", " with ZipFile(BytesIO(response.content)) as thezip:\n", " # Extract all files from the zip into a directory\n", " thezip.extractall(desired_directory)\n", " # Loop through each file in the zip\n", " for zipinfo in thezip.infolist():\n", " # Construct the full path to the extracted file\n", " file_path = f'{desired_directory}/{zipinfo.filename}'\n", " # Assuming the file is a tab-separated txt file\n", " df = pd.read_csv(file_path, sep='\\t', low_memory=False) # Adjust sep if necessary\n", " # Assign the DataFrame to the corresponding variable\n", " dataframes[name] = df\n", " else:\n", " print(f\"Failed to retrieve {zip_url}\")" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Group_IndicatorGenderIssue_Age_BucketIncurred_Age_BucketIssue_Year_BucketPolicy_YearMarital_StatusPremium_ClassUnderwriting_TypeCoverage_Type_Bucket...ALF_EP_BucketHHC_EP_BucketRegionActive_ExposureTotal_ExposureClaim_CountCount_NHCount_ALFCount_HHCCount_Unk
0GroupFemale55-5955-592003-20051-3 yearsSingleStandardOtherComprehensive...00Unknown37.58333237.58333200000
1IndividualFemale60-6470-741997-199910-12 yearsSinglePreferredOtherComprehensive...0002: Northeast14.50000014.50000000000
2GroupFemale50-5460-642000-200210-12 yearsUnknownStandardUnknownComprehensive...00Unknown2250.0000002252.00000000000
3IndividualFemale55-5970-741994-199613-15 yearsMarriedStandardOtherComprehensive...0001: Mid-West19.00000019.00000000000
4IndividualMale60-6465-692003-20054-6 yearsMarriedSubstandardFull underwritingComprehensive...902002: Northeast2.0000002.00000000000
..................................................................
2089318IndividualMale55-5960-642012-20141-3 yearsMarriedSubstandardFull underwritingComprehensive...0001: Mid-West7.0833337.08333300000
2089319IndividualMale65-6970-741997-19997-9 yearsMarriedStandardFull underwritingComprehensive...18018002: Northeast2.0000002.00000000000
2089320IndividualFemale65-6975-791994-19967-9 yearsSinglePreferredOtherComprehensive...0001: Mid-West6.0000006.00000000000
2089321IndividualFemale50-5455-591994-19967-9 yearsSingleStandardUnknownComprehensive...00Unknown6.0000006.00000000000
2089322IndividualFemale0-4950-541997-19991-3 yearsSinglePreferredFull underwritingComprehensive...903003: South3.0000003.00000000000
\n", "

2089323 rows × 31 columns

\n", "
" ], "text/plain": [ " Group_Indicator Gender Issue_Age_Bucket Incurred_Age_Bucket \\\n", "0 Group Female 55-59 55-59 \n", "1 Individual Female 60-64 70-74 \n", "2 Group Female 50-54 60-64 \n", "3 Individual Female 55-59 70-74 \n", "4 Individual Male 60-64 65-69 \n", "... ... ... ... ... \n", "2089318 Individual Male 55-59 60-64 \n", "2089319 Individual Male 65-69 70-74 \n", "2089320 Individual Female 65-69 75-79 \n", "2089321 Individual Female 50-54 55-59 \n", "2089322 Individual Female 0-49 50-54 \n", "\n", " Issue_Year_Bucket Policy_Year Marital_Status Premium_Class \\\n", "0 2003-2005 1-3 years Single Standard \n", "1 1997-1999 10-12 years Single Preferred \n", "2 2000-2002 10-12 years Unknown Standard \n", "3 1994-1996 13-15 years Married Standard \n", "4 2003-2005 4-6 years Married Substandard \n", "... ... ... ... ... \n", "2089318 2012-2014 1-3 years Married Substandard \n", "2089319 1997-1999 7-9 years Married Standard \n", "2089320 1994-1996 7-9 years Single Preferred \n", "2089321 1994-1996 7-9 years Single Standard \n", "2089322 1997-1999 1-3 years Single Preferred \n", "\n", " Underwriting_Type Coverage_Type_Bucket ... ALF_EP_Bucket \\\n", "0 Other Comprehensive ... 0 \n", "1 Other Comprehensive ... 0 \n", "2 Unknown Comprehensive ... 0 \n", "3 Other Comprehensive ... 0 \n", "4 Full underwriting Comprehensive ... 90 \n", "... ... ... ... ... \n", "2089318 Full underwriting Comprehensive ... 0 \n", "2089319 Full underwriting Comprehensive ... 180 \n", "2089320 Other Comprehensive ... 0 \n", "2089321 Unknown Comprehensive ... 0 \n", "2089322 Full underwriting Comprehensive ... 90 \n", "\n", " HHC_EP_Bucket Region Active_Exposure Total_Exposure \\\n", "0 0 Unknown 37.583332 37.583332 \n", "1 0 02: Northeast 14.500000 14.500000 \n", "2 0 Unknown 2250.000000 2252.000000 \n", "3 0 01: Mid-West 19.000000 19.000000 \n", "4 20 02: Northeast 2.000000 2.000000 \n", "... ... ... ... ... \n", "2089318 0 01: Mid-West 7.083333 7.083333 \n", "2089319 180 02: Northeast 2.000000 2.000000 \n", "2089320 0 01: Mid-West 6.000000 6.000000 \n", "2089321 0 Unknown 6.000000 6.000000 \n", "2089322 30 03: South 3.000000 3.000000 \n", "\n", " Claim_Count Count_NH Count_ALF Count_HHC Count_Unk \n", "0 0 0 0 0 0 \n", "1 0 0 0 0 0 \n", "2 0 0 0 0 0 \n", "3 0 0 0 0 0 \n", "4 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "2089318 0 0 0 0 0 \n", "2089319 0 0 0 0 0 \n", "2089320 0 0 0 0 0 \n", "2089321 0 0 0 0 0 \n", "2089322 0 0 0 0 0 \n", "\n", "[2089323 rows x 31 columns]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "incidence_df = dataframes['incidence']\n", "incidence_df\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total_claim_count = incidence_df['Claim_Count'].sum()\n", "total_claim_count" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GenderIncurred_Age_BucketIncurred_Year_BucketClaim_TypeRegionDiagnosis_CategoryClaim_DurationExposureDeathsRecoveryTerminationsBenefit_ExpiryOthers_Terminations
0FemaleUnknown2009-2010HCCUnknownUnknown807702210
1FemaleUnknown2003-2004NHUnknownUnknown864910100
2FemaleUnknown2005-2006OtherUnknownUnknown881610100
3FemaleUnknown2003-2004ALFUnknownUnknown1142910100
4FemaleUnknown< 2001NHUnknownUnknown45651600
..........................................
627185Male85-892007-2008NHUnknown05: Injury33200000
627186Male75-792007-2008HCC03: SouthUnknown316230300
627187Female80-842013-2014HCC01: Mid-West07: Stroke23600000
627188Male70-742001-2002HCC02: NortheastUnknown30700000
627189Female85-892003-2004ALFUnknownUnknown329510100
\n", "

627190 rows × 13 columns

\n", "
" ], "text/plain": [ " Gender Incurred_Age_Bucket Incurred_Year_Bucket Claim_Type \\\n", "0 Female Unknown 2009-2010 HCC \n", "1 Female Unknown 2003-2004 NH \n", "2 Female Unknown 2005-2006 Other \n", "3 Female Unknown 2003-2004 ALF \n", "4 Female Unknown < 2001 NH \n", "... ... ... ... ... \n", "627185 Male 85-89 2007-2008 NH \n", "627186 Male 75-79 2007-2008 HCC \n", "627187 Female 80-84 2013-2014 HCC \n", "627188 Male 70-74 2001-2002 HCC \n", "627189 Female 85-89 2003-2004 ALF \n", "\n", " Region Diagnosis_Category Claim_Duration Exposure Deaths \\\n", "0 Unknown Unknown 80 77 0 \n", "1 Unknown Unknown 86 49 1 \n", "2 Unknown Unknown 88 16 1 \n", "3 Unknown Unknown 114 29 1 \n", "4 Unknown Unknown 4 56 5 \n", "... ... ... ... ... ... \n", "627185 Unknown 05: Injury 33 2 0 \n", "627186 03: South Unknown 31 62 3 \n", "627187 01: Mid-West 07: Stroke 23 6 0 \n", "627188 02: Northeast Unknown 30 7 0 \n", "627189 Unknown Unknown 32 95 1 \n", "\n", " Recovery Terminations Benefit_Expiry Others_Terminations \n", "0 2 2 1 0 \n", "1 0 1 0 0 \n", "2 0 1 0 0 \n", "3 0 1 0 0 \n", "4 1 6 0 0 \n", "... ... ... ... ... \n", "627185 0 0 0 0 \n", "627186 0 3 0 0 \n", "627187 0 0 0 0 \n", "627188 0 0 0 0 \n", "627189 0 1 0 0 \n", "\n", "[627190 rows x 13 columns]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "termination_df = dataframes['termination']\n", "termination_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## II. Data Cleaning Process for \"Incidence Datasets\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (a) Inspect for the presence of outliers and missing values in the variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 1. Check out the dataframe at first and make sure the unique vlaue of each variable is reasonable " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Group_Indicator': array(['Group', 'Individual'], dtype=object),\n", " 'Gender': array(['Female', 'Male'], dtype=object),\n", " 'Issue_Age_Bucket': array(['55-59', '60-64', '50-54', '65-69', '70-74', ' 0-49', '75-79',\n", " '80-84', '85-89', 'Unknown', '90+'], dtype=object),\n", " 'Incurred_Age_Bucket': array(['55-59', '70-74', '60-64', '65-69', '50-54', '75-79', ' 0-49',\n", " '80-84', '85-89', '90+', 'Unknown'], dtype=object),\n", " 'Issue_Year_Bucket': array(['2003-2005', '1997-1999', '2000-2002', '1994-1996', '2006-2008',\n", " '1991-1993', ' < 1991', '2012-2014', '2009-2011', '2015-2016',\n", " 'Unknown'], dtype=object),\n", " 'Policy_Year': array([' 1-3 years', '10-12 years', '13-15 years', ' 4-6 years',\n", " ' 7-9 years', '15+ years'], dtype=object),\n", " 'Marital_Status': array(['Single', 'Unknown', 'Married'], dtype=object),\n", " 'Premium_Class': array(['Standard', 'Preferred', 'Substandard'], dtype=object),\n", " 'Underwriting_Type': array(['Other', 'Unknown', 'Full underwriting'], dtype=object),\n", " 'Coverage_Type_Bucket': array(['Comprehensive', 'Other'], dtype=object),\n", " 'Tax_Qualification_Status': array(['Tax-qualified', 'Non-tax-qualified'], dtype=object),\n", " 'Inflation_Rider': array(['Inflation protection', 'No inflation protection', 'GPO',\n", " 'Unknown'], dtype=object),\n", " 'Rate_Increase_Flag': array(['No', 'Yes'], dtype=object),\n", " 'Restoration_of_Benefits': array(['No', 'Yes'], dtype=object),\n", " 'NH_Orig_Daily_Ben_Bucket': array(['100-199', ' < 100', 'Unknown', '200+'], dtype=object),\n", " 'ALF_Orig_Daily_Ben_Bucket': array([' < 100', '100-199', 'Unknown', '200+'], dtype=object),\n", " 'HHC_Orig_Daily_Ben_Bucket': array([' < 100', '100-199', 'Unknown', '200+'], dtype=object),\n", " 'NH_Ben_Period_Bucket': array(['Unknown', ' 3-4 year', ' Unlimited', ' < 3 year', ' 5-9 year',\n", " ' 10+ years'], dtype=object),\n", " 'ALF_Ben_Period_Bucket': array(['Unknown', ' 3-4 year', ' Unlimited', ' 5-9 year', ' < 3 year',\n", " ' 10+ years'], dtype=object),\n", " 'HHC_Ben_Period_Bucket': array(['Unknown', ' 5-9 year', ' Unlimited', ' < 3 year', ' 3-4 year',\n", " ' 10+ years'], dtype=object),\n", " 'NH_EP_Bucket': array([' 0', ' 90', ' 20', ' 30', ' 180', 'Unknown'], dtype=object),\n", " 'ALF_EP_Bucket': array([' 0', ' 90', ' 20', ' 30', ' 180', 'Unknown'], dtype=object),\n", " 'HHC_EP_Bucket': array([' 0', ' 20', ' 30', ' 90', ' 180', 'Unknown'], dtype=object),\n", " 'Region': array(['Unknown', '02: Northeast', '01: Mid-West', '05: Other',\n", " '04: West', '03: South'], dtype=object),\n", " 'Active_Exposure': array([ 37.583332, 14.5 , 2250. , ..., 354.91653 ,\n", " 1583. , 3501.9167 ]),\n", " 'Total_Exposure': array([ 37.583332, 14.5 , 2252. , ..., 155.0833 ,\n", " 1583. , 3501.9167 ]),\n", " 'Claim_Count': array([ 0, 1, 2, 3, 12, 4, 5, 28, 6, 7, 20, 33, 9,\n", " 21, 48, 8, 26, 14, 10, 13, 15, 24, 23, 17, 22, 25,\n", " 18, 19, 16, 57, 11, 195, 39, 86, 130, 38, 27, 36, 56,\n", " 32, 45, 30, 114, 31, 84, 65, 145, 122, 265, 63, 42, 61,\n", " 173, 70, 29, 66, 47, 79, 35, 82, 49, 40, 59, 46, 34,\n", " 37, 123, 69, 55, 43, 90, 92, 72, 80, 58, 50, 96, 68,\n", " 51, 99, 73, 168, 109, 75, 52, 150, 77, 107, 67, 93, 60,\n", " 41, 429, 115, 44, 53, 85, 64, 54, 146, 110, 134, 219, 135,\n", " 129, 126, 74, 62, 254, 233, 91, 108, 285, 78, 128, 94, 89,\n", " 155, 101, 127, 236, 95, 125, 204, 154, 148, 81, 76, 71, 100,\n", " 83, 106, 113, 138, 158, 153, 232, 239, 336, 202, 185, 111, 170,\n", " 200, 142, 88, 205, 171, 324, 176, 121, 162, 151, 179, 147, 87,\n", " 132, 117, 348, 102, 184, 266, 116, 189, 252, 160, 301, 141, 201,\n", " 97, 104, 103, 317, 174, 98, 166, 181, 105, 365, 143, 167, 120,\n", " 280, 144, 413, 140, 248, 161, 119, 359, 222, 267, 274, 112, 344,\n", " 276, 180, 238, 260, 163, 159, 136, 263, 190, 458]),\n", " 'Count_NH': array([ 0, 1, 7, 3, 2, 9, 5, 11, 4, 21, 48, 26, 24,\n", " 6, 13, 23, 15, 8, 19, 12, 16, 25, 10, 39, 130, 22,\n", " 31, 14, 17, 28, 18, 29, 55, 145, 32, 49, 34, 20, 36,\n", " 35, 46, 40, 122, 57, 41, 42, 92, 61, 45, 33, 56, 99,\n", " 75, 107, 73, 30, 27, 52, 38, 47, 109, 70, 43, 44, 126,\n", " 64, 54, 63, 253, 62, 77, 83, 89, 114, 65, 51, 67, 60,\n", " 96, 88, 72, 53, 37, 59, 154, 79, 123, 58, 98, 50, 323,\n", " 176, 119, 93, 110, 179, 81, 146, 153, 78, 69, 116, 189, 80,\n", " 66, 121, 71, 68, 100, 94, 97, 144, 82, 105, 104, 85]),\n", " 'Count_ALF': array([ 0, 1, 3, 5, 6, 2, 4, 8, 20, 11, 9, 13, 10,\n", " 12, 7, 25, 29, 17, 35, 14, 33, 27, 21, 32, 15, 16,\n", " 30, 53, 45, 24, 19, 18, 23, 28, 22, 47, 56, 58, 89,\n", " 26, 54, 90, 31, 62, 106, 109, 143, 41, 39, 59, 38, 52,\n", " 82, 91, 34, 119, 55]),\n", " 'Count_HHC': array([ 0, 1, 3, 4, 13, 2, 5, 6, 33, 8, 10, 12, 11,\n", " 7, 192, 22, 85, 18, 9, 21, 14, 20, 52, 16, 40, 114,\n", " 54, 121, 244, 31, 23, 61, 19, 139, 45, 15, 25, 24, 17,\n", " 81, 27, 35, 39, 46, 28, 68, 89, 43, 48, 99, 55, 94,\n", " 51, 66, 44, 149, 34, 95, 32, 47, 137, 26, 392, 42, 72,\n", " 30, 155, 41, 62, 64, 202, 56, 37, 53, 83, 98, 29, 73,\n", " 58, 57, 211, 59, 269, 118, 91, 127, 50, 38, 65, 212, 146,\n", " 181, 135, 69, 136, 49, 36, 113, 63, 131, 147, 75, 207, 78,\n", " 308, 189, 111, 166, 157, 60, 109, 79, 154, 86, 148, 67, 229,\n", " 76, 126, 115, 71, 74, 347, 97, 77, 218, 110, 167, 117, 128,\n", " 159, 92, 141, 108, 142, 70, 80, 315, 160, 104, 339, 106, 105,\n", " 156, 103, 196, 249, 390, 88, 82, 152, 359, 190, 87, 265, 120,\n", " 246, 116, 230, 248, 130, 144, 237, 150, 254, 173, 234, 424, 133]),\n", " 'Count_Unk': array([ 0, 1, 5, 2, 8, 3, 4, 9, 6, 26, 10, 11, 18,\n", " 12, 16, 17, 7, 22, 19, 28, 21, 15, 25, 13, 50, 14,\n", " 27, 54, 37, 38, 20, 31, 33, 42, 23, 29, 35, 24, 63,\n", " 73, 71, 39, 58, 32, 56, 102, 30, 66, 60, 49, 43, 41,\n", " 45, 52])}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "incidence_unique_values = {col: incidence_df[col].unique() for col in incidence_df.columns}\n", "incidence_unique_values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 2. Checking for \"Unknown\" Values That Do Not Contribute Information\n", "The dataset contains 627,190 rows and 13 columns. For actuarial analysis, it is essential that the variables 'Issue_Age_Bucket', 'Incurred_Age_Bucket', and 'Issue_Year_Bucket' contain valid information since they are crucial in evaluating claim incidences. Rows with 'Unknown' values in these variables do not contribute to the analysis and can potentially skew the results. Currently, there are 519 'Unknown' values in 'Issue_Age_Bucket', 481 in 'Incurred_Age_Bucket', and 431 in 'Issue_Year_Bucket'. Removing rows with 'Unknown' values in these three variables is a necessary step to ensure data quality and reliability of the analysis.\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Issue_Age_Bucket': 519\n" ] } ], "source": [ "# Calculate the number of 'Unknown' values in the 'Issue_Age_Bucket' column\n", "unknown_issue_age = (incidence_df['Issue_Age_Bucket'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Issue_Age_Bucket': {unknown_issue_age}\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Incurred_Age_Bucket': 481\n" ] } ], "source": [ "unknown_incurred_age = (incidence_df['Incurred_Age_Bucket'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Incurred_Age_Bucket': {unknown_incurred_age}\")\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Issue_Year_Bucket': 431\n" ] } ], "source": [ "unknown_issue_year = (incidence_df['Issue_Year_Bucket'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Issue_Year_Bucket': {unknown_issue_year}\")" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Marital_Status': 406015\n" ] } ], "source": [ "unknown_Marital_Status = (incidence_df['Marital_Status'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Marital_Status': {unknown_Marital_Status}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 3. Eliminate rows where below three variables have 'Unknown' values\n", "- Counts of 'Unknown' values in key variables:\n", "- 'Issue_Age_Bucket': 519\n", "- 'Incurred_Age_Bucket': 481\n", "- 'Issue_Year_Bucket': 431\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataframe shape after elimination: (2088757, 31)\n" ] } ], "source": [ "# Eliminate rows where these three variables have 'Unknown' values\n", "filtered_incidence_df = incidence_df[\n", " (incidence_df['Issue_Age_Bucket'] != 'Unknown') & \n", " (incidence_df['Incurred_Age_Bucket'] != 'Unknown') & \n", " (incidence_df['Issue_Year_Bucket'] != 'Unknown')\n", "]\n", "\n", "# Display the shape of the dataframe after elimination\n", "print(f\"Dataframe shape after elimination: {filtered_incidence_df.shape}\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Group_IndicatorGenderIssue_Age_BucketIncurred_Age_BucketIssue_Year_BucketPolicy_YearMarital_StatusPremium_ClassUnderwriting_TypeCoverage_Type_Bucket...ALF_EP_BucketHHC_EP_BucketRegionActive_ExposureTotal_ExposureClaim_CountCount_NHCount_ALFCount_HHCCount_Unk
0GroupFemale55-5955-592003-20051-3 yearsSingleStandardOtherComprehensive...00Unknown37.58333237.58333200000
1IndividualFemale60-6470-741997-199910-12 yearsSinglePreferredOtherComprehensive...0002: Northeast14.50000014.50000000000
2GroupFemale50-5460-642000-200210-12 yearsUnknownStandardUnknownComprehensive...00Unknown2250.0000002252.00000000000
3IndividualFemale55-5970-741994-199613-15 yearsMarriedStandardOtherComprehensive...0001: Mid-West19.00000019.00000000000
4IndividualMale60-6465-692003-20054-6 yearsMarriedSubstandardFull underwritingComprehensive...902002: Northeast2.0000002.00000000000
..................................................................
2089318IndividualMale55-5960-642012-20141-3 yearsMarriedSubstandardFull underwritingComprehensive...0001: Mid-West7.0833337.08333300000
2089319IndividualMale65-6970-741997-19997-9 yearsMarriedStandardFull underwritingComprehensive...18018002: Northeast2.0000002.00000000000
2089320IndividualFemale65-6975-791994-19967-9 yearsSinglePreferredOtherComprehensive...0001: Mid-West6.0000006.00000000000
2089321IndividualFemale50-5455-591994-19967-9 yearsSingleStandardUnknownComprehensive...00Unknown6.0000006.00000000000
2089322IndividualFemale0-4950-541997-19991-3 yearsSinglePreferredFull underwritingComprehensive...903003: South3.0000003.00000000000
\n", "

2088757 rows × 31 columns

\n", "
" ], "text/plain": [ " Group_Indicator Gender Issue_Age_Bucket Incurred_Age_Bucket \\\n", "0 Group Female 55-59 55-59 \n", "1 Individual Female 60-64 70-74 \n", "2 Group Female 50-54 60-64 \n", "3 Individual Female 55-59 70-74 \n", "4 Individual Male 60-64 65-69 \n", "... ... ... ... ... \n", "2089318 Individual Male 55-59 60-64 \n", "2089319 Individual Male 65-69 70-74 \n", "2089320 Individual Female 65-69 75-79 \n", "2089321 Individual Female 50-54 55-59 \n", "2089322 Individual Female 0-49 50-54 \n", "\n", " Issue_Year_Bucket Policy_Year Marital_Status Premium_Class \\\n", "0 2003-2005 1-3 years Single Standard \n", "1 1997-1999 10-12 years Single Preferred \n", "2 2000-2002 10-12 years Unknown Standard \n", "3 1994-1996 13-15 years Married Standard \n", "4 2003-2005 4-6 years Married Substandard \n", "... ... ... ... ... \n", "2089318 2012-2014 1-3 years Married Substandard \n", "2089319 1997-1999 7-9 years Married Standard \n", "2089320 1994-1996 7-9 years Single Preferred \n", "2089321 1994-1996 7-9 years Single Standard \n", "2089322 1997-1999 1-3 years Single Preferred \n", "\n", " Underwriting_Type Coverage_Type_Bucket ... ALF_EP_Bucket \\\n", "0 Other Comprehensive ... 0 \n", "1 Other Comprehensive ... 0 \n", "2 Unknown Comprehensive ... 0 \n", "3 Other Comprehensive ... 0 \n", "4 Full underwriting Comprehensive ... 90 \n", "... ... ... ... ... \n", "2089318 Full underwriting Comprehensive ... 0 \n", "2089319 Full underwriting Comprehensive ... 180 \n", "2089320 Other Comprehensive ... 0 \n", "2089321 Unknown Comprehensive ... 0 \n", "2089322 Full underwriting Comprehensive ... 90 \n", "\n", " HHC_EP_Bucket Region Active_Exposure Total_Exposure \\\n", "0 0 Unknown 37.583332 37.583332 \n", "1 0 02: Northeast 14.500000 14.500000 \n", "2 0 Unknown 2250.000000 2252.000000 \n", "3 0 01: Mid-West 19.000000 19.000000 \n", "4 20 02: Northeast 2.000000 2.000000 \n", "... ... ... ... ... \n", "2089318 0 01: Mid-West 7.083333 7.083333 \n", "2089319 180 02: Northeast 2.000000 2.000000 \n", "2089320 0 01: Mid-West 6.000000 6.000000 \n", "2089321 0 Unknown 6.000000 6.000000 \n", "2089322 30 03: South 3.000000 3.000000 \n", "\n", " Claim_Count Count_NH Count_ALF Count_HHC Count_Unk \n", "0 0 0 0 0 0 \n", "1 0 0 0 0 0 \n", "2 0 0 0 0 0 \n", "3 0 0 0 0 0 \n", "4 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "2089318 0 0 0 0 0 \n", "2089319 0 0 0 0 0 \n", "2089320 0 0 0 0 0 \n", "2089321 0 0 0 0 0 \n", "2089322 0 0 0 0 0 \n", "\n", "[2088757 rows x 31 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filtered_incidence_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 4. Inspect Underwriting_Type with Unknown value" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Region': 465920\n" ] } ], "source": [ "unknown_Underwriting_Type = (incidence_df['Underwriting_Type'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Region': {unknown_Underwriting_Type}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 5. Inspect Region with Unknown value" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Region': 388271\n" ] } ], "source": [ "unknown_incidence_Region = (incidence_df['Region'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Region': {unknown_incidence_Region}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (b) Impute Region with Conditional Mode Imputation\n", "\n", "#### How Conditional Mode Imputation Helps Incidence Datasets:\n", "\n", "1. **Enhances Data Integrity and Completeness**: In incidence datasets, 'Unknown' values in the 'Region' column can lead to information gaps or biases during analysis. Conditional Mode Imputation, by leveraging relationships known from other variables in the dataset (such as 'Policy_Year', 'Issue_Age_Bucket', etc.), allows for reasoned inference and filling of these unknown regional values. This not only adds to the dataset's completeness but also maintains the intrinsic correlation and consistency among the data.\n", "\n", "2. **Reflects Actual Regional Distribution Characteristics**: By grouping similar records and calculating the modal value of 'Region' for each group based on conditions, Conditional Mode Imputation can more accurately reflect the regional distribution under specific conditions or backgrounds. This approach provides a closer approximation to reality, especially when the distribution of regions strongly correlates with specific variables, offering a more detailed view than simply replacing 'Unknown' with the overall most common region.\n", "\n", "#### Improvement in Analysis Performance:\n", "\n", "1. **Increases Model Accuracy and Interpretability**: A dataset with 'Unknown' values filled provides a more complete picture for analysis models, helping them capture correlations and patterns more accurately. This not only boosts the accuracy of analyses but also enhances the interpretability of model predictions or conclusions, as specific characteristics and trends of each region can be better identified and understood.\n", "\n", "2. **Enhances Reliability of Decision Support**: For tasks that require risk assessment, product design, or market analysis based on incidence datasets, a more complete dataset reflecting the actual distribution of regions offers stronger support. By reducing the uncertainty associated with data missingness, decisions made on such data become more reliable.\n", "\n", "3. **Avoids Analysis Bias**: Excluding or overlooking records with 'Unknown' regions could lead to overrepresentation or neglect of certain regions or demographics, introducing analysis bias. Conditional Mode Imputation allows these records to be included in the analysis but in a way that maintains data consistency and relevance, thereby avoiding potential biases.\n", "\n", "#### Why Policy Year Matters:\n", "\n", "The 'Policy_Year' is a significant variable in incidence datasets for several reasons:\n", "\n", "1. **Temporal Trends**: Policy year can reflect changes in policies, market conditions, or risk profiles over time. Understanding these temporal trends is crucial for accurate risk assessment and forecasting.\n", "\n", "2. **Regional Variations**: Policy uptake and preferences can vary by region in different years, influenced by economic conditions, regulatory changes, or cultural factors. Analyzing data based on policy year allows for a nuanced understanding of these regional variations.\n", "\n", "3. **Product Evolution**: Insurance products and their features might evolve over the years. The policy year helps in distinguishing between different product generations and their impact on incidences.\n", "\n", "4. **Risk Management**: Risk profiles can change over time due to various factors, including changes in underwriting practices or shifts in demographic patterns. The policy year provides a temporal dimension to analyze these changes, enabling more tailored risk management strategies.\n", "\n", "In summary, Conditional Mode Imputation provides a method that ensures data integrity while preserving intrinsic data relationships. The inclusion of variables like 'Policy_Year' enriches the analysis by incorporating temporal and contextual dimensions, making the imputation process and subsequent analyses more aligned with real-world conditions and trends." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/fs/5ct03tj50v1_hbz7dk31c6mw0000gn/T/ipykernel_2098/771386354.py:4: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " filtered_incidence_df['Region'].replace('Unknown', np.nan, inplace=True)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Region\n", "03: South 933136\n", "01: Mid-West 476202\n", "04: West 416651\n", "02: Northeast 257988\n", "05: Other 4780\n", "Name: count, dtype: int64\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/fs/5ct03tj50v1_hbz7dk31c6mw0000gn/T/ipykernel_2098/771386354.py:16: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " filtered_incidence_df['Region'].fillna(overall_mode, inplace=True)\n" ] } ], "source": [ "import numpy as np\n", "\n", "# Step 1: Replace 'Unknown' with np.nan for easier handling with pandas methods\n", "filtered_incidence_df['Region'].replace('Unknown', np.nan, inplace=True)\n", "\n", "# Step 2 & 3: Impute 'Unknown' (now np.nan) in 'Region' based on the mode of each 'Policy_Year' group\n", "for policy_year in filtered_incidence_df['Policy_Year'].unique():\n", " # Compute the mode of 'Region' for the current 'Policy_Year'\n", " mode_region = filtered_incidence_df.loc[filtered_incidence_df['Policy_Year'] == policy_year, 'Region'].mode()\n", " if not mode_region.empty:\n", " # Impute 'Unknown' values with the mode for this subset\n", " filtered_incidence_df.loc[(filtered_incidence_df['Policy_Year'] == policy_year) & (filtered_incidence_df['Region'].isna()), 'Region'] = mode_region[0]\n", "\n", "# If there are still any 'Unknown' (np.nan) values left, fill them with the overall mode\n", "overall_mode = filtered_incidence_df['Region'].mode()[0]\n", "filtered_incidence_df['Region'].fillna(overall_mode, inplace=True)\n", "\n", "# Check the imputation result\n", "print(filtered_incidence_df['Region'].value_counts())\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Group_IndicatorGenderIssue_Age_BucketIncurred_Age_BucketIssue_Year_BucketPolicy_YearMarital_StatusPremium_ClassUnderwriting_TypeCoverage_Type_Bucket...ALF_EP_BucketHHC_EP_BucketRegionActive_ExposureTotal_ExposureClaim_CountCount_NHCount_ALFCount_HHCCount_Unk
0GroupFemale55-5955-592003-20051-3 yearsSingleStandardOtherComprehensive...0003: South37.58333237.58333200000
1IndividualFemale60-6470-741997-199910-12 yearsSinglePreferredOtherComprehensive...0002: Northeast14.50000014.50000000000
2GroupFemale50-5460-642000-200210-12 yearsUnknownStandardUnknownComprehensive...0003: South2250.0000002252.00000000000
3IndividualFemale55-5970-741994-199613-15 yearsMarriedStandardOtherComprehensive...0001: Mid-West19.00000019.00000000000
4IndividualMale60-6465-692003-20054-6 yearsMarriedSubstandardFull underwritingComprehensive...902002: Northeast2.0000002.00000000000
..................................................................
2089318IndividualMale55-5960-642012-20141-3 yearsMarriedSubstandardFull underwritingComprehensive...0001: Mid-West7.0833337.08333300000
2089319IndividualMale65-6970-741997-19997-9 yearsMarriedStandardFull underwritingComprehensive...18018002: Northeast2.0000002.00000000000
2089320IndividualFemale65-6975-791994-19967-9 yearsSinglePreferredOtherComprehensive...0001: Mid-West6.0000006.00000000000
2089321IndividualFemale50-5455-591994-19967-9 yearsSingleStandardUnknownComprehensive...0003: South6.0000006.00000000000
2089322IndividualFemale0-4950-541997-19991-3 yearsSinglePreferredFull underwritingComprehensive...903003: South3.0000003.00000000000
\n", "

2088757 rows × 31 columns

\n", "
" ], "text/plain": [ " Group_Indicator Gender Issue_Age_Bucket Incurred_Age_Bucket \\\n", "0 Group Female 55-59 55-59 \n", "1 Individual Female 60-64 70-74 \n", "2 Group Female 50-54 60-64 \n", "3 Individual Female 55-59 70-74 \n", "4 Individual Male 60-64 65-69 \n", "... ... ... ... ... \n", "2089318 Individual Male 55-59 60-64 \n", "2089319 Individual Male 65-69 70-74 \n", "2089320 Individual Female 65-69 75-79 \n", "2089321 Individual Female 50-54 55-59 \n", "2089322 Individual Female 0-49 50-54 \n", "\n", " Issue_Year_Bucket Policy_Year Marital_Status Premium_Class \\\n", "0 2003-2005 1-3 years Single Standard \n", "1 1997-1999 10-12 years Single Preferred \n", "2 2000-2002 10-12 years Unknown Standard \n", "3 1994-1996 13-15 years Married Standard \n", "4 2003-2005 4-6 years Married Substandard \n", "... ... ... ... ... \n", "2089318 2012-2014 1-3 years Married Substandard \n", "2089319 1997-1999 7-9 years Married Standard \n", "2089320 1994-1996 7-9 years Single Preferred \n", "2089321 1994-1996 7-9 years Single Standard \n", "2089322 1997-1999 1-3 years Single Preferred \n", "\n", " Underwriting_Type Coverage_Type_Bucket ... ALF_EP_Bucket \\\n", "0 Other Comprehensive ... 0 \n", "1 Other Comprehensive ... 0 \n", "2 Unknown Comprehensive ... 0 \n", "3 Other Comprehensive ... 0 \n", "4 Full underwriting Comprehensive ... 90 \n", "... ... ... ... ... \n", "2089318 Full underwriting Comprehensive ... 0 \n", "2089319 Full underwriting Comprehensive ... 180 \n", "2089320 Other Comprehensive ... 0 \n", "2089321 Unknown Comprehensive ... 0 \n", "2089322 Full underwriting Comprehensive ... 90 \n", "\n", " HHC_EP_Bucket Region Active_Exposure Total_Exposure \\\n", "0 0 03: South 37.583332 37.583332 \n", "1 0 02: Northeast 14.500000 14.500000 \n", "2 0 03: South 2250.000000 2252.000000 \n", "3 0 01: Mid-West 19.000000 19.000000 \n", "4 20 02: Northeast 2.000000 2.000000 \n", "... ... ... ... ... \n", "2089318 0 01: Mid-West 7.083333 7.083333 \n", "2089319 180 02: Northeast 2.000000 2.000000 \n", "2089320 0 01: Mid-West 6.000000 6.000000 \n", "2089321 0 03: South 6.000000 6.000000 \n", "2089322 30 03: South 3.000000 3.000000 \n", "\n", " Claim_Count Count_NH Count_ALF Count_HHC Count_Unk \n", "0 0 0 0 0 0 \n", "1 0 0 0 0 0 \n", "2 0 0 0 0 0 \n", "3 0 0 0 0 0 \n", "4 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "2089318 0 0 0 0 0 \n", "2089319 0 0 0 0 0 \n", "2089320 0 0 0 0 0 \n", "2089321 0 0 0 0 0 \n", "2089322 0 0 0 0 0 \n", "\n", "[2088757 rows x 31 columns]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filtered_incidence_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## III. Data Cleaning Process for \"Termination Datasets\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (a) Inspect for the presence of outliers and missing values in the variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Check out the dataframe at first and make sure the unique vlaue of each variable is reasonable " ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GenderIncurred_Age_BucketIncurred_Year_BucketClaim_TypeRegionDiagnosis_CategoryClaim_DurationExposureDeathsRecoveryTerminationsBenefit_ExpiryOthers_Terminations
0FemaleUnknown2009-2010HCCUnknownUnknown807702210
1FemaleUnknown2003-2004NHUnknownUnknown864910100
2FemaleUnknown2005-2006OtherUnknownUnknown881610100
3FemaleUnknown2003-2004ALFUnknownUnknown1142910100
4FemaleUnknown< 2001NHUnknownUnknown45651600
..........................................
627185Male85-892007-2008NHUnknown05: Injury33200000
627186Male75-792007-2008HCC03: SouthUnknown316230300
627187Female80-842013-2014HCC01: Mid-West07: Stroke23600000
627188Male70-742001-2002HCC02: NortheastUnknown30700000
627189Female85-892003-2004ALFUnknownUnknown329510100
\n", "

627190 rows × 13 columns

\n", "
" ], "text/plain": [ " Gender Incurred_Age_Bucket Incurred_Year_Bucket Claim_Type \\\n", "0 Female Unknown 2009-2010 HCC \n", "1 Female Unknown 2003-2004 NH \n", "2 Female Unknown 2005-2006 Other \n", "3 Female Unknown 2003-2004 ALF \n", "4 Female Unknown < 2001 NH \n", "... ... ... ... ... \n", "627185 Male 85-89 2007-2008 NH \n", "627186 Male 75-79 2007-2008 HCC \n", "627187 Female 80-84 2013-2014 HCC \n", "627188 Male 70-74 2001-2002 HCC \n", "627189 Female 85-89 2003-2004 ALF \n", "\n", " Region Diagnosis_Category Claim_Duration Exposure Deaths \\\n", "0 Unknown Unknown 80 77 0 \n", "1 Unknown Unknown 86 49 1 \n", "2 Unknown Unknown 88 16 1 \n", "3 Unknown Unknown 114 29 1 \n", "4 Unknown Unknown 4 56 5 \n", "... ... ... ... ... ... \n", "627185 Unknown 05: Injury 33 2 0 \n", "627186 03: South Unknown 31 62 3 \n", "627187 01: Mid-West 07: Stroke 23 6 0 \n", "627188 02: Northeast Unknown 30 7 0 \n", "627189 Unknown Unknown 32 95 1 \n", "\n", " Recovery Terminations Benefit_Expiry Others_Terminations \n", "0 2 2 1 0 \n", "1 0 1 0 0 \n", "2 0 1 0 0 \n", "3 0 1 0 0 \n", "4 1 6 0 0 \n", "... ... ... ... ... \n", "627185 0 0 0 0 \n", "627186 0 3 0 0 \n", "627187 0 0 0 0 \n", "627188 0 0 0 0 \n", "627189 0 1 0 0 \n", "\n", "[627190 rows x 13 columns]" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "termination_df" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Gender': array(['Female', 'Male'], dtype=object),\n", " 'Incurred_Age_Bucket': array(['Unknown', '65-69', '90+', '80-84', ' 0-49', '60-64', '85-89',\n", " '70-74', '75-79', '55-59', '50-54'], dtype=object),\n", " 'Incurred_Year_Bucket': array(['2009-2010', '2003-2004', '2005-2006', ' < 2001', '2011-2012',\n", " '2015-2016', '2007-2008', 'Unknown', '2001-2002', '2013-2014'],\n", " dtype=object),\n", " 'Claim_Type': array(['HCC', 'NH', 'Other', 'ALF'], dtype=object),\n", " 'Region': array(['Unknown', '04: West', '03: South', '02: Northeast',\n", " '01: Mid-West', '05: Other'], dtype=object),\n", " 'Diagnosis_Category': array(['Unknown', '04: Circulatory', \"01: Alzheimer's and Dementia\",\n", " '06: Nervous System', '07: Stroke', '05: Injury', '08: Other',\n", " '03: Cancer', '02: Arthritis'], dtype=object),\n", " 'Claim_Duration': array([' 80', ' 86', ' 88', '114', ' 4', ' 1', ' 50', ' 83', ' 18',\n", " ' 41', ' 15', ' 32', '161', '157', ' 62', ' 87', '207', ' 16',\n", " ' 2', ' 33', ' 22', '205', ' 29', ' 91', '162', ' 37', ' 39',\n", " ' 35', '112', ' 99', ' 43', '174', ' 27', '106', ' 53', '150',\n", " ' 23', ' 3', '117', '125', '101', ' 73', ' 49', ' 13', ' 17',\n", " ' 55', ' 25', ' 74', ' 67', ' 8', ' 24', ' 69', ' 89', ' 44',\n", " ' 61', ' 65', ' 26', '116', ' 21', ' 45', ' 58', ' 71', ' 84',\n", " ' 64', '183', '110', '158', ' 38', ' 57', '204', '103', ' 40',\n", " '181', ' 93', ' 14', '108', ' 72', ' 5', '263', ' 48', ' 75',\n", " ' 54', ' 47', ' 10', '182', ' 11', ' 42', ' 68', '109', ' 70',\n", " ' 98', ' 60', ' 36', ' 20', ' 82', ' 59', ' 79', '115', ' 7',\n", " ' 95', '129', '120', ' 52', ' 97', '105', '171', ' 9', ' 46',\n", " ' 19', ' 6', '243', ' 81', ' 92', '124', ' 94', '113', ' 76',\n", " ' 28', '271', '145', ' 85', ' 12', ' 63', ' 56', '100', ' 66',\n", " '268', ' 31', '102', '191', '209', ' 77', '180', '179', ' 96',\n", " '164', ' 90', '111', '122', '176', ' 30', '142', '246', '188',\n", " '274', ' 78', ' 51', '189', '195', '170', '130', '202', '152',\n", " '148', '177', '190', '151', '128', '175', '107', '165', '104',\n", " '193', '133', ' 34', '167', '198', '192', '199', '212', '216',\n", " '146', '121', '168', '185', '136', '143', '147', '154', '200',\n", " '256', '126', '228', '134', '277', '123', '217', '218', '153',\n", " '139', '186', '118', '166', '132', '279', '141', '196', '201',\n", " '173', '155', '276', '137', '163', '219', '127', '240', '206',\n", " '156', '131', '178', '160', '135', '215', '253', '281', '159',\n", " '262', '213', '119', '203', '211', '254', '138', '140', '210',\n", " '220', '144', '223', '149', '232', '194', '237', '226', '184',\n", " '169', '172', '231', '187', '208', '265', '248', '255', '225',\n", " '197', '234', '245', '214', '221', '222', '249', '269', '251',\n", " '227', '236', '244', '259', '229', '233', '270', '250', '238',\n", " '252', '224', '282', '258', '257', '280', '264', '273', '235',\n", " '247', '230', '239', '261', '241', '272', '278', '242', '267',\n", " '266', '260', '275', '283'], dtype=object),\n", " 'Exposure': array([ 77, 49, 16, 29, 56, 68, 30, 58, 32, 51, 42,\n", " 57, 19, 35, 21, 10, 66, 40, 26, 39, 15, 48,\n", " 46, 17, 75, 64, 44, 52, 50, 59, 74, 94, 55,\n", " 228, 28, 31, 73, 84, 61, 65, 36, 43, 60, 27,\n", " 5, 69, 53, 18, 62, 4, 3, 47, 45, 76, 41,\n", " 24, 67, 54, 22, 71, 34, 37, 12, 162, 14, 13,\n", " 38, 23, 86, 25, 11, 115, 20, 102, 63, 7, 72,\n", " 79, 81, 223, 175, 33, 8, 70, 87, 9, 133, 105,\n", " 107, 83, 130, 104, 80, 100, 138, 144, 176, 85, 106,\n", " 131, 78, 149, 90, 98, 99, 140, 2, 82, 153, 95,\n", " 112, 161, 97, 183, 142, 145, 190, 6, 205, 136, 124,\n", " 127, 196, 116, 206, 109, 238, 230, 91, 192, 103, 88,\n", " 137, 89, 120, 214, 174, 141, 182, 118, 110, 246, 96,\n", " 108, 128, 280, 155, 119, 202, 164, 331, 117, 114, 255,\n", " 170, 203, 187, 101, 243, 179, 222, 372, 167, 168, 425,\n", " 93, 122, 171, 132, 113, 143, 126, 358, 135, 224, 121,\n", " 394, 353, 154, 306, 534, 125, 92, 172, 287, 195, 163,\n", " 210, 579, 229, 173, 208, 180, 201, 158, 377, 111, 245,\n", " 226, 384, 248, 489, 186, 236, 249, 184, 160, 435, 282,\n", " 268, 272, 254, 269, 250, 311, 221, 198, 157, 156, 256,\n", " 288, 146, 418, 151, 237, 181, 185, 177, 266, 300, 278,\n", " 792, 275, 147, 420, 296, 225, 233, 129, 529, 220, 193,\n", " 209, 273, 152, 415, 308, 219, 169, 592, 334, 369, 264,\n", " 303, 375, 241, 239, 150, 312, 370, 385, 297, 362, 317,\n", " 242, 550, 413, 615, 397, 676, 134, 148, 216, 199, 191,\n", " 355, 166, 165, 159, 530, 452, 309, 514, 123, 329, 259,\n", " 437, 472, 294, 1092, 345, 215, 705, 178, 412, 281, 274,\n", " 401, 234, 346, 302, 323, 438, 213, 267, 461, 310, 538,\n", " 218, 204, 440, 379, 335, 290, 337, 194, 674, 277, 327,\n", " 139, 291, 456, 258, 188, 442, 298, 292, 812, 240, 227,\n", " 349, 231, 405, 605, 247, 276, 232, 390, 864, 431, 285,\n", " 207, 411, 244, 395, 251, 336, 197, 325, 427, 522, 271,\n", " 212, 319, 441, 262, 321, 293, 432, 314, 299, 698, 211,\n", " 286, 340, 270, 260, 304, 333, 536, 419, 189, 200, 393,\n", " 665, 392, 322, 451, 235, 316, 305, 338, 610, 253, 408,\n", " 460, 341, 284, 543, 467, 217, 523, 279, 347, 387, 263,\n", " 473, 324, 265, 366, 252, 454, 402, 289, 391, 368, 388,\n", " 339, 611, 429, 789, 443, 743, 330, 434, 505, 328, 484,\n", " 257, 410, 446, 295, 586, 422, 361, 378, 389, 360, 261,\n", " 439, 469, 344, 574, 563, 383, 356, 315, 381, 583, 348,\n", " 428, 398, 515, 307, 364, 465, 332, 380, 350, 581, 479,\n", " 283, 541, 727, 318, 582, 417, 575, 396, 588, 433, 430,\n", " 352, 506, 462, 798, 722, 645, 357, 693, 313, 774, 654,\n", " 512, 448, 376, 612, 445, 718, 343, 354, 444, 386, 599,\n", " 607, 544, 488, 320, 365, 453, 468, 496, 604, 595, 682,\n", " 342, 359, 301, 647, 802, 426, 551, 791, 680, 526, 524,\n", " 371, 518, 475, 520, 525, 367, 463, 471, 836, 400, 508,\n", " 406, 511, 501, 495, 560, 713, 590, 653, 618, 414, 757,\n", " 403, 500, 636, 509, 688, 399, 499, 670, 492, 552, 707,\n", " 494, 516, 738, 382, 584, 326, 373, 577, 513, 795, 474,\n", " 497, 600, 450, 351, 646, 613, 487, 458, 455, 486, 478,\n", " 788, 553, 597, 416, 564, 687, 621, 734, 807, 464, 587,\n", " 781, 700, 589, 540, 535, 780, 566, 482, 630, 602, 470,\n", " 902, 633, 689, 601, 449, 507, 363, 660, 594, 447, 374,\n", " 922, 805, 762, 531, 658, 493, 606, 642, 409, 585, 424,\n", " 510, 692, 436, 759, 650, 503, 672, 952, 517, 644, 608,\n", " 799, 421, 778, 639, 940, 547, 457, 490, 423, 555, 521,\n", " 967, 565, 731, 407, 766, 911, 498, 533, 711, 1284, 724,\n", " 572, 876, 641, 477, 619, 491, 783, 679, 803, 545, 404,\n", " 502, 567, 769, 573, 634, 709, 519, 744, 527, 483, 747,\n", " 532, 539, 1150, 995, 702, 790, 562, 754, 837, 659, 666,\n", " 542, 620, 569, 782, 549, 974, 760, 466, 719]),\n", " 'Deaths': array([ 0, 1, 5, 4, 2, 3, 12, 6, 8, 7, 16, 10, 50,\n", " 9, 103, 13, 27, 14, 11, 41, 15, 31, 49, 40, 24, 34,\n", " 18, 22, 32, 106, 20, 55, 29, 30, 17, 65, 21, 44, 42,\n", " 38, 28, 19, 66, 73, 23, 58, 51, 63, 59, 53, 25, 57,\n", " 39, 26, 33, 48, 80, 36, 37, 56, 52, 54, 46, 61, 81,\n", " 45, 47, 83, 35, 71, 91, 64, 62, 77, 79, 110, 99, 43,\n", " 74, 60, 95, 134, 76, 67]),\n", " 'Recovery': array([ 2, 0, 1, 5, 4, 3, 10, 6, 7, 8, 15, 58, 9,\n", " 22, 14, 26, 34, 28, 30, 23, 57, 11, 40, 31, 35, 21,\n", " 20, 17, 116, 13, 19, 18, 27, 12, 16, 24, 25, 29, 41,\n", " 70, 33, 38, 39, 55, 54, 43, 102, 108, 95, 76, 32, 82,\n", " 37, 44, 46, 47, 48, 50, 56, 36, 72, 67, 53, 73, 42,\n", " 71, 88, 65, 100, 69, 84, 167, 86, 121, 106, 63, 64, 97,\n", " 59, 75]),\n", " 'Terminations': array([ 2, 1, 6, 9, 0, 3, 4, 7, 5, 8, 22, 11, 10,\n", " 12, 13, 38, 17, 120, 43, 16, 18, 209, 46, 40, 57, 25,\n", " 19, 14, 104, 15, 20, 76, 34, 51, 73, 62, 21, 50, 28,\n", " 83, 42, 61, 228, 37, 36, 30, 58, 23, 35, 101, 27, 24,\n", " 55, 49, 26, 60, 142, 63, 29, 33, 116, 44, 31, 72, 75,\n", " 32, 65, 39, 45, 82, 52, 41, 89, 84, 66, 106, 69, 68,\n", " 162, 176, 121, 163, 114, 59, 90, 64, 135, 109, 74, 179, 81,\n", " 54, 67, 47, 87, 98, 88, 79, 48, 85, 126, 53, 110, 70,\n", " 105, 93, 138, 102, 71, 166, 100, 144, 145, 155, 56, 80, 99,\n", " 134, 183, 136, 119, 147, 280, 91, 107, 117, 111, 169, 271, 230,\n", " 96, 150, 181, 149]),\n", " 'Benefit_Expiry': array([ 1, 0, 2, 3, 4, 5, 8, 6, 20, 22, 7, 27, 28, 25, 10, 9, 12,\n", " 18, 11, 13, 21, 16, 26, 14, 23, 29, 17, 32, 19, 15]),\n", " 'Others_Terminations': array([ 0, 1, 2, 4, 7, 6, 3, 12, 5, 80, 10, 39, 27, 8, 24, 25, 54,\n", " 13, 16, 11, 30, 9, 23, 29, 17, 18, 14, 51, 37, 53, 20, 26, 19, 33,\n", " 22, 46, 31, 34, 15, 21, 66, 48, 61, 28])}" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "termination_unique_values = {col: termination_df[col].unique() for col in termination_df.columns}\n", "termination_unique_values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Checking for \"Unknown\" Values That Do Not Contribute Information" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [], "source": [ "termination_df['Claim_Duration'] = termination_df['Claim_Duration'].replace('283+', '283')" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Incurred_Age_Bucket': 7378\n" ] } ], "source": [ "# Calculate the number of 'Unknown' values in the 'Incurred_Age_Bucket' column\n", "unknown_incurred_age_bucket = (termination_df['Incurred_Age_Bucket'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Incurred_Age_Bucket': {unknown_incurred_age_bucket}\")" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Incurred_Year_Bucket': 778\n" ] } ], "source": [ "# Calculate the number of 'Unknown' values in the 'Incurred_Year_Bucket' column\n", "unknown_incurred_year_bucket = (termination_df['Incurred_Year_Bucket'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Incurred_Year_Bucket': {unknown_incurred_year_bucket}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Eliminate rows where below three variables have 'Unknown' values\n", "- Counts of 'Unknown' values in key variables:\n", "- 'Incurred_Age_Bucket': 7378\n", "- 'Incurred_Age_Bucket': 778" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataframe shape after elimination: (619812, 13)\n" ] } ], "source": [ "# Eliminate rows where these three variables have 'Unknown' values\n", "filtered_termination_df = termination_df[\n", " (termination_df['Incurred_Age_Bucket'] != 'Unknown') & \n", " (termination_df['Incurred_Year_Bucket'] != 'Unknown')\n", "]\n", "\n", "# Display the shape of the dataframe after elimination\n", "print(f\"Dataframe shape after elimination: {filtered_termination_df.shape}\")" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Gender': array(['Female', 'Male'], dtype=object),\n", " 'Incurred_Age_Bucket': array(['65-69', '90+', '80-84', ' 0-49', '60-64', '85-89', '70-74',\n", " '75-79', '55-59', '50-54'], dtype=object),\n", " 'Incurred_Year_Bucket': array(['2009-2010', '2005-2006', ' < 2001', '2013-2014', '2003-2004',\n", " '2015-2016', '2007-2008', '2011-2012', '2001-2002'], dtype=object),\n", " 'Claim_Type': array(['HCC', 'NH', 'ALF', 'Other'], dtype=object),\n", " 'Region': array(['04: West', '03: South', 'Unknown', '02: Northeast',\n", " '01: Mid-West', '05: Other'], dtype=object),\n", " 'Diagnosis_Category': array(['04: Circulatory', 'Unknown', \"01: Alzheimer's and Dementia\",\n", " '06: Nervous System', '07: Stroke', '05: Injury', '08: Other',\n", " '03: Cancer', '02: Arthritis'], dtype=object),\n", " 'Claim_Duration': array([' 17', ' 20', ' 93', ' 23', ' 36', ' 8', ' 10', ' 1', ' 29',\n", " ' 65', ' 48', ' 6', ' 50', ' 30', ' 13', ' 18', ' 75', ' 76',\n", " ' 12', ' 47', ' 94', ' 26', ' 70', ' 37', ' 22', ' 86', ' 38',\n", " ' 39', ' 51', ' 42', '119', ' 81', ' 14', ' 35', ' 78', ' 64',\n", " ' 80', ' 15', ' 24', ' 27', ' 7', ' 60', '130', ' 55', ' 34',\n", " ' 69', ' 3', ' 58', ' 44', ' 73', ' 11', ' 32', '100', ' 54',\n", " '111', ' 41', '104', ' 98', ' 21', ' 31', ' 28', ' 67', ' 71',\n", " ' 33', ' 2', ' 49', '108', '106', ' 52', '126', ' 45', ' 46',\n", " ' 5', ' 4', ' 16', ' 25', ' 79', ' 62', '103', '114', ' 77',\n", " ' 72', '125', ' 87', ' 85', ' 66', ' 61', ' 43', ' 19', ' 89',\n", " ' 57', '144', ' 90', ' 56', ' 82', ' 68', ' 9', ' 59', '118',\n", " ' 53', ' 88', '133', '169', '105', '110', ' 74', ' 63', '101',\n", " '116', '107', ' 99', '122', '139', ' 96', ' 97', ' 83', '155',\n", " '102', '113', '117', ' 40', ' 84', '120', '115', '128', '121',\n", " ' 95', '156', '109', '193', ' 92', '147', '182', '134', ' 91',\n", " '123', '142', '112', '127', '137', '179', '131', '143', '136',\n", " '170', '138', '172', '135', '188', '221', '165', '185', '157',\n", " '148', '145', '150', '177', '152', '154', '146', '153', '124',\n", " '140', '129', '196', '151', '160', '141', '158', '149', '192',\n", " '190', '132', '161', '166', '187', '200', '199', '162', '205',\n", " '173', '194', '171', '167', '189', '209', '175', '180', '159',\n", " '208', '202', '198', '164', '168', '222', '197', '176', '184',\n", " '174', '163', '215', '181', '195', '214', '217', '178', '206',\n", " '225', '183', '210', '191', '211', '216', '234', '201', '241',\n", " '186', '236', '207', '239', '229', '204', '228', '230', '203',\n", " '231', '219', '220', '223', '218', '224', '213', '237', '232',\n", " '226', '235', '242', '212', '238', '283', '240', '227', '233'],\n", " dtype=object),\n", " 'Exposure': array([ 2, 108, 3, 142, 8, 15, 45, 5, 4, 20, 16,\n", " 6, 116, 9, 28, 17, 40, 7, 32, 53, 10, 35,\n", " 25, 81, 23, 65, 128, 51, 14, 18, 107, 280, 56,\n", " 11, 24, 155, 124, 12, 50, 49, 22, 119, 202, 19,\n", " 21, 47, 182, 36, 41, 60, 13, 44, 48, 66, 164,\n", " 106, 52, 104, 31, 26, 161, 331, 75, 29, 76, 57,\n", " 77, 42, 55, 61, 117, 68, 58, 30, 71, 33, 27,\n", " 63, 43, 114, 73, 109, 37, 102, 83, 255, 170, 203,\n", " 149, 64, 230, 34, 187, 38, 101, 243, 179, 222, 372,\n", " 46, 153, 67, 100, 167, 112, 120, 39, 59, 168, 78,\n", " 88, 69, 425, 93, 72, 122, 110, 89, 130, 79, 145,\n", " 171, 131, 132, 127, 113, 143, 80, 90, 70, 126, 86,\n", " 358, 74, 97, 54, 85, 135, 224, 121, 394, 353, 82,\n", " 154, 223, 62, 99, 103, 306, 228, 137, 534, 125, 92,\n", " 172, 287, 115, 105, 140, 195, 163, 210, 95, 579, 229,\n", " 173, 208, 180, 136, 201, 158, 377, 111, 94, 245, 226,\n", " 384, 248, 489, 186, 236, 98, 84, 249, 184, 160, 435,\n", " 96, 141, 282, 268, 272, 254, 269, 250, 118, 87, 311,\n", " 221, 91, 198, 157, 156, 256, 288, 146, 418, 151, 237,\n", " 181, 185, 177, 266, 144, 300, 278, 792, 275, 147, 420,\n", " 296, 225, 233, 129, 529, 220, 193, 192, 133, 209, 273,\n", " 152, 415, 183, 308, 219, 162, 169, 592, 334, 138, 369,\n", " 264, 303, 375, 241, 239, 196, 150, 312, 370, 385, 297,\n", " 362, 317, 242, 550, 413, 615, 397, 676, 134, 148, 216,\n", " 199, 205, 191, 238, 355, 166, 214, 165, 159, 530, 452,\n", " 176, 309, 514, 123, 329, 175, 259, 437, 472, 294, 1092,\n", " 345, 215, 705, 178, 412, 281, 190, 274, 401, 234, 346,\n", " 302, 323, 438, 213, 267, 461, 310, 538, 218, 204, 440,\n", " 379, 335, 290, 337, 194, 674, 277, 327, 139, 174, 291,\n", " 456, 258, 188, 442, 298, 292, 812, 240, 227, 349, 231,\n", " 405, 605, 247, 276, 206, 232, 390, 864, 431, 285, 207,\n", " 411, 244, 395, 251, 336, 197, 325, 427, 522, 271, 319,\n", " 212, 441, 262, 321, 293, 432, 314, 299, 698, 211, 286,\n", " 340, 270, 260, 304, 333, 536, 419, 189, 200, 393, 665,\n", " 392, 322, 451, 235, 316, 305, 338, 610, 253, 408, 460,\n", " 341, 284, 543, 467, 217, 523, 279, 347, 387, 263, 473,\n", " 324, 265, 366, 252, 454, 402, 289, 391, 368, 388, 339,\n", " 611, 429, 789, 443, 743, 330, 246, 434, 505, 328, 484,\n", " 257, 410, 446, 295, 586, 422, 361, 378, 389, 360, 261,\n", " 439, 469, 344, 574, 563, 383, 356, 315, 381, 583, 348,\n", " 428, 398, 515, 307, 364, 465, 332, 380, 350, 581, 479,\n", " 283, 541, 727, 318, 582, 417, 575, 396, 588, 433, 430,\n", " 352, 506, 462, 798, 722, 645, 357, 693, 313, 774, 654,\n", " 512, 448, 376, 612, 445, 718, 343, 354, 444, 386, 599,\n", " 607, 544, 488, 320, 365, 453, 468, 496, 604, 595, 682,\n", " 342, 359, 301, 647, 802, 426, 551, 791, 680, 526, 524,\n", " 371, 518, 475, 520, 525, 367, 463, 471, 836, 400, 508,\n", " 406, 511, 501, 495, 560, 713, 590, 653, 618, 414, 757,\n", " 403, 500, 636, 509, 688, 399, 499, 670, 492, 552, 707,\n", " 494, 516, 738, 382, 584, 326, 373, 577, 513, 795, 474,\n", " 497, 600, 450, 351, 646, 613, 487, 458, 455, 486, 478,\n", " 788, 553, 597, 416, 564, 687, 621, 734, 807, 464, 587,\n", " 781, 700, 589, 540, 535, 780, 566, 482, 630, 602, 470,\n", " 902, 633, 689, 601, 449, 507, 363, 660, 594, 447, 374,\n", " 922, 805, 762, 531, 658, 493, 606, 642, 409, 585, 424,\n", " 510, 692, 436, 759, 650, 503, 672, 952, 517, 644, 608,\n", " 799, 421, 778, 639, 940, 547, 457, 490, 423, 555, 521,\n", " 967, 565, 731, 407, 766, 911, 498, 533, 711, 1284, 724,\n", " 572, 876, 641, 477, 619, 491, 783, 679, 803, 545, 404,\n", " 502, 567, 769, 573, 634, 709, 519, 744, 527, 483, 747,\n", " 532, 539, 1150, 995, 702, 790, 562, 754, 837, 659, 666,\n", " 542, 620, 569, 782, 549, 974, 760, 466, 719]),\n", " 'Deaths': array([ 0, 3, 1, 2, 6, 5, 4, 16, 7, 10, 50, 12, 9,\n", " 103, 8, 13, 27, 14, 11, 41, 15, 31, 49, 40, 24, 34,\n", " 18, 22, 32, 106, 20, 55, 29, 30, 17, 65, 21, 44, 42,\n", " 38, 28, 19, 66, 73, 23, 58, 51, 63, 59, 53, 25, 57,\n", " 39, 26, 33, 48, 80, 36, 37, 56, 52, 54, 46, 61, 81,\n", " 45, 47, 83, 35, 71, 91, 64, 62, 77, 79, 110, 99, 43,\n", " 74, 60, 95, 134, 76, 67]),\n", " 'Recovery': array([ 0, 3, 7, 1, 2, 15, 4, 8, 58, 9, 22, 6, 5,\n", " 14, 26, 34, 28, 30, 23, 10, 57, 11, 40, 31, 35, 21,\n", " 20, 17, 116, 13, 19, 18, 27, 12, 16, 24, 25, 29, 41,\n", " 70, 33, 38, 39, 55, 54, 43, 102, 108, 95, 76, 32, 82,\n", " 37, 44, 46, 47, 48, 50, 56, 36, 72, 67, 53, 73, 42,\n", " 71, 88, 65, 100, 69, 84, 167, 86, 121, 106, 63, 64, 97,\n", " 59, 75]),\n", " 'Terminations': array([ 0, 6, 1, 4, 8, 2, 3, 5, 7, 38, 13, 10, 17,\n", " 12, 120, 11, 43, 9, 16, 18, 209, 46, 40, 57, 25, 19,\n", " 14, 22, 104, 15, 20, 76, 34, 51, 73, 62, 21, 50, 28,\n", " 83, 42, 61, 228, 37, 36, 30, 58, 23, 35, 101, 27, 24,\n", " 55, 49, 26, 60, 142, 63, 29, 33, 116, 44, 31, 72, 75,\n", " 32, 65, 39, 45, 82, 52, 41, 89, 84, 66, 106, 69, 68,\n", " 162, 176, 121, 163, 114, 59, 90, 64, 135, 109, 74, 179, 81,\n", " 54, 67, 47, 87, 98, 88, 79, 48, 85, 126, 53, 110, 70,\n", " 105, 93, 138, 102, 71, 166, 100, 144, 145, 155, 56, 80, 99,\n", " 134, 183, 136, 119, 147, 280, 91, 107, 117, 111, 169, 271, 230,\n", " 96, 150, 181, 149]),\n", " 'Benefit_Expiry': array([ 0, 1, 2, 3, 4, 5, 8, 6, 20, 22, 7, 27, 28, 25, 10, 9, 12,\n", " 18, 11, 13, 21, 16, 26, 14, 23, 29, 17, 32, 19, 15]),\n", " 'Others_Terminations': array([ 0, 1, 2, 7, 6, 3, 12, 5, 80, 4, 10, 39, 27, 8, 24, 25, 54,\n", " 13, 16, 11, 30, 9, 23, 29, 17, 18, 14, 51, 37, 53, 20, 26, 19, 33,\n", " 22, 46, 31, 34, 15, 21, 66, 48, 61, 28])}" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "filtered_termination_df\n", "\n", "termination_unique_values = {col: filtered_termination_df[col].unique() for col in filtered_termination_df.columns}\n", "termination_unique_values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4. Inspect Region with Unknown value" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of 'Unknown' values in 'Region': 157344\n" ] } ], "source": [ "unknown_termination_Region = (termination_df['Region'] == 'Unknown').sum()\n", "print(f\"Number of 'Unknown' values in 'Region': {unknown_termination_Region}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (b) Impute Region with Conditional Mode Imputation\n", "\n", "Choosing \"Diagnosis_Category\" as the variable for conditional imputation of \"Region\" in a long-term care termination dataset is influenced by several considerations that reflect the interplay between geographic regions and healthcare patterns, especially in the context of diagnoses that lead to long-term care needs. Here's why \"Diagnosis_Category\" can be a significant choice for this purpose:\n", "\n", "1. **Regional Health Trends and Prevalence:** Different regions may exhibit varying prevalence rates for certain health conditions due to factors like environmental influences, lifestyle, genetic predispositions, and availability of healthcare services. For instance, certain areas might have higher incidences of lifestyle-related diseases due to the prevalent lifestyle choices of their inhabitants, while others might have higher rates of conditions linked to environmental factors.\n", "\n", "2. **Access to Healthcare Facilities:** The distribution of specialized healthcare facilities and services can vary significantly across regions. Some regions might have better facilities for treating specific conditions, leading to a higher likelihood of patients with those conditions being concentrated in those areas. Imputing \"Region\" based on \"Diagnosis_Category\" can indirectly account for these disparities by assuming that patients are likely to seek care where it's most accessible and specialized for their conditions.\n", "\n", "3. **Cultural and Socioeconomic Factors:** Cultural attitudes towards certain conditions and the socioeconomic status of populations in different regions can influence the diagnosis rates of certain categories. For example, regions with higher socioeconomic status might have better health literacy and access to diagnostic services, leading to higher reported incidences of certain conditions.\n", "\n", "4. **Public Health Policies and Programs:** The focus of public health initiatives can vary by region, influenced by the prevalent health issues in those areas. This can affect the rates of diagnosis for certain conditions, making \"Diagnosis_Category\" a valuable proxy for understanding regional differences in healthcare needs and priorities.\n", "\n", "5. **Epidemiological Insights:** Using \"Diagnosis_Category\" for imputation helps preserve the epidemiological integrity of the dataset by maintaining a link between the type of care required (as indicated by the diagnosis) and the region. This approach respects the notion that healthcare needs and the prevalence of certain conditions are not randomly distributed but are influenced by a complex interplay of regional factors.\n", "\n", "By imputing \"Region\" based on \"Diagnosis_Category,\" you're leveraging the underlying assumption that certain diagnoses are more common in certain areas, which can be due to various environmental, social, and healthcare system factors unique to each region. This method helps in maintaining the coherence of the dataset, ensuring that imputed values for \"Region\" are not just statistically plausible but also make sense in the real-world context of healthcare geography and epidemiology." ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/fs/5ct03tj50v1_hbz7dk31c6mw0000gn/T/ipykernel_2098/1359758504.py:7: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " filtered_termination_df['Region'].replace('Unknown', np.nan, inplace=True)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Region\n", "03: South 299311\n", "01: Mid-West 129914\n", "04: West 110127\n", "02: Northeast 80413\n", "05: Other 47\n", "Name: count, dtype: int64\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/fs/5ct03tj50v1_hbz7dk31c6mw0000gn/T/ipykernel_2098/1359758504.py:19: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " filtered_termination_df['Region'].fillna(overall_mode, inplace=True)\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "# Assuming filtered_termination_df is your DataFrame\n", "\n", "# Step 1: Replace 'Unknown' with np.nan for easier handling with pandas methods\n", "filtered_termination_df['Region'].replace('Unknown', np.nan, inplace=True)\n", "\n", "# Step 2 & 3: Impute 'Unknown' (now np.nan) in 'Region' based on the mode of each 'Diagnosis_Category' group\n", "for diagnosis_category in filtered_termination_df['Diagnosis_Category'].unique():\n", " # Compute the mode of 'Region' for the current 'Diagnosis_Category'\n", " mode_region = filtered_termination_df.loc[filtered_termination_df['Diagnosis_Category'] == diagnosis_category, 'Region'].mode()\n", " if not mode_region.empty:\n", " # Impute 'Unknown' values with the mode for this subset\n", " filtered_termination_df.loc[(filtered_termination_df['Diagnosis_Category'] == diagnosis_category) & (filtered_termination_df['Region'].isna()), 'Region'] = mode_region[0]\n", "\n", "# If there are still any 'Unknown' (np.nan) values left, fill them with the overall mode\n", "overall_mode = filtered_termination_df['Region'].mode()[0]\n", "filtered_termination_df['Region'].fillna(overall_mode, inplace=True)\n", "\n", "# Check the imputation result\n", "print(filtered_termination_df['Region'].value_counts())\n" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GenderIncurred_Age_BucketIncurred_Year_BucketClaim_TypeRegionDiagnosis_CategoryClaim_DurationExposureDeathsRecoveryTerminationsBenefit_ExpiryOthers_Terminations
3675Female65-692009-2010HCC04: West04: Circulatory17200000
3676Female90+2005-2006NH03: SouthUnknown2010833600
3677Female90+< 2001NH04: West01: Alzheimer's and Dementia93300000
3678Female80-842013-2014HCC03: South01: Alzheimer's and Dementia2314210100
3679Female0-492003-2004NH03: South06: Nervous System36300000
..........................................
627185Male85-892007-2008NH03: South05: Injury33200000
627186Male75-792007-2008HCC03: SouthUnknown316230300
627187Female80-842013-2014HCC01: Mid-West07: Stroke23600000
627188Male70-742001-2002HCC02: NortheastUnknown30700000
627189Female85-892003-2004ALF03: SouthUnknown329510100
\n", "

619812 rows × 13 columns

\n", "
" ], "text/plain": [ " Gender Incurred_Age_Bucket Incurred_Year_Bucket Claim_Type \\\n", "3675 Female 65-69 2009-2010 HCC \n", "3676 Female 90+ 2005-2006 NH \n", "3677 Female 90+ < 2001 NH \n", "3678 Female 80-84 2013-2014 HCC \n", "3679 Female 0-49 2003-2004 NH \n", "... ... ... ... ... \n", "627185 Male 85-89 2007-2008 NH \n", "627186 Male 75-79 2007-2008 HCC \n", "627187 Female 80-84 2013-2014 HCC \n", "627188 Male 70-74 2001-2002 HCC \n", "627189 Female 85-89 2003-2004 ALF \n", "\n", " Region Diagnosis_Category Claim_Duration Exposure \\\n", "3675 04: West 04: Circulatory 17 2 \n", "3676 03: South Unknown 20 108 \n", "3677 04: West 01: Alzheimer's and Dementia 93 3 \n", "3678 03: South 01: Alzheimer's and Dementia 23 142 \n", "3679 03: South 06: Nervous System 36 3 \n", "... ... ... ... ... \n", "627185 03: South 05: Injury 33 2 \n", "627186 03: South Unknown 31 62 \n", "627187 01: Mid-West 07: Stroke 23 6 \n", "627188 02: Northeast Unknown 30 7 \n", "627189 03: South Unknown 32 95 \n", "\n", " Deaths Recovery Terminations Benefit_Expiry Others_Terminations \n", "3675 0 0 0 0 0 \n", "3676 3 3 6 0 0 \n", "3677 0 0 0 0 0 \n", "3678 1 0 1 0 0 \n", "3679 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "627185 0 0 0 0 0 \n", "627186 3 0 3 0 0 \n", "627187 0 0 0 0 0 \n", "627188 0 0 0 0 0 \n", "627189 1 0 1 0 0 \n", "\n", "[619812 rows x 13 columns]" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filtered_termination_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IV. Split both \"Incidence\" and \"Termination\" datasets into train/test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (a) Split filtered_incidence_df" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "\n", "# Now we will split the 'filtered_incidence_df' DataFrame into training and validation sets\n", "train_filtered_incidence_df, validation_filtered_incidence_df = train_test_split(filtered_incidence_df, test_size=0.2, random_state=42)\n", "\n", "# train_df is now your training set, and validation_df is your validation set." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Group_IndicatorGenderIssue_Age_BucketIncurred_Age_BucketIssue_Year_BucketPolicy_YearMarital_StatusPremium_ClassUnderwriting_TypeCoverage_Type_Bucket...ALF_EP_BucketHHC_EP_BucketRegionActive_ExposureTotal_ExposureClaim_CountCount_NHCount_ALFCount_HHCCount_Unk
1702737IndividualFemale50-5460-642003-20057-9 yearsSinglePreferredOtherComprehensive...90004: West19.58333219.58333200000
1380059IndividualFemale70-7480-842000-200213-15 yearsMarriedStandardUnknownComprehensive...0003: South84.333336108.91666080080
837211IndividualFemale60-6460-642000-20024-6 yearsUnknownStandardUnknownComprehensive...90003: South3.0000003.00000000000
1315575IndividualFemale80-8480-841994-19964-6 yearsMarriedStandardFull underwritingOther...0003: South3.7500003.75000000000
907204IndividualFemale55-5955-591994-19964-6 yearsMarriedStandardFull underwritingOther...30003: South2.5833332.58333300000
..................................................................
259245GroupFemale65-6965-692003-20054-6 yearsMarriedStandardOtherComprehensive...0002: Northeast4.0000004.00000000000
1414796IndividualFemale55-5965-691997-199910-12 yearsSingleStandardUnknownComprehensive...0004: West62.75000062.83333210010
131965IndividualMale70-7480-842009-20117-9 yearsUnknownStandardFull underwritingComprehensive...0002: Northeast2.4166662.41666600000
671338IndividualFemale70-7485-891997-199915+ yearsSinglePreferredUnknownComprehensive...0003: South14.24999919.91666430120
121987IndividualMale70-7485-892000-200213-15 yearsMarriedPreferredUnknownOther...0004: West3.0833333.08333300000
\n", "

1671005 rows × 31 columns

\n", "
" ], "text/plain": [ " Group_Indicator Gender Issue_Age_Bucket Incurred_Age_Bucket \\\n", "1702737 Individual Female 50-54 60-64 \n", "1380059 Individual Female 70-74 80-84 \n", "837211 Individual Female 60-64 60-64 \n", "1315575 Individual Female 80-84 80-84 \n", "907204 Individual Female 55-59 55-59 \n", "... ... ... ... ... \n", "259245 Group Female 65-69 65-69 \n", "1414796 Individual Female 55-59 65-69 \n", "131965 Individual Male 70-74 80-84 \n", "671338 Individual Female 70-74 85-89 \n", "121987 Individual Male 70-74 85-89 \n", "\n", " Issue_Year_Bucket Policy_Year Marital_Status Premium_Class \\\n", "1702737 2003-2005 7-9 years Single Preferred \n", "1380059 2000-2002 13-15 years Married Standard \n", "837211 2000-2002 4-6 years Unknown Standard \n", "1315575 1994-1996 4-6 years Married Standard \n", "907204 1994-1996 4-6 years Married Standard \n", "... ... ... ... ... \n", "259245 2003-2005 4-6 years Married Standard \n", "1414796 1997-1999 10-12 years Single Standard \n", "131965 2009-2011 7-9 years Unknown Standard \n", "671338 1997-1999 15+ years Single Preferred \n", "121987 2000-2002 13-15 years Married Preferred \n", "\n", " Underwriting_Type Coverage_Type_Bucket ... ALF_EP_Bucket \\\n", "1702737 Other Comprehensive ... 90 \n", "1380059 Unknown Comprehensive ... 0 \n", "837211 Unknown Comprehensive ... 90 \n", "1315575 Full underwriting Other ... 0 \n", "907204 Full underwriting Other ... 30 \n", "... ... ... ... ... \n", "259245 Other Comprehensive ... 0 \n", "1414796 Unknown Comprehensive ... 0 \n", "131965 Full underwriting Comprehensive ... 0 \n", "671338 Unknown Comprehensive ... 0 \n", "121987 Unknown Other ... 0 \n", "\n", " HHC_EP_Bucket Region Active_Exposure Total_Exposure \\\n", "1702737 0 04: West 19.583332 19.583332 \n", "1380059 0 03: South 84.333336 108.916660 \n", "837211 0 03: South 3.000000 3.000000 \n", "1315575 0 03: South 3.750000 3.750000 \n", "907204 0 03: South 2.583333 2.583333 \n", "... ... ... ... ... \n", "259245 0 02: Northeast 4.000000 4.000000 \n", "1414796 0 04: West 62.750000 62.833332 \n", "131965 0 02: Northeast 2.416666 2.416666 \n", "671338 0 03: South 14.249999 19.916664 \n", "121987 0 04: West 3.083333 3.083333 \n", "\n", " Claim_Count Count_NH Count_ALF Count_HHC Count_Unk \n", "1702737 0 0 0 0 0 \n", "1380059 8 0 0 8 0 \n", "837211 0 0 0 0 0 \n", "1315575 0 0 0 0 0 \n", "907204 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "259245 0 0 0 0 0 \n", "1414796 1 0 0 1 0 \n", "131965 0 0 0 0 0 \n", "671338 3 0 1 2 0 \n", "121987 0 0 0 0 0 \n", "\n", "[1671005 rows x 31 columns]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_filtered_incidence_df" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Group_IndicatorGenderIssue_Age_BucketIncurred_Age_BucketIssue_Year_BucketPolicy_YearMarital_StatusPremium_ClassUnderwriting_TypeCoverage_Type_Bucket...ALF_EP_BucketHHC_EP_BucketRegionActive_ExposureTotal_ExposureClaim_CountCount_NHCount_ALFCount_HHCCount_Unk
1756216GroupFemale50-5455-592009-20117-9 yearsUnknownStandardUnknownComprehensive...0003: South32.74999632.74999600000
2001415IndividualFemale60-6475-791994-199615+ yearsMarriedStandardFull underwritingComprehensive...0002: Northeast2.0000002.00000000000
454164GroupMale0-490-491997-199910-12 yearsMarriedStandardOtherComprehensive...0002: Northeast7.0000007.00000000000
1073250IndividualMale65-6980-841997-199913-15 yearsMarriedStandardOtherComprehensive...0001: Mid-West63.58333265.58332831110
811617IndividualMale65-6965-691994-19964-6 yearsMarriedStandardUnknownComprehensive...0002: Northeast23.41666223.41666200000
..................................................................
736427IndividualFemale60-6475-792000-200215+ yearsMarriedPreferredOtherComprehensive...903001: Mid-West1.5000001.50000000000
506473IndividualFemale65-6965-692000-20024-6 yearsUnknownSubstandardFull underwritingOther...0001: Mid-West3.0000003.00000000000
1894427IndividualFemale0-4960-642000-200215+ yearsSinglePreferredUnknownComprehensive...0003: South2.0833332.08333300000
244198GroupFemale55-5960-642006-20081-3 yearsUnknownStandardFull underwritingComprehensive...0003: South2.0000002.00000000000
2069551IndividualFemale60-6465-691997-19997-9 yearsMarriedPreferredOtherComprehensive...202004: West9.0000009.00000000000
\n", "

417752 rows × 31 columns

\n", "
" ], "text/plain": [ " Group_Indicator Gender Issue_Age_Bucket Incurred_Age_Bucket \\\n", "1756216 Group Female 50-54 55-59 \n", "2001415 Individual Female 60-64 75-79 \n", "454164 Group Male 0-49 0-49 \n", "1073250 Individual Male 65-69 80-84 \n", "811617 Individual Male 65-69 65-69 \n", "... ... ... ... ... \n", "736427 Individual Female 60-64 75-79 \n", "506473 Individual Female 65-69 65-69 \n", "1894427 Individual Female 0-49 60-64 \n", "244198 Group Female 55-59 60-64 \n", "2069551 Individual Female 60-64 65-69 \n", "\n", " Issue_Year_Bucket Policy_Year Marital_Status Premium_Class \\\n", "1756216 2009-2011 7-9 years Unknown Standard \n", "2001415 1994-1996 15+ years Married Standard \n", "454164 1997-1999 10-12 years Married Standard \n", "1073250 1997-1999 13-15 years Married Standard \n", "811617 1994-1996 4-6 years Married Standard \n", "... ... ... ... ... \n", "736427 2000-2002 15+ years Married Preferred \n", "506473 2000-2002 4-6 years Unknown Substandard \n", "1894427 2000-2002 15+ years Single Preferred \n", "244198 2006-2008 1-3 years Unknown Standard \n", "2069551 1997-1999 7-9 years Married Preferred \n", "\n", " Underwriting_Type Coverage_Type_Bucket ... ALF_EP_Bucket \\\n", "1756216 Unknown Comprehensive ... 0 \n", "2001415 Full underwriting Comprehensive ... 0 \n", "454164 Other Comprehensive ... 0 \n", "1073250 Other Comprehensive ... 0 \n", "811617 Unknown Comprehensive ... 0 \n", "... ... ... ... ... \n", "736427 Other Comprehensive ... 90 \n", "506473 Full underwriting Other ... 0 \n", "1894427 Unknown Comprehensive ... 0 \n", "244198 Full underwriting Comprehensive ... 0 \n", "2069551 Other Comprehensive ... 20 \n", "\n", " HHC_EP_Bucket Region Active_Exposure Total_Exposure \\\n", "1756216 0 03: South 32.749996 32.749996 \n", "2001415 0 02: Northeast 2.000000 2.000000 \n", "454164 0 02: Northeast 7.000000 7.000000 \n", "1073250 0 01: Mid-West 63.583332 65.583328 \n", "811617 0 02: Northeast 23.416662 23.416662 \n", "... ... ... ... ... \n", "736427 30 01: Mid-West 1.500000 1.500000 \n", "506473 0 01: Mid-West 3.000000 3.000000 \n", "1894427 0 03: South 2.083333 2.083333 \n", "244198 0 03: South 2.000000 2.000000 \n", "2069551 20 04: West 9.000000 9.000000 \n", "\n", " Claim_Count Count_NH Count_ALF Count_HHC Count_Unk \n", "1756216 0 0 0 0 0 \n", "2001415 0 0 0 0 0 \n", "454164 0 0 0 0 0 \n", "1073250 3 1 1 1 0 \n", "811617 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "736427 0 0 0 0 0 \n", "506473 0 0 0 0 0 \n", "1894427 0 0 0 0 0 \n", "244198 0 0 0 0 0 \n", "2069551 0 0 0 0 0 \n", "\n", "[417752 rows x 31 columns]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "validation_filtered_incidence_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (b) Split filtered_termination_df" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "# Now we will split the 'filtered_termination_df' DataFrame into training and validation sets\n", "train_filtered_termination_df, validation_filtered_termination_df = train_test_split(filtered_termination_df, test_size=0.2, random_state=42)\n" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GenderIncurred_Age_BucketIncurred_Year_BucketClaim_TypeRegionDiagnosis_CategoryClaim_DurationExposureDeathsRecoveryTerminationsBenefit_ExpiryOthers_Terminations
57033Male75-792001-2002HCC03: South08: Other56300000
365822Male90+2009-2010HCC03: South01: Alzheimer's and Dementia361000000
575969Female70-74< 2001HCC04: West04: Circulatory123200000
217054Female85-892007-2008NH02: NortheastUnknown651601100
47634Female90+2013-2014NH04: West08: Other40600000
..........................................
117646Female70-74< 2001HCC03: South05: Injury1802301
266556Male60-642007-2008Other01: Mid-WestUnknown45200000
373216Female80-842001-2002HCC03: South08: Other138400000
139310Female50-542005-2006HCC03: South06: Nervous System3700000
129336Female90+2009-2010HCC01: Mid-West08: Other281400000
\n", "

495849 rows × 13 columns

\n", "
" ], "text/plain": [ " Gender Incurred_Age_Bucket Incurred_Year_Bucket Claim_Type \\\n", "57033 Male 75-79 2001-2002 HCC \n", "365822 Male 90+ 2009-2010 HCC \n", "575969 Female 70-74 < 2001 HCC \n", "217054 Female 85-89 2007-2008 NH \n", "47634 Female 90+ 2013-2014 NH \n", "... ... ... ... ... \n", "117646 Female 70-74 < 2001 HCC \n", "266556 Male 60-64 2007-2008 Other \n", "373216 Female 80-84 2001-2002 HCC \n", "139310 Female 50-54 2005-2006 HCC \n", "129336 Female 90+ 2009-2010 HCC \n", "\n", " Region Diagnosis_Category Claim_Duration Exposure \\\n", "57033 03: South 08: Other 56 3 \n", "365822 03: South 01: Alzheimer's and Dementia 36 10 \n", "575969 04: West 04: Circulatory 123 2 \n", "217054 02: Northeast Unknown 65 16 \n", "47634 04: West 08: Other 40 6 \n", "... ... ... ... ... \n", "117646 03: South 05: Injury 1 8 \n", "266556 01: Mid-West Unknown 45 2 \n", "373216 03: South 08: Other 138 4 \n", "139310 03: South 06: Nervous System 3 7 \n", "129336 01: Mid-West 08: Other 28 14 \n", "\n", " Deaths Recovery Terminations Benefit_Expiry Others_Terminations \n", "57033 0 0 0 0 0 \n", "365822 0 0 0 0 0 \n", "575969 0 0 0 0 0 \n", "217054 0 1 1 0 0 \n", "47634 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "117646 0 2 3 0 1 \n", "266556 0 0 0 0 0 \n", "373216 0 0 0 0 0 \n", "139310 0 0 0 0 0 \n", "129336 0 0 0 0 0 \n", "\n", "[495849 rows x 13 columns]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_filtered_termination_df" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GenderIncurred_Age_BucketIncurred_Year_BucketClaim_TypeRegionDiagnosis_CategoryClaim_DurationExposureDeathsRecoveryTerminationsBenefit_ExpiryOthers_Terminations
368230Female65-692009-2010HCC03: South06: Nervous System57500000
9464Female75-79< 2001NH03: South08: Other119300000
278806Female90+2005-2006NH04: WestUnknown97500000
280654Female85-892013-2014NH03: South08: Other619230310
592960Female65-692005-2006ALF02: NortheastUnknown8600000
..........................................
595747Female90+2001-2002HCC01: Mid-West01: Alzheimer's and Dementia80201100
120359Female85-892005-2006ALF01: Mid-West05: Injury2400000
124058Male0-492001-2002HCC03: SouthUnknown84200000
409170Male80-842007-2008HCC02: Northeast06: Nervous System10400000
391058Male80-842003-2004Other01: Mid-WestUnknown34600000
\n", "

123963 rows × 13 columns

\n", "
" ], "text/plain": [ " Gender Incurred_Age_Bucket Incurred_Year_Bucket Claim_Type \\\n", "368230 Female 65-69 2009-2010 HCC \n", "9464 Female 75-79 < 2001 NH \n", "278806 Female 90+ 2005-2006 NH \n", "280654 Female 85-89 2013-2014 NH \n", "592960 Female 65-69 2005-2006 ALF \n", "... ... ... ... ... \n", "595747 Female 90+ 2001-2002 HCC \n", "120359 Female 85-89 2005-2006 ALF \n", "124058 Male 0-49 2001-2002 HCC \n", "409170 Male 80-84 2007-2008 HCC \n", "391058 Male 80-84 2003-2004 Other \n", "\n", " Region Diagnosis_Category Claim_Duration Exposure \\\n", "368230 03: South 06: Nervous System 57 5 \n", "9464 03: South 08: Other 119 3 \n", "278806 04: West Unknown 97 5 \n", "280654 03: South 08: Other 6 192 \n", "592960 02: Northeast Unknown 8 6 \n", "... ... ... ... ... \n", "595747 01: Mid-West 01: Alzheimer's and Dementia 80 2 \n", "120359 01: Mid-West 05: Injury 2 4 \n", "124058 03: South Unknown 84 2 \n", "409170 02: Northeast 06: Nervous System 10 4 \n", "391058 01: Mid-West Unknown 34 6 \n", "\n", " Deaths Recovery Terminations Benefit_Expiry Others_Terminations \n", "368230 0 0 0 0 0 \n", "9464 0 0 0 0 0 \n", "278806 0 0 0 0 0 \n", "280654 3 0 3 1 0 \n", "592960 0 0 0 0 0 \n", "... ... ... ... ... ... \n", "595747 0 1 1 0 0 \n", "120359 0 0 0 0 0 \n", "124058 0 0 0 0 0 \n", "409170 0 0 0 0 0 \n", "391058 0 0 0 0 0 \n", "\n", "[123963 rows x 13 columns]" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "validation_filtered_termination_df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## V. Data export as CSV" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "train_filtered_incidence_df.to_csv('train_filtered_incidence_df.csv', index=False)\n", "validation_filtered_incidence_df.to_csv('validation_filtered_incidence_df.csv', index=False)\n", "train_filtered_termination_df.to_csv('train_filtered_termination_df.csv', index=False)\n", "validation_filtered_termination_df.to_csv('validation_filtered_termination_df.csv', index=False)\n" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import zipfile\n", "import os\n", "\n", "def df_to_csv_zip(dataframe, csv_filename, zip_filename):\n", " # Save the DataFrame to a CSV\n", " csv_path = csv_filename\n", " dataframe.to_csv(csv_path, index=False)\n", " \n", " # Compress the CSV into a ZIP file\n", " with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:\n", " zipf.write(csv_path, arcname=csv_filename)\n", " \n", " # Remove the CSV file to clean up space\n", " os.remove(csv_path)\n", "\n", "# For each DataFrame, call this function\n", "df_to_csv_zip(train_filtered_incidence_df, 'train_filtered_incidence_df.csv', 'train_filtered_incidence_df.zip')\n", "df_to_csv_zip(validation_filtered_incidence_df, 'validation_filtered_incidence_df.csv', 'validation_filtered_incidence_df.zip')\n", "df_to_csv_zip(train_filtered_termination_df, 'train_filtered_termination_df.csv', 'train_filtered_termination_df.zip')\n", "df_to_csv_zip(validation_filtered_termination_df, 'validation_filtered_termination_df.csv', 'validation_filtered_termination_df.zip')\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }