{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":104803,"databundleVersionId":12615271,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Data Science 5 SBU Competition Project","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport time\nfrom sklearn.tree import DecisionTreeRegressor\n\ninputFiles = []\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        inputFiles.append(os.path.join(dirname, filename))\n        \n        if filename == \"train.csv\":\n            trainingData = pd.read_csv(os.path.join(dirname, filename))\n            print(\"Training data found\")\n        elif filename == \"x_test.csv\":\n            testData = pd.read_csv(os.path.join(dirname, filename))\n            print(\"Testing data found\")\n\nprint()\ncolumns = trainingData.columns\n\ninsuranceDataFeatures = ['Years Lived','Yearly Earnings', 'Relationship Status', 'Dependent Count', 'Academic Standing', 'Job Title', 'Wellness Index', 'Coverage Class', 'Prior Claims', 'Automobile Age', 'Financial Rating', 'Coverage Period', 'Tobacco Use', 'Physical Activity', 'Asset Category'] # Full feature set of useful training data\n# insuranceDataFeaturesTesting = ['Wellness Index', 'Financial Rating', 'Tobacco Use', 'Physical Activity'] # Temporary feature set for testing\nX = trainingData[insuranceDataFeatures]\nX_test = testData[insuranceDataFeatures]\n\n\ndef cleanTrainingAndTestingData():\n    global trainingData, X, testData, X_test\n\n    def formatSmokerData(x): # Formats smoker\n        if x == 'Smoker':\n            return 1\n        elif x == 'Non-Smoker':\n            return 0\n        else:\n            print(\"Missing or incorrect data\")\n            return np.nan\n\n    def formatRelationshipData(x): # Formats relationship status\n        if x == 'Spouse':\n            return 1\n        elif x == 'Unmarried':\n            return 0\n        elif x == 'Separated':\n            return 2\n        else:\n            print(\"Missing or incorrect data\")\n            print(x)\n            return np.nan\n        \n    def formatAcademicStandingData(x): # Formats academic standing\n        if x == 'Secondary':\n            return 0\n        elif x == 'Undergraduate':\n            return 1\n        elif x == 'Graduate':\n            return 2\n        elif x == 'Doctorate':\n            return 3\n        else:\n            print(\"Missing or incorrect data\")\n            return np.nan\n\n    def formatJobTitleData(x): # Formats job title\n        if x == 'Jobless':\n            return 0\n        elif x == 'Working':\n            return 1\n        elif x == 'Freelancer':\n            return 2\n        else:\n            print(\"Missing or incorrect data\")\n            print(x)\n            return np.nan\n\n    def formatCoverageClassData(x): # Formats coverage class\n        if x == 'Standard':\n            return 0\n        elif x == 'Full':\n            return 1\n        elif x == 'Superior':\n            return 2\n        else:\n            print(\"Missing or incorrect data\")\n            return np.nan\n\n    def formatPhysicalActivityData(x): # Formats physical activity\n        if x == 'Seldom':\n            return 0\n        elif x == 'Once a Month':\n            return 1\n        elif x == 'Once a Week':\n            return 2\n        elif x == 'Everyday':\n            return 3\n        else:\n            print(\"Missing or incorrect data\")\n            return np.nan\n\n    def formatAssetCategoryData(x): # Formats asset category\n        if x == 'Unit':\n            return 0\n        elif x == 'Flat':\n            return 1\n        elif x == 'Residence':\n            return 2\n        else:\n            print(\"Missing or incorrect data\")\n            return np.nan\n\n    def yearlyEarningsCorrection(row):\n        if row.name >= 200: # Temporary as looping through the entire dataset takes too long; the higher the number, the more accurate the precitions will be\n            return row['Yearly Earnings']\n        if pd.isna(row['Yearly Earnings']):\n            similarRows = trainingData[\n                (trainingData['Years Lived'] == row['Years Lived']) &\n                (trainingData['Academic Standing'] == row['Academic Standing']) &\n                (trainingData['Job Title'] == row['Job Title']) &\n                (~trainingData['Yearly Earnings'].isna())\n            ]\n\n            if not similarRows.empty:\n                return similarRows['Yearly Earnings'].median()\n            else:\n                return np.nan  # Mark for dropping later\n        return row['Yearly Earnings']\n\n    def financialRatingCorrection(row):\n        if row.name >= 200: # Temporary as looping through the entire dataset takes too long; the higher the number, the more accurate the precitions will be\n            return row['Financial Rating']\n        if pd.isna(row['Financial Rating']):\n            similarRows = trainingData[\n                (trainingData['Yearly Earnings'] == row['Yearly Earnings']) &\n                (trainingData['Academic Standing'] == row['Academic Standing']) &\n                (trainingData['Job Title'] == row['Job Title']) &\n                (~trainingData['Financial Rating'].isna())\n            ]\n\n            if not similarRows.empty:\n                return similarRows['Financial Rating'].median()\n            else:\n                return np.nan  # Mark for dropping later\n        return row['Financial Rating']\n    \n    trainingData = trainingData.dropna(subset=['Relationship Status'])\n    trainingData = trainingData.dropna(subset=['Client Review'])\n    trainingData = trainingData.dropna(subset=['Dependent Count'])\n    trainingData = trainingData.dropna(subset=['Prior Claims'])\n    trainingData = trainingData.dropna(subset=['Job Title'])\n\n    # Temporary fix to remove rows with NaN in testData\n\n    testData = testData.dropna(subset=['Relationship Status'])\n    testData = testData.dropna(subset=['Client Review'])\n    testData = testData.dropna(subset=['Dependent Count'])\n    testData = testData.dropna(subset=['Prior Claims'])\n    testData = testData.dropna(subset=['Job Title'])\n\n    X = X.loc[trainingData.index]\n    X_test = X_test.loc[testData.index]\n\n    X.loc[:, 'Tobacco Use'] = X['Tobacco Use'].apply(formatSmokerData) # type: ignore\n    print(\"Formatted tobacco use training data\")\n    X.loc[:, 'Relationship Status'] = X['Relationship Status'].apply(formatRelationshipData) # type: ignore\n    print(\"Formatted relationship status training data\")\n    X.loc[:, 'Academic Standing'] = X['Academic Standing'].apply(formatAcademicStandingData) # type: ignore\n    print(\"Formatted academic standing training data\")\n    X.loc[:, 'Job Title'] = X['Job Title'].apply(formatJobTitleData) # type: ignore\n    print(\"Formatted job title training data\")\n    X.loc[:, 'Coverage Class'] = X['Coverage Class'].apply(formatCoverageClassData) # type: ignore\n    print(\"Formatted coverage class training data\")\n    X.loc[:, 'Physical Activity'] = X['Physical Activity'].apply(formatPhysicalActivityData) # type: ignore\n    print(\"Formatted physical activity training data\")\n    X.loc[:, 'Asset Category'] = X['Asset Category'].apply(formatAssetCategoryData) # type: ignore\n    print(\"Formatted asset category training data\")\n\n    X_test.loc[:, 'Tobacco Use'] = X_test['Tobacco Use'].apply(formatSmokerData) # type: ignore\n    print(\"Formatted tobacco use data for test data\")\n    X_test.loc[:, 'Relationship Status'] = X_test['Relationship Status'].apply(formatRelationshipData) # type: ignore\n    print(\"Formatted relationship status data for test data\")\n    time.sleep(5) # TESTING\n    X_test.loc[:, 'Academic Standing'] = X_test['Academic Standing'].apply(formatAcademicStandingData) # type: ignore\n    print(\"Formatted academic standing data for test data\")\n    X_test.loc[:, 'Job Title'] = X_test['Job Title'].apply(formatJobTitleData) # type: ignore\n    print(\"Formatted job title data for test data\")\n    X_test.loc[:, 'Coverage Class'] = X_test['Coverage Class'].apply(formatCoverageClassData) # type: ignore\n    print(\"Formatted coverage class data for test data\")\n    X_test.loc[:, 'Physical Activity'] = X_test['Physical Activity'].apply(formatPhysicalActivityData) # type: ignore\n    print(\"Formatted physical activity data for test data\")\n    X_test.loc[:, 'Asset Category'] = X_test['Asset Category'].apply(formatAssetCategoryData) # type: ignore\n    print(\"Formatted asset category data for test data\")\n\n\n    X.loc[:, 'Yearly Earnings'] = trainingData.apply(yearlyEarningsCorrection, axis=1)\n    print(\"Estimated yearly earnings for missing values in training data\")\n    X.loc[:, 'Financial Rating'] = trainingData.apply(financialRatingCorrection, axis=1)\n    print(\"Estimated financial ratings for missing values in training data\")\n\n    X_test.loc[:, 'Yearly Earnings'] = testData.apply(yearlyEarningsCorrection, axis=1)\n    print(\"Estimated yearly earnings for missing values in test data\")\n    X_test.loc[:, 'Financial Rating'] = testData.apply(financialRatingCorrection, axis=1) \n    print(\"Estimated financial ratings for missing values in test data\")\n\n    # Drops rows where 'Yearly Earnings' or 'Financial Rating' is NaN\n    nanIndices = X[X[['Yearly Earnings', 'Financial Rating']].isna().any(axis=1)].index\n    X = X.drop(index=nanIndices)\n    trainingData = trainingData.loc[X.index]\n\n    X = X.reset_index(drop=True)\n    trainingData = trainingData.reset_index(drop=True)\n\n\ncleanTrainingAndTestingData()\ny = trainingData[\"Policy Cost\"]\n\ninsuranceModel = DecisionTreeRegressor(random_state=1)\ninsuranceModel.fit(X, y)\n\nprint(\"Training complete!\")\nprint()\nprint()\n\n\n# Testing the model with test data\n\npredictions = insuranceModel.predict(X_test)\npredictionNumber = 10\n\nprint(\"---PREDICTIONS---\")\nprint()\n\nprint(\"Predictions for the first\", str(predictionNumber), \"valid entries:\")\nprint(X_test.head(predictionNumber))\nprint()\nprint(\"Predictions:\")\nprint(predictions[:predictionNumber])\n\nprint()\nprint(\"Predictions complete\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null}]}