{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84896,"databundleVersionId":10305135,"sourceType":"competition"},{"sourceId":10115763,"sourceType":"datasetVersion","datasetId":6241269},{"sourceId":10135814,"sourceType":"datasetVersion","datasetId":6255491}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler, QuantileTransformer, MinMaxScaler\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.pipeline import Pipeline, make_pipeline\nimport catboost as ctb\nfrom sklearn.model_selection import train_test_split\nimport math\nfrom sklearn.compose import ColumnTransformer\nimport sklearn\nfrom sklearn.metrics import mean_squared_log_error\n\nimport time\nimport pickle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:48.539134Z","iopub.execute_input":"2024-12-08T07:53:48.540294Z","iopub.status.idle":"2024-12-08T07:53:49.893164Z","shell.execute_reply.started":"2024-12-08T07:53:48.540213Z","shell.execute_reply":"2024-12-08T07:53:49.89186Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Upvotes appreciated\n\n### Toward the bottom is a dataframe called transformations_log.\n### It shows scores for various models and combinations of feature engineering.\n### Surprisingly, adding more complex features very rarely helped at all.\n\n## Look for \"Transformations Log\" in the Table of Contents","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')\nsample = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:49.894889Z","iopub.execute_input":"2024-12-08T07:53:49.895429Z","iopub.status.idle":"2024-12-08T07:53:59.063716Z","shell.execute_reply.started":"2024-12-08T07:53:49.895374Z","shell.execute_reply":"2024-12-08T07:53:59.062351Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"print('data shape: {}'.format(data.shape))\nprint('test shape: {}'.format(test.shape))\nprint('sample shape: {}'.format(sample.shape))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.065088Z","iopub.execute_input":"2024-12-08T07:53:59.065424Z","iopub.status.idle":"2024-12-08T07:53:59.071832Z","shell.execute_reply.started":"2024-12-08T07:53:59.065391Z","shell.execute_reply":"2024-12-08T07:53:59.070682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.074719Z","iopub.execute_input":"2024-12-08T07:53:59.075872Z","iopub.status.idle":"2024-12-08T07:53:59.110412Z","shell.execute_reply.started":"2024-12-08T07:53:59.075818Z","shell.execute_reply":"2024-12-08T07:53:59.109242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.11162Z","iopub.execute_input":"2024-12-08T07:53:59.111962Z","iopub.status.idle":"2024-12-08T07:53:59.120787Z","shell.execute_reply.started":"2024-12-08T07:53:59.11192Z","shell.execute_reply":"2024-12-08T07:53:59.119585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[['Customer Feedback','Premium Amount']].groupby('Customer Feedback').agg('mean')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.122203Z","iopub.execute_input":"2024-12-08T07:53:59.122608Z","iopub.status.idle":"2024-12-08T07:53:59.270594Z","shell.execute_reply.started":"2024-12-08T07:53:59.122572Z","shell.execute_reply":"2024-12-08T07:53:59.269214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.dtypes.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.272087Z","iopub.execute_input":"2024-12-08T07:53:59.272564Z","iopub.status.idle":"2024-12-08T07:53:59.281594Z","shell.execute_reply.started":"2024-12-08T07:53:59.272513Z","shell.execute_reply":"2024-12-08T07:53:59.280406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.select_dtypes('float64').head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.28344Z","iopub.execute_input":"2024-12-08T07:53:59.283813Z","iopub.status.idle":"2024-12-08T07:53:59.33917Z","shell.execute_reply.started":"2024-12-08T07:53:59.283777Z","shell.execute_reply":"2024-12-08T07:53:59.337934Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pandas.plotting import scatter_matrix","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.340522Z","iopub.execute_input":"2024-12-08T07:53:59.340824Z","iopub.status.idle":"2024-12-08T07:53:59.345671Z","shell.execute_reply.started":"2024-12-08T07:53:59.340796Z","shell.execute_reply":"2024-12-08T07:53:59.344532Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# df = pd.DataFrame(np.random.randn(1000, 4), columns = ['a', 'b', 'c', 'd'])\nshow_without_printout_by_creating_variable = scatter_matrix(data.select_dtypes('float64').sample(100), alpha = 1, figsize = (20, 20), diagonal = 'kde')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:53:59.349307Z","iopub.execute_input":"2024-12-08T07:53:59.349679Z","iopub.status.idle":"2024-12-08T07:54:04.667293Z","shell.execute_reply.started":"2024-12-08T07:53:59.349638Z","shell.execute_reply":"2024-12-08T07:54:04.665969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 4)) \nsns.heatmap(data.select_dtypes('float64').corr().replace(1,0).apply(lambda x: round(x,3)) ,annot = True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:04.669198Z","iopub.execute_input":"2024-12-08T07:54:04.669633Z","iopub.status.idle":"2024-12-08T07:54:05.627065Z","shell.execute_reply.started":"2024-12-08T07:54:04.669587Z","shell.execute_reply":"2024-12-08T07:54:05.625878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_data = data.sample(5000, random_state = 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:05.62845Z","iopub.execute_input":"2024-12-08T07:54:05.628799Z","iopub.status.idle":"2024-12-08T07:54:05.690394Z","shell.execute_reply.started":"2024-12-08T07:54:05.628762Z","shell.execute_reply":"2024-12-08T07:54:05.689088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_data.dropna().shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:05.692237Z","iopub.execute_input":"2024-12-08T07:54:05.692633Z","iopub.status.idle":"2024-12-08T07:54:05.706072Z","shell.execute_reply.started":"2024-12-08T07:54:05.692597Z","shell.execute_reply":"2024-12-08T07:54:05.704899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_data.select_dtypes('float64').isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:05.707737Z","iopub.execute_input":"2024-12-08T07:54:05.708187Z","iopub.status.idle":"2024-12-08T07:54:05.719195Z","shell.execute_reply.started":"2024-12-08T07:54:05.708122Z","shell.execute_reply":"2024-12-08T07:54:05.718005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"float_cols = sample_data.select_dtypes('float64').columns.tolist()\n# float_cols.remove('Premium Amount')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:05.720757Z","iopub.execute_input":"2024-12-08T07:54:05.721213Z","iopub.status.idle":"2024-12-08T07:54:05.72784Z","shell.execute_reply.started":"2024-12-08T07:54:05.721152Z","shell.execute_reply":"2024-12-08T07:54:05.726751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"float_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:05.729588Z","iopub.execute_input":"2024-12-08T07:54:05.72993Z","iopub.status.idle":"2024-12-08T07:54:05.748438Z","shell.execute_reply.started":"2024-12-08T07:54:05.729898Z","shell.execute_reply":"2024-12-08T07:54:05.747068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in float_cols:\n    plt.figure(figsize=(10, 4)) \n    sns.displot(data[col], kde = True)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:05.750102Z","iopub.execute_input":"2024-12-08T07:54:05.750629Z","iopub.status.idle":"2024-12-08T07:54:55.706525Z","shell.execute_reply.started":"2024-12-08T07:54:05.750588Z","shell.execute_reply":"2024-12-08T07:54:55.705278Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"sample_data['Age_x_Health'] = sample_data['Age'] * sample_data['Health Score']\n\ndata['Age_x_Health'] = data['Age'] * sample_data['Health Score']","metadata":{"execution":{"iopub.status.busy":"2024-12-05T22:20:15.37758Z","iopub.execute_input":"2024-12-05T22:20:15.378112Z","iopub.status.idle":"2024-12-05T22:20:15.387608Z","shell.execute_reply.started":"2024-12-05T22:20:15.378074Z","shell.execute_reply":"2024-12-05T22:20:15.386332Z"}},"attachments":{}},{"cell_type":"code","source":"float_cols = sample_data.select_dtypes('float64').columns.tolist()\n# float_cols.remove('Premium Amount')\nfloat_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:55.708353Z","iopub.execute_input":"2024-12-08T07:54:55.70874Z","iopub.status.idle":"2024-12-08T07:54:55.717584Z","shell.execute_reply.started":"2024-12-08T07:54:55.708705Z","shell.execute_reply":"2024-12-08T07:54:55.71624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for col in float_cols:\n    print(col)\n    plot_data = sample_data[[col,'Premium Amount']].dropna()\n    full_data = data[[col,'Premium Amount']].dropna()\n    \n    x = sample_data.dropna()[col]\n    y = sample_data.dropna()['Premium Amount']\n    plot = plt.scatter(x, y)\n    \n    z = np.polyfit(x, y, 1)\n    p = np.poly1d(z)\n    plt.plot(x,p(x),\"r--\")\n\n    correlation = full_data.corr(method = 'pearson').iloc[0,1]\n    correlation = str(round(correlation,4))\n    \n    covariance = full_data.cov().iloc[0,1]\n    covariance = str(round(covariance,4))\n\n    plt.title(col + '\\n Correlation: ' + correlation + '\\n Covariance' + covariance)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:55.719041Z","iopub.execute_input":"2024-12-08T07:54:55.719422Z","iopub.status.idle":"2024-12-08T07:54:58.310858Z","shell.execute_reply.started":"2024-12-08T07:54:55.719387Z","shell.execute_reply":"2024-12-08T07:54:58.309725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.select_dtypes('object').head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:58.312649Z","iopub.execute_input":"2024-12-08T07:54:58.313148Z","iopub.status.idle":"2024-12-08T07:54:58.458732Z","shell.execute_reply.started":"2024-12-08T07:54:58.313096Z","shell.execute_reply":"2024-12-08T07:54:58.457514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.select_dtypes('object').nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:58.460127Z","iopub.execute_input":"2024-12-08T07:54:58.460638Z","iopub.status.idle":"2024-12-08T07:54:59.626861Z","shell.execute_reply.started":"2024-12-08T07:54:58.4606Z","shell.execute_reply":"2024-12-08T07:54:59.625521Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Transformations","metadata":{}},{"cell_type":"markdown","source":"## log transformations\n\n#### Preparing to use log transformations and then use exponential to go back to original in order to train based on RMSLE.\n\n#### The code below is making sure I have the functions right","metadata":{}},{"cell_type":"code","source":"math.log10(1000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:59.628201Z","iopub.execute_input":"2024-12-08T07:54:59.628576Z","iopub.status.idle":"2024-12-08T07:54:59.635459Z","shell.execute_reply.started":"2024-12-08T07:54:59.628541Z","shell.execute_reply":"2024-12-08T07:54:59.634283Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"10**3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:59.636887Z","iopub.execute_input":"2024-12-08T07:54:59.637211Z","iopub.status.idle":"2024-12-08T07:54:59.649556Z","shell.execute_reply.started":"2024-12-08T07:54:59.637181Z","shell.execute_reply":"2024-12-08T07:54:59.64838Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['Premium Amount'].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:59.650773Z","iopub.execute_input":"2024-12-08T07:54:59.651132Z","iopub.status.idle":"2024-12-08T07:54:59.665663Z","shell.execute_reply.started":"2024-12-08T07:54:59.651096Z","shell.execute_reply":"2024-12-08T07:54:59.664178Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Will train using log transformed...\n\nSince models train using RMSE scoring. RMSLE in only available in newer versions of sklearn, which requires a newer Python version (3.9). Furthermore, you have to create custom loss functions and eval metrics for catboost, and I was having issues with that. It's easier to just to the transformations (log base 10 transformation) prior to training, then reverse it back (exponential function).","metadata":{}},{"cell_type":"code","source":"log_transformed = data['Premium Amount'].head().apply(lambda x: math.log10(x))\nlog_transformed","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:59.667167Z","iopub.execute_input":"2024-12-08T07:54:59.667657Z","iopub.status.idle":"2024-12-08T07:54:59.679934Z","shell.execute_reply.started":"2024-12-08T07:54:59.667603Z","shell.execute_reply":"2024-12-08T07:54:59.678824Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"back to original, works as expected","metadata":{}},{"cell_type":"code","source":"# exponential = \nlog_transformed.apply(lambda x: 10**x)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:59.681612Z","iopub.execute_input":"2024-12-08T07:54:59.682046Z","iopub.status.idle":"2024-12-08T07:54:59.698648Z","shell.execute_reply.started":"2024-12-08T07:54:59.682008Z","shell.execute_reply":"2024-12-08T07:54:59.697333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def log_transform(X):\n    return X.apply(lambda x: math.log10(x))\n\ndef exponential_transform(X):\n    if type(X) == np.ndarray:\n        X = 10 ** X\n    else:\n        X = X.apply(lambda x: 10**x) \n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:59.699773Z","iopub.execute_input":"2024-12-08T07:54:59.700087Z","iopub.status.idle":"2024-12-08T07:54:59.710572Z","shell.execute_reply.started":"2024-12-08T07:54:59.700055Z","shell.execute_reply":"2024-12-08T07:54:59.709438Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Xy","metadata":{}},{"cell_type":"code","source":"X = data.drop('Premium Amount', axis = 1).copy()\nX = data.copy()\ny = data['Premium Amount'].copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:54:59.718704Z","iopub.execute_input":"2024-12-08T07:54:59.719133Z","iopub.status.idle":"2024-12-08T07:55:00.426926Z","shell.execute_reply.started":"2024-12-08T07:54:59.719091Z","shell.execute_reply":"2024-12-08T07:55:00.425471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X['Policy Start Date'].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:00.428219Z","iopub.execute_input":"2024-12-08T07:55:00.428668Z","iopub.status.idle":"2024-12-08T07:55:00.43697Z","shell.execute_reply.started":"2024-12-08T07:55:00.42862Z","shell.execute_reply":"2024-12-08T07:55:00.435705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X['Policy Start DateTime'] = pd.to_datetime(X['Policy Start Date'])\nX['Policy Start DateTime'].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:00.43841Z","iopub.execute_input":"2024-12-08T07:55:00.43881Z","iopub.status.idle":"2024-12-08T07:55:00.870791Z","shell.execute_reply.started":"2024-12-08T07:55:00.438775Z","shell.execute_reply":"2024-12-08T07:55:00.869366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X['Day'] = X['Policy Start DateTime'].dt.day\nX['Month'] = X['Policy Start DateTime'].dt.month\nX['Year'] = X['Policy Start DateTime'].dt.year\nX['Day of Week'] = X['Policy Start DateTime'].dt.day_name()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:00.872409Z","iopub.execute_input":"2024-12-08T07:55:00.87288Z","iopub.status.idle":"2024-12-08T07:55:01.452651Z","shell.execute_reply.started":"2024-12-08T07:55:00.872825Z","shell.execute_reply":"2024-12-08T07:55:01.451475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"piv = X.pivot_table(index=['Year','Month'], \n                    columns='Day', \n                    values=['Premium Amount'], aggfunc='mean')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:01.454746Z","iopub.execute_input":"2024-12-08T07:55:01.455203Z","iopub.status.idle":"2024-12-08T07:55:01.556633Z","shell.execute_reply.started":"2024-12-08T07:55:01.455103Z","shell.execute_reply":"2024-12-08T07:55:01.555556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# piv.to_excel('pivot calendar data.xlsx')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:01.558228Z","iopub.execute_input":"2024-12-08T07:55:01.558722Z","iopub.status.idle":"2024-12-08T07:55:01.564848Z","shell.execute_reply.started":"2024-12-08T07:55:01.558669Z","shell.execute_reply":"2024-12-08T07:55:01.563337Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Premium by Month and Day\n\nCreated in Excel","metadata":{}},{"cell_type":"code","source":"from IPython.display import Image\nImage(filename='/kaggle/input/premium-by-date/Premium by Date.jpg') ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:01.56656Z","iopub.execute_input":"2024-12-08T07:55:01.566947Z","iopub.status.idle":"2024-12-08T07:55:01.616788Z","shell.execute_reply.started":"2024-12-08T07:55:01.566902Z","shell.execute_reply":"2024-12-08T07:55:01.61565Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[X['Day']==31][['Month','Premium Amount']].groupby('Month').agg('mean')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:01.618052Z","iopub.execute_input":"2024-12-08T07:55:01.618419Z","iopub.status.idle":"2024-12-08T07:55:01.661024Z","shell.execute_reply.started":"2024-12-08T07:55:01.618384Z","shell.execute_reply":"2024-12-08T07:55:01.659739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[['Premium Amount','Day of Week']].groupby('Day of Week').agg('mean')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:01.662283Z","iopub.execute_input":"2024-12-08T07:55:01.662614Z","iopub.status.idle":"2024-12-08T07:55:01.790366Z","shell.execute_reply.started":"2024-12-08T07:55:01.662582Z","shell.execute_reply":"2024-12-08T07:55:01.789304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[['Premium Amount','Day']].groupby('Day').agg('mean')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:01.792101Z","iopub.execute_input":"2024-12-08T07:55:01.792454Z","iopub.status.idle":"2024-12-08T07:55:01.83237Z","shell.execute_reply.started":"2024-12-08T07:55:01.792418Z","shell.execute_reply":"2024-12-08T07:55:01.831045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X['Policy Start Date'].apply(lambda x: x[:7]).head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:01.833964Z","iopub.execute_input":"2024-12-08T07:55:01.834441Z","iopub.status.idle":"2024-12-08T07:55:02.166191Z","shell.execute_reply.started":"2024-12-08T07:55:01.834389Z","shell.execute_reply":"2024-12-08T07:55:02.165064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X['Policy Start Date'].apply(lambda x: x[:7]).rank(method = 'dense')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:02.167185Z","iopub.execute_input":"2024-12-08T07:55:02.167503Z","iopub.status.idle":"2024-12-08T07:55:05.410181Z","shell.execute_reply.started":"2024-12-08T07:55:02.167474Z","shell.execute_reply":"2024-12-08T07:55:05.409075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X['Year'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.411555Z","iopub.execute_input":"2024-12-08T07:55:05.411912Z","iopub.status.idle":"2024-12-08T07:55:05.429331Z","shell.execute_reply.started":"2024-12-08T07:55:05.411877Z","shell.execute_reply":"2024-12-08T07:55:05.42803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[X['Year']==2019].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.430911Z","iopub.execute_input":"2024-12-08T07:55:05.431413Z","iopub.status.idle":"2024-12-08T07:55:05.489996Z","shell.execute_reply.started":"2024-12-08T07:55:05.43136Z","shell.execute_reply":"2024-12-08T07:55:05.4888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def transform_policy_start_date(X):\n    X['Policy Start Date'] = X['Policy Start Date'].apply(lambda x: x[:7]).rank(method = 'dense')\n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.491227Z","iopub.execute_input":"2024-12-08T07:55:05.491556Z","iopub.status.idle":"2024-12-08T07:55:05.496736Z","shell.execute_reply.started":"2024-12-08T07:55:05.491527Z","shell.execute_reply":"2024-12-08T07:55:05.495653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def transform_policy_start_date_year(X):\n    X['Policy Start Year'] = X['Policy Start Date'].apply(lambda x: x[:4]).rank(method = 'dense')\n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.498654Z","iopub.execute_input":"2024-12-08T07:55:05.499336Z","iopub.status.idle":"2024-12-08T07:55:05.5109Z","shell.execute_reply.started":"2024-12-08T07:55:05.499241Z","shell.execute_reply":"2024-12-08T07:55:05.509799Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def transform_policy_start_year_string(X):\n    X['Policy Start Year'] = X['Policy Start Date'].apply(lambda x: x[:4]).astype('str')\n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.512539Z","iopub.execute_input":"2024-12-08T07:55:05.513037Z","iopub.status.idle":"2024-12-08T07:55:05.524675Z","shell.execute_reply.started":"2024-12-08T07:55:05.512988Z","shell.execute_reply":"2024-12-08T07:55:05.523588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def drop_policy_start_date(X):\n    X = X.drop('Policy Start Date',axis = 1)\n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.526167Z","iopub.execute_input":"2024-12-08T07:55:05.526635Z","iopub.status.idle":"2024-12-08T07:55:05.538517Z","shell.execute_reply.started":"2024-12-08T07:55:05.526585Z","shell.execute_reply":"2024-12-08T07:55:05.537304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def drop_id(X):\n    X = X.drop('id',axis = 1)\n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.539863Z","iopub.execute_input":"2024-12-08T07:55:05.540279Z","iopub.status.idle":"2024-12-08T07:55:05.552354Z","shell.execute_reply.started":"2024-12-08T07:55:05.540221Z","shell.execute_reply":"2024-12-08T07:55:05.551214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def previous_claim_as_str(X):\n    X['Previous Claims'] = X['Previous Claims'].astype('str')\n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.554163Z","iopub.execute_input":"2024-12-08T07:55:05.554615Z","iopub.status.idle":"2024-12-08T07:55:05.565439Z","shell.execute_reply.started":"2024-12-08T07:55:05.554578Z","shell.execute_reply":"2024-12-08T07:55:05.564226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# X = drop_policy_start_date(X)\nX = transform_policy_start_date(X)\nX = previous_claim_as_str(X)\nX = drop_id(X)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:05.566916Z","iopub.execute_input":"2024-12-08T07:55:05.567398Z","iopub.status.idle":"2024-12-08T07:55:09.666538Z","shell.execute_reply.started":"2024-12-08T07:55:05.567346Z","shell.execute_reply":"2024-12-08T07:55:09.665325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"global obj_cols\nobj_cols = X.select_dtypes('object').columns.tolist()\nX[obj_cols].nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:09.667764Z","iopub.execute_input":"2024-12-08T07:55:09.668104Z","iopub.status.idle":"2024-12-08T07:55:11.51582Z","shell.execute_reply.started":"2024-12-08T07:55:09.668072Z","shell.execute_reply":"2024-12-08T07:55:11.514325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[obj_cols].isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:11.517567Z","iopub.execute_input":"2024-12-08T07:55:11.517905Z","iopub.status.idle":"2024-12-08T07:55:12.37068Z","shell.execute_reply.started":"2024-12-08T07:55:11.517874Z","shell.execute_reply":"2024-12-08T07:55:12.36936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[obj_cols].nunique().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:12.372205Z","iopub.execute_input":"2024-12-08T07:55:12.372678Z","iopub.status.idle":"2024-12-08T07:55:13.308255Z","shell.execute_reply.started":"2024-12-08T07:55:12.372626Z","shell.execute_reply":"2024-12-08T07:55:13.307049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def obj_cols_fill_na(X):\n    X[obj_cols] = X[obj_cols].fillna('missing')\n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:13.310135Z","iopub.execute_input":"2024-12-08T07:55:13.310627Z","iopub.status.idle":"2024-12-08T07:55:13.316282Z","shell.execute_reply.started":"2024-12-08T07:55:13.310573Z","shell.execute_reply":"2024-12-08T07:55:13.315061Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"X = obj_cols_fill_na(X)\nX[obj_cols].isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-12-05T19:29:58.815084Z","iopub.execute_input":"2024-12-05T19:29:58.815464Z","iopub.status.idle":"2024-12-05T19:30:00.730441Z","shell.execute_reply.started":"2024-12-05T19:29:58.815415Z","shell.execute_reply":"2024-12-05T19:30:00.729293Z"}}},{"cell_type":"code","source":"X[obj_cols].nunique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:13.317843Z","iopub.execute_input":"2024-12-08T07:55:13.318243Z","iopub.status.idle":"2024-12-08T07:55:14.253412Z","shell.execute_reply.started":"2024-12-08T07:55:13.318207Z","shell.execute_reply":"2024-12-08T07:55:14.252331Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"X[obj_cols].nunique().sum()","metadata":{"execution":{"iopub.status.busy":"2024-12-05T19:46:01.147872Z","iopub.execute_input":"2024-12-05T19:46:01.148289Z","iopub.status.idle":"2024-12-05T19:46:01.996532Z","shell.execute_reply.started":"2024-12-05T19:46:01.148256Z","shell.execute_reply":"2024-12-05T19:46:01.995167Z"}}},{"cell_type":"code","source":"X.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:14.254822Z","iopub.execute_input":"2024-12-08T07:55:14.25537Z","iopub.status.idle":"2024-12-08T07:55:14.263981Z","shell.execute_reply.started":"2024-12-08T07:55:14.255206Z","shell.execute_reply":"2024-12-08T07:55:14.26257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"global float_cols\nfloat_cols = X.select_dtypes('float64').columns.tolist()\nfloat_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:14.26548Z","iopub.execute_input":"2024-12-08T07:55:14.265842Z","iopub.status.idle":"2024-12-08T07:55:14.374487Z","shell.execute_reply.started":"2024-12-08T07:55:14.265795Z","shell.execute_reply":"2024-12-08T07:55:14.373321Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Transformations","metadata":{}},{"cell_type":"code","source":"qt = QuantileTransformer(output_distribution='uniform', copy = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:14.375891Z","iopub.execute_input":"2024-12-08T07:55:14.376413Z","iopub.status.idle":"2024-12-08T07:55:14.381335Z","shell.execute_reply.started":"2024-12-08T07:55:14.376361Z","shell.execute_reply":"2024-12-08T07:55:14.380308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X[float_cols].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:14.382624Z","iopub.execute_input":"2024-12-08T07:55:14.383111Z","iopub.status.idle":"2024-12-08T07:55:14.430197Z","shell.execute_reply.started":"2024-12-08T07:55:14.383073Z","shell.execute_reply":"2024-12-08T07:55:14.429035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_check_it = X[float_cols]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:14.43168Z","iopub.execute_input":"2024-12-08T07:55:14.432028Z","iopub.status.idle":"2024-12-08T07:55:14.470333Z","shell.execute_reply.started":"2024-12-08T07:55:14.431993Z","shell.execute_reply":"2024-12-08T07:55:14.469242Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"X_check_it = qt.fit_transform(X[float_cols])\n\n['QT_'+i for i in float_cols]","metadata":{"execution":{"iopub.status.busy":"2024-12-07T06:36:40.342449Z","iopub.execute_input":"2024-12-07T06:36:40.342864Z","iopub.status.idle":"2024-12-07T06:36:42.736422Z","shell.execute_reply.started":"2024-12-07T06:36:40.342828Z","shell.execute_reply":"2024-12-07T06:36:42.735129Z"}},"attachments":{}},{"cell_type":"code","source":" X_check_it[['QT_'+i for i in float_cols]] = qt.fit_transform(X_check_it)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:14.471978Z","iopub.execute_input":"2024-12-08T07:55:14.472468Z","iopub.status.idle":"2024-12-08T07:55:16.857341Z","shell.execute_reply.started":"2024-12-08T07:55:14.472419Z","shell.execute_reply":"2024-12-08T07:55:16.85609Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"new_cols = float_cols + ['QT_'+i for i in float_cols]\nnew_cols = ['QT_'+i for i in float_cols]","metadata":{"execution":{"iopub.status.busy":"2024-12-07T06:36:42.738468Z","iopub.execute_input":"2024-12-07T06:36:42.738843Z","iopub.status.idle":"2024-12-07T06:36:42.744156Z","shell.execute_reply.started":"2024-12-07T06:36:42.738807Z","shell.execute_reply":"2024-12-07T06:36:42.743098Z"}}},{"cell_type":"code","source":"X_check_it.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.858598Z","iopub.execute_input":"2024-12-08T07:55:16.85889Z","iopub.status.idle":"2024-12-08T07:55:16.883596Z","shell.execute_reply.started":"2024-12-08T07:55:16.858861Z","shell.execute_reply":"2024-12-08T07:55:16.882474Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"X_check_it_df = pd.DataFrame(X_check_it, columns=new_cols)\nX_check_it_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-12-07T06:44:13.497115Z","iopub.execute_input":"2024-12-07T06:44:13.4975Z","iopub.status.idle":"2024-12-07T06:44:13.583453Z","shell.execute_reply.started":"2024-12-07T06:44:13.497463Z","shell.execute_reply":"2024-12-07T06:44:13.582211Z"}}},{"cell_type":"code","source":"\nquantile_pipeline = Pipeline(steps=[\n    ('quantile_transformer', QuantileTransformer(output_distribution='uniform', copy = True))\n])\n\nmin_max_pipeline = Pipeline(steps=[\n    ('min_max_transformer', MinMaxScaler(feature_range = (.01,.99), copy=False))\n])\n\n\nstandard_scaler_pipeline = Pipeline(steps=[\n    ('standard_scaler', StandardScaler(copy = False))\n])\n\nmean_imputer_pipeline = Pipeline(steps=[\n    ('mean_imputer', SimpleImputer(strategy='mean', copy = True))\n])\n\nohe_pipeline = Pipeline(steps=[\n    ('ohe', OneHotEncoder(handle_unknown='ignore', sparse_output=False))\n])\n\nohe_pipeline = Pipeline(steps=[\n    ('ohe', OneHotEncoder(handle_unknown='ignore', sparse_output=True))\n])\n\nmost_frequent_imputer_pipeline = Pipeline(steps=[\n    ('most_frequent_imputer', SimpleImputer(strategy='most_frequent'))\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.884791Z","iopub.execute_input":"2024-12-08T07:55:16.885126Z","iopub.status.idle":"2024-12-08T07:55:16.894446Z","shell.execute_reply.started":"2024-12-08T07:55:16.885094Z","shell.execute_reply":"2024-12-08T07:55:16.893329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"global col_trans\n\ncol_trans = ColumnTransformer(transformers=[\n    # ('most_frequent_imputer', most_frequent_imputer_pipeline, obj_cols),\n    ('quantile_transformer', quantile_pipeline, float_cols),\n    # ('standard_scaler', standard_scaler_pipeline, float_cols),\n    ('ohe_pipeline', ohe_pipeline, obj_cols)\n    # ('mean_imputer', mean_imputer_pipeline, float_cols),\n    ],\n    remainder='passthrough', \n    # n_jobs=-1)\n    n_jobs= 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.895985Z","iopub.execute_input":"2024-12-08T07:55:16.896372Z","iopub.status.idle":"2024-12-08T07:55:16.911884Z","shell.execute_reply.started":"2024-12-08T07:55:16.896327Z","shell.execute_reply":"2024-12-08T07:55:16.910645Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"def all_transformations(X,fit_transform = 1):\n    # X = drop_policy_start_date(X)\n    # print('drop_policy_start_date')\n    # X = obj_cols_fill_na(X)\n    print(X.shape)\n    X['Annual Income'] = log_transform(X['Annual Income'])\n    X = transform_policy_start_date(X)\n    print('transform_policy_start_date')\n    print(X.shape)\n    X = drop_id(X)\n    X = previous_claim_as_str(X)\n    print('drop_id')\n    print(X.shape)\n    if fit_transform:\n        print('running col_trans fit_transform')\n        X = col_trans.fit_transform(X)\n        print(X.shape)\n    else:\n        print('running col_trans transform')\n        X = col_trans.transform(X)\n        print(X.shape)\n    return X","metadata":{"execution":{"iopub.status.busy":"2024-12-05T19:30:03.787882Z","iopub.execute_input":"2024-12-05T19:30:03.788247Z","iopub.status.idle":"2024-12-05T19:30:03.802331Z","shell.execute_reply.started":"2024-12-05T19:30:03.788214Z","shell.execute_reply":"2024-12-05T19:30:03.801045Z"}}},{"cell_type":"code","source":"def add_trans(x, data):\n    transformations[x] = 1\n    print(x)\n    print(data.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.913545Z","iopub.execute_input":"2024-12-08T07:55:16.914003Z","iopub.status.idle":"2024-12-08T07:55:16.924381Z","shell.execute_reply.started":"2024-12-08T07:55:16.913951Z","shell.execute_reply":"2024-12-08T07:55:16.92334Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in col_trans.transformers:\n    print(i[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.925835Z","iopub.execute_input":"2024-12-08T07:55:16.926208Z","iopub.status.idle":"2024-12-08T07:55:16.94274Z","shell.execute_reply.started":"2024-12-08T07:55:16.926172Z","shell.execute_reply":"2024-12-08T07:55:16.941546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in col_trans.transformers:\n    print(i[1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.944303Z","iopub.execute_input":"2024-12-08T07:55:16.944677Z","iopub.status.idle":"2024-12-08T07:55:16.96119Z","shell.execute_reply.started":"2024-12-08T07:55:16.94462Z","shell.execute_reply":"2024-12-08T07:55:16.960087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in col_trans.transformers:\n    print(i[2])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.962451Z","iopub.execute_input":"2024-12-08T07:55:16.962895Z","iopub.status.idle":"2024-12-08T07:55:16.974922Z","shell.execute_reply.started":"2024-12-08T07:55:16.962855Z","shell.execute_reply":"2024-12-08T07:55:16.973787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def all_transformations(X,fit_transform = 1):\n    # X['Annual Income_Log_Col_added'] = log_transform(X['Annual Income'])\n    # add_trans('Annual Income_Log_Col_added',X)\n\n    # X['Credit Score_Log_Col_added'] = log_transform(X['Credit Score'])\n    # add_trans('Credit Score_Log_Col_added',X)\n    \n    X['Annual Income'] = log_transform(X['Annual Income'])\n    add_trans('log_transform_income',X)\n    \n    # X = transform_policy_start_date_year(X)\n    # add_trans('transform_policy_start_date_year',X)\n\n    # X = transform_policy_start_year_string(X)\n    # add_trans('transform_policy_start_year_string',X)\n    \n    X = transform_policy_start_date(X)\n    add_trans('transform_policy_start_date',X)\n    \n    X = drop_id(X)\n    add_trans('drop_id',X)\n    \n    # X = previous_claim_as_str(X)\n    # add_trans('previous_claim_as_str',X)\n\n    global obj_cols\n    obj_cols = X.select_dtypes('object').columns.tolist()\n    transformations['obj'] = len(obj_cols)\n\n    global float_cols\n    float_cols = X.select_dtypes('float64').columns.tolist()\n    transformations['float'] = len(float_cols)\n\n    if fit_transform:\n        global col_trans\n        col_trans = ColumnTransformer(transformers=[\n            ('standard_scaler', standard_scaler_pipeline, float_cols),\n            # ('mean_imputer', mean_imputer_pipeline, float_cols),\n            # ('quantile_transformer', quantile_pipeline, float_cols),\n            # ('min_max_transformer', min_max_pipeline, float_cols),\n            ('ohe_pipeline', ohe_pipeline, obj_cols)\n            ],\n            remainder='passthrough', \n            n_jobs= 1)\n    \n    print(float_cols)\n    print(obj_cols)\n    \n    X = X[float_cols + obj_cols]\n\n    if fit_transform:\n        print('running col_trans fit_transform')\n        X = col_trans.fit_transform(X)\n        for i in col_trans.transformers:\n            add_trans(i[0],X)\n    else:\n        print('running col_trans transform')\n        X = col_trans.transform(X)\n        for i in col_trans.transformers:\n            add_trans(i[0],X)\n\n    global obj_col_names\n    obj_col_names = col_trans.transformers_[1][1].get_feature_names_out().tolist()\n    # obj_col_names = col_trans.transformers[1][1].get_feature_names_out()\n    \n    return X","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.976549Z","iopub.execute_input":"2024-12-08T07:55:16.977015Z","iopub.status.idle":"2024-12-08T07:55:16.989813Z","shell.execute_reply.started":"2024-12-08T07:55:16.976964Z","shell.execute_reply":"2024-12-08T07:55:16.988719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transformations = {}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:16.991511Z","iopub.execute_input":"2024-12-08T07:55:16.991974Z","iopub.status.idle":"2024-12-08T07:55:17.007991Z","shell.execute_reply.started":"2024-12-08T07:55:16.991923Z","shell.execute_reply":"2024-12-08T07:55:17.006801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = data.drop('Premium Amount', axis = 1).copy()\ny = data['Premium Amount'].copy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:17.009468Z","iopub.execute_input":"2024-12-08T07:55:17.009812Z","iopub.status.idle":"2024-12-08T07:55:17.597319Z","shell.execute_reply.started":"2024-12-08T07:55:17.009779Z","shell.execute_reply":"2024-12-08T07:55:17.596221Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = all_transformations(X)\ny = log_transform(y)\nX.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:17.598616Z","iopub.execute_input":"2024-12-08T07:55:17.598964Z","iopub.status.idle":"2024-12-08T07:55:27.03035Z","shell.execute_reply.started":"2024-12-08T07:55:17.598894Z","shell.execute_reply":"2024-12-08T07:55:27.029075Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train Test Split","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=25)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.031509Z","iopub.execute_input":"2024-12-08T07:55:27.031827Z","iopub.status.idle":"2024-12-08T07:55:27.312177Z","shell.execute_reply.started":"2024-12-08T07:55:27.031795Z","shell.execute_reply":"2024-12-08T07:55:27.310771Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"if use_old_log:\n    try:\n        with open('/kaggle/working/transformations_log.pkl', 'rb') as f:\n            read_pickle = pickle.load(f)\n        transformations_log = read_pickle.copy()\n        model_counter = int(transformations_log.columns.max())\n    except:\n        print('used exception')\n        with open('/kaggle/input/transformations-log/transformations_log.pkl', 'rb') as f:\n            # Load the pickled object from the file\n            read_pickle = pickle.load(f)\n        transformations_log = read_pickle.copy()\n        model_counter = int(transformations_log.columns.max())\nelse:\n    model_counter = 0\n    transformations_log = pd.DataFrame()","metadata":{}},{"cell_type":"markdown","source":"# Model Testing Prep","metadata":{}},{"cell_type":"code","source":"import catboost as ctb\nimport lightgbm as lgb\nimport xgboost as xgb","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.313859Z","iopub.execute_input":"2024-12-08T07:55:27.314367Z","iopub.status.idle":"2024-12-08T07:55:27.931845Z","shell.execute_reply.started":"2024-12-08T07:55:27.314313Z","shell.execute_reply":"2024-12-08T07:55:27.930532Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.model_selection import cross_val_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.933811Z","iopub.execute_input":"2024-12-08T07:55:27.934442Z","iopub.status.idle":"2024-12-08T07:55:27.93949Z","shell.execute_reply.started":"2024-12-08T07:55:27.934399Z","shell.execute_reply":"2024-12-08T07:55:27.938351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cv = KFold(n_splits=5, shuffle = True, random_state=10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.941173Z","iopub.execute_input":"2024-12-08T07:55:27.941949Z","iopub.status.idle":"2024-12-08T07:55:27.953501Z","shell.execute_reply.started":"2024-12-08T07:55:27.941898Z","shell.execute_reply":"2024-12-08T07:55:27.952279Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"catboost = ctb.CatBoostRegressor(verbose = 100)\nlightgbm = lgb.LGBMRegressor()\nlightgbm = lgb.LGBMRegressor(verbose = -1)\nxgboost = xgb.XGBRegressor()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.955152Z","iopub.execute_input":"2024-12-08T07:55:27.955696Z","iopub.status.idle":"2024-12-08T07:55:27.968404Z","shell.execute_reply.started":"2024-12-08T07:55:27.955634Z","shell.execute_reply":"2024-12-08T07:55:27.967348Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_dict = {\n    'lightgbm':lightgbm\n    # 'catboost':catboost,\n    # 'xgboost':xgboost\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.969736Z","iopub.execute_input":"2024-12-08T07:55:27.970079Z","iopub.status.idle":"2024-12-08T07:55:27.980737Z","shell.execute_reply.started":"2024-12-08T07:55:27.970045Z","shell.execute_reply":"2024-12-08T07:55:27.979624Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Logging Model Performance","metadata":{}},{"cell_type":"code","source":"use_old_log = 1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.982354Z","iopub.execute_input":"2024-12-08T07:55:27.982821Z","iopub.status.idle":"2024-12-08T07:55:27.996309Z","shell.execute_reply.started":"2024-12-08T07:55:27.982771Z","shell.execute_reply":"2024-12-08T07:55:27.995102Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Code to handle the exception\nif use_old_log:\n    try:\n        # with open('/kaggle/input/insur_premium_transformations_log/insur_premium_transformations_log.pkl', 'rb') as f:\n        with open('/kaggle/input/insur-premium-transformations-log/insur_premium_transformations_log.pkl', 'rb') as f:\n            read_pickle = pickle.load(f)\n        transformations_log = read_pickle.copy()\n        model_counter = max(transformations_log.columns.astype('int'))\n    except:\n        model_counter = 0\n        transformations_log = pd.DataFrame()\nelse:\n    model_counter = 0\n    transformations_log = pd.DataFrame()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:27.997923Z","iopub.execute_input":"2024-12-08T07:55:27.998427Z","iopub.status.idle":"2024-12-08T07:55:28.01487Z","shell.execute_reply.started":"2024-12-08T07:55:27.998376Z","shell.execute_reply":"2024-12-08T07:55:28.01381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transformations_log","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.016349Z","iopub.execute_input":"2024-12-08T07:55:28.016761Z","iopub.status.idle":"2024-12-08T07:55:28.044248Z","shell.execute_reply.started":"2024-12-08T07:55:28.016716Z","shell.execute_reply":"2024-12-08T07:55:28.04312Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Optuna Tuning","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\nfrom math import sqrt\n\n\nimport optuna\n\n# from sklearn.metrics import accuracy_score\n\n# accuracy_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.045836Z","iopub.execute_input":"2024-12-08T07:55:28.04618Z","iopub.status.idle":"2024-12-08T07:55:28.102864Z","shell.execute_reply.started":"2024-12-08T07:55:28.046149Z","shell.execute_reply":"2024-12-08T07:55:28.101748Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# credit to\n# https://www.kaggle.com/code/adrienmorel97/eda-lightgbm-optuna-1-0644-v1/notebook\n# I needed to look this up since I'm still new to Optuna tuning\n# I still need to understanding what this is actually doing in more detail\n\ndef objective(trial):\n    # Define parameter search space\n    params = {\n        \"objective\": \"regression\",\n        \"metric\": \"rmse\",\n        \"boosting_type\": trial.suggest_categorical(\"boosting_type\", [\"gbdt\", \"dart\"]),\n        \"num_leaves\": trial.suggest_int(\"num_leaves\", 200, 512),\n        \"learning_rate\": trial.suggest_loguniform(\"learning_rate\", 1e-2, 1e-1),\n        # \"learning_rate\": trial.suggest_loguniform(\"learning_rate\", 5e-3, 1e-1),\n        \"feature_fraction\": trial.suggest_uniform(\"feature_fraction\", 0.6, 1.0),\n        \"bagging_fraction\": trial.suggest_uniform(\"bagging_fraction\", 0.6, 1.0),\n        \"bagging_freq\": trial.suggest_int(\"bagging_freq\", 5, 12),\n        \"min_data_in_leaf\": trial.suggest_int(\"min_data_in_leaf\", 20, 100),\n        \"max_depth\": trial.suggest_int(\"max_depth\", -1, 16),  # -1 means no limit\n        \"lambda_l1\": trial.suggest_loguniform(\"lambda_l1\", 1e-4, 10.0),\n        \"lambda_l2\": trial.suggest_loguniform(\"lambda_l2\", 1e-4, 10.0),\n        # \"device_type\": \"gpu\",  # Enable GPU support\n        \"seed\" : 50\n\n    }\n\n    model = lgb.LGBMRegressor(**params, silent = True)\n    # lgb.LGBMClassifier(**params, silent = True)\n    model.fit(X_train,y_train.values.ravel())\n    \n    global predictions\n    predictions = model.predict(X_test)\n    rmse = sqrt(mean_squared_error(y_test, predictions))\n    \n    return rmse\n    \n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.104193Z","iopub.execute_input":"2024-12-08T07:55:28.104553Z","iopub.status.idle":"2024-12-08T07:55:28.113754Z","shell.execute_reply.started":"2024-12-08T07:55:28.10452Z","shell.execute_reply":"2024-12-08T07:55:28.112356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"run = 0\nif run:\n    study = optuna.create_study(direction='maximize')\n    study.optimize(objective, n_trials=3)\n    print('Best hyperparameters:', study.best_params)\n    print('Best accuracy score:', study.best_value)\n    lightgbm_params = study.best_params\nelse:\n    lightgbm_params = {'boosting_type': 'dart', 'num_leaves': 431, 'learning_rate': 0.01045746877437017, 'feature_fraction': 0.6788746600509749, 'bagging_fraction': 0.8190021587977174, 'bagging_freq': 7, 'min_data_in_leaf': 53, 'max_depth': 15, 'lambda_l1': 3.889882911970894, 'lambda_l2': 0.057920564261415654}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.126985Z","iopub.execute_input":"2024-12-08T07:55:28.127445Z","iopub.status.idle":"2024-12-08T07:55:28.137616Z","shell.execute_reply.started":"2024-12-08T07:55:28.127401Z","shell.execute_reply":"2024-12-08T07:55:28.136352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lightgbm_params","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.139091Z","iopub.execute_input":"2024-12-08T07:55:28.139524Z","iopub.status.idle":"2024-12-08T07:55:28.157825Z","shell.execute_reply.started":"2024-12-08T07:55:28.139484Z","shell.execute_reply":"2024-12-08T07:55:28.1566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lightgbm_tuned = lgb.LGBMRegressor(**lightgbm_params)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.159454Z","iopub.execute_input":"2024-12-08T07:55:28.159845Z","iopub.status.idle":"2024-12-08T07:55:28.1689Z","shell.execute_reply.started":"2024-12-08T07:55:28.159794Z","shell.execute_reply":"2024-12-08T07:55:28.167679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import VotingRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.170689Z","iopub.execute_input":"2024-12-08T07:55:28.171384Z","iopub.status.idle":"2024-12-08T07:55:28.212443Z","shell.execute_reply.started":"2024-12-08T07:55:28.171325Z","shell.execute_reply":"2024-12-08T07:55:28.21137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import StackingRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.213901Z","iopub.execute_input":"2024-12-08T07:55:28.214271Z","iopub.status.idle":"2024-12-08T07:55:28.219422Z","shell.execute_reply.started":"2024-12-08T07:55:28.214216Z","shell.execute_reply":"2024-12-08T07:55:28.21832Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.220753Z","iopub.execute_input":"2024-12-08T07:55:28.221164Z","iopub.status.idle":"2024-12-08T07:55:28.232924Z","shell.execute_reply.started":"2024-12-08T07:55:28.221126Z","shell.execute_reply":"2024-12-08T07:55:28.231715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import HistGradientBoostingRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.234289Z","iopub.execute_input":"2024-12-08T07:55:28.234655Z","iopub.status.idle":"2024-12-08T07:55:28.247477Z","shell.execute_reply.started":"2024-12-08T07:55:28.234619Z","shell.execute_reply":"2024-12-08T07:55:28.246124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"catboost = ctb.CatBoostRegressor(verbose = 100)\nlightgbm = lgb.LGBMRegressor()\nlightgbm = lgb.LGBMRegressor(verbose = -1)\nxgboost = xgb.XGBRegressor()\nrandomforest = RandomForestRegressor()\nhist_gb = HistGradientBoostingRegressor()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.248971Z","iopub.execute_input":"2024-12-08T07:55:28.249414Z","iopub.status.idle":"2024-12-08T07:55:28.259302Z","shell.execute_reply.started":"2024-12-08T07:55:28.249366Z","shell.execute_reply":"2024-12-08T07:55:28.257889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"voting_regressor = VotingRegressor(estimators=[\n        ('cat', catboost),\n        ('lgb', lightgbm),\n        ('hist_gb',hist_gb)\n    ], weights = [.2,.6,.2], verbose=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.260761Z","iopub.execute_input":"2024-12-08T07:55:28.261155Z","iopub.status.idle":"2024-12-08T07:55:28.272026Z","shell.execute_reply.started":"2024-12-08T07:55:28.261118Z","shell.execute_reply":"2024-12-08T07:55:28.270774Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stacking_regressor = StackingRegressor(estimators=[\n        ('cat', catboost),\n        ('lgb', lightgbm),\n        ('hist_gb',hist_gb)\n    ] )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.273684Z","iopub.execute_input":"2024-12-08T07:55:28.274815Z","iopub.status.idle":"2024-12-08T07:55:28.285784Z","shell.execute_reply.started":"2024-12-08T07:55:28.274758Z","shell.execute_reply":"2024-12-08T07:55:28.284566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_dict = {\n    # 'lightgbm':lightgbm\n    # 'voting_cat2_lgb6_histgb2': voting_regressor\n    'stacking_cat_lgb_histgb': stacking_regressor\n    # 'lightgbmtuned': lightgbm_tuned\n    # 'catboost':catboost,\n    # 'hist_gb':hist_gb\n    # 'random_forest':randomforest\n    # 'xgboost':xgboost\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.287101Z","iopub.execute_input":"2024-12-08T07:55:28.287579Z","iopub.status.idle":"2024-12-08T07:55:28.300223Z","shell.execute_reply.started":"2024-12-08T07:55:28.287534Z","shell.execute_reply":"2024-12-08T07:55:28.299107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"run_model_tracking = 0\n\nif run_model_tracking:\n    \n    for model_key in model_dict:\n        print('\\n\\n\\n')\n    \n        global transformations    \n    \n        transformations = {}\n        start_time = time.time()\n    \n        print(model_key + ' started')\n        transformations['model'] = model_key\n        \n        X = data.drop('Premium Amount', axis = 1).copy()\n        y = data['Premium Amount'].copy()\n        \n        X = all_transformations(X)\n        y = log_transform(y)\n\n        \n    \n        cv_score = cross_val_score(model_dict[model_key], X, y.values.ravel(), scoring='neg_root_mean_squared_error', cv=cv, n_jobs=-1)\n    \n        transformations['score'] = cv_score.mean()\n        transformations['std'] = cv_score.std()\n        transformations['time'] = '{} sec'.format(round((time.time() - start_time),0))\n    \n        model_counter += 1\n        transformations_new = pd.DataFrame.from_dict(transformations, orient = 'index', columns = [str(model_counter)])\n        \n        if 'transformations_log' in globals():\n            transformations_log = pd.concat([transformations_log, transformations_new], join = 'outer', axis = 1)\n        else:\n            transformations_log = transformations_new\n        \n        print('\\n\\n')\n        print(model_key + ' done')\n        print('{} sec'.format(round((time.time() - start_time),0)))\n        print(cv_score)\n        print(cv_score.mean())\n\ntransformations_log        ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.301651Z","iopub.execute_input":"2024-12-08T07:55:28.302059Z","iopub.status.idle":"2024-12-08T07:55:28.333519Z","shell.execute_reply.started":"2024-12-08T07:55:28.302019Z","shell.execute_reply":"2024-12-08T07:55:28.332295Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Transformations Log","metadata":{}},{"cell_type":"code","source":"transformations_log.T.sort_values('score', ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.3352Z","iopub.execute_input":"2024-12-08T07:55:28.335934Z","iopub.status.idle":"2024-12-08T07:55:28.367557Z","shell.execute_reply.started":"2024-12-08T07:55:28.335879Z","shell.execute_reply":"2024-12-08T07:55:28.366349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"save_log = 0\n\nif save_log:\n    with open('insur_premium_transformations_log.pkl', \"wb\") as fp:\n        pickle.dump(transformations_log, fp)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.368914Z","iopub.execute_input":"2024-12-08T07:55:28.369281Z","iopub.status.idle":"2024-12-08T07:55:28.375206Z","shell.execute_reply.started":"2024-12-08T07:55:28.369216Z","shell.execute_reply":"2024-12-08T07:55:28.374137Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Add public scores manually\n\ntransformations_log.loc['public score'] = [np.nan for i in range(7)] + ['1.04471']\n\ntransformations_log","metadata":{"execution":{"iopub.status.busy":"2024-12-06T03:59:54.661932Z","iopub.execute_input":"2024-12-06T03:59:54.66245Z","iopub.status.idle":"2024-12-06T03:59:54.670493Z","shell.execute_reply.started":"2024-12-06T03:59:54.662413Z","shell.execute_reply":"2024-12-06T03:59:54.669203Z"}},"attachments":{}},{"cell_type":"markdown","source":"## Plot Feature Importance","metadata":{}},{"cell_type":"code","source":"X_cols = float_cols + obj_col_names\nlen(X_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.376687Z","iopub.execute_input":"2024-12-08T07:55:28.377036Z","iopub.status.idle":"2024-12-08T07:55:28.389385Z","shell.execute_reply.started":"2024-12-08T07:55:28.377001Z","shell.execute_reply":"2024-12-08T07:55:28.388248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_df = pd.DataFrame(X, columns=X_cols)\nX_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.390782Z","iopub.execute_input":"2024-12-08T07:55:28.391123Z","iopub.status.idle":"2024-12-08T07:55:28.42183Z","shell.execute_reply.started":"2024-12-08T07:55:28.391088Z","shell.execute_reply":"2024-12-08T07:55:28.420751Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#lightgbm.\n\nlgb.plot_importance(lightgbm, importance_type=\"split\", figsize=(8,10), title=\"LightGBM Feature Importance (Split)\")\n#lgb.plot_importance(lightgbm, importance_type=\"split\", title=\"LightGBM Feature Importance (Split)\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-08T07:48:37.086776Z","iopub.execute_input":"2024-12-08T07:48:37.087212Z","iopub.status.idle":"2024-12-08T07:48:37.246458Z","shell.execute_reply.started":"2024-12-08T07:48:37.087164Z","shell.execute_reply":"2024-12-08T07:48:37.245031Z"}}},{"cell_type":"markdown","source":"# Test and Transformations","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:28.42317Z","iopub.execute_input":"2024-12-08T07:55:28.423628Z","iopub.status.idle":"2024-12-08T07:55:31.365329Z","shell.execute_reply.started":"2024-12-08T07:55:28.423578Z","shell.execute_reply":"2024-12-08T07:55:31.363971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = all_transformations(test, 0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:31.366498Z","iopub.execute_input":"2024-12-08T07:55:31.366808Z","iopub.status.idle":"2024-12-08T07:55:36.783913Z","shell.execute_reply.started":"2024-12-08T07:55:31.366778Z","shell.execute_reply":"2024-12-08T07:55:36.782569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:36.785191Z","iopub.execute_input":"2024-12-08T07:55:36.785572Z","iopub.status.idle":"2024-12-08T07:55:36.792611Z","shell.execute_reply.started":"2024-12-08T07:55:36.785536Z","shell.execute_reply":"2024-12-08T07:55:36.791472Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Models","metadata":{}},{"cell_type":"code","source":"y.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:36.794365Z","iopub.execute_input":"2024-12-08T07:55:36.794689Z","iopub.status.idle":"2024-12-08T07:55:36.814323Z","shell.execute_reply.started":"2024-12-08T07:55:36.794658Z","shell.execute_reply":"2024-12-08T07:55:36.813068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model_used = catboost\n# model_used = lightgbm\nmodel_used = voting_regressor\nmodel_used = stacking_regressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:36.815811Z","iopub.execute_input":"2024-12-08T07:55:36.816291Z","iopub.status.idle":"2024-12-08T07:55:36.826696Z","shell.execute_reply.started":"2024-12-08T07:55:36.816215Z","shell.execute_reply":"2024-12-08T07:55:36.82563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_used.fit(X,y.values.ravel())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:55:36.827944Z","iopub.execute_input":"2024-12-08T07:55:36.828295Z","iopub.status.idle":"2024-12-08T07:58:04.619356Z","shell.execute_reply.started":"2024-12-08T07:55:36.828229Z","shell.execute_reply":"2024-12-08T07:58:04.618224Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"prediction = model_used.predict(test)\nprediction","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:04.620896Z","iopub.execute_input":"2024-12-08T07:58:04.621343Z","iopub.status.idle":"2024-12-08T07:58:31.904707Z","shell.execute_reply.started":"2024-12-08T07:58:04.621304Z","shell.execute_reply":"2024-12-08T07:58:31.903615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction[:3]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:31.906227Z","iopub.execute_input":"2024-12-08T07:58:31.907008Z","iopub.status.idle":"2024-12-08T07:58:31.915637Z","shell.execute_reply.started":"2024-12-08T07:58:31.906911Z","shell.execute_reply":"2024-12-08T07:58:31.914348Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction = exponential_transform(prediction)\nprediction[:3]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:31.917071Z","iopub.execute_input":"2024-12-08T07:58:31.917541Z","iopub.status.idle":"2024-12-08T07:58:31.951108Z","shell.execute_reply.started":"2024-12-08T07:58:31.917498Z","shell.execute_reply":"2024-12-08T07:58:31.94976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:31.952596Z","iopub.execute_input":"2024-12-08T07:58:31.952974Z","iopub.status.idle":"2024-12-08T07:58:31.962202Z","shell.execute_reply.started":"2024-12-08T07:58:31.952924Z","shell.execute_reply":"2024-12-08T07:58:31.96094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/playground-series-s4e12/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:31.963463Z","iopub.execute_input":"2024-12-08T07:58:31.963957Z","iopub.status.idle":"2024-12-08T07:58:32.136882Z","shell.execute_reply.started":"2024-12-08T07:58:31.963908Z","shell.execute_reply":"2024-12-08T07:58:32.135788Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:32.138203Z","iopub.execute_input":"2024-12-08T07:58:32.138581Z","iopub.status.idle":"2024-12-08T07:58:32.14948Z","shell.execute_reply.started":"2024-12-08T07:58:32.138546Z","shell.execute_reply":"2024-12-08T07:58:32.148296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:32.150775Z","iopub.execute_input":"2024-12-08T07:58:32.151132Z","iopub.status.idle":"2024-12-08T07:58:32.165913Z","shell.execute_reply.started":"2024-12-08T07:58:32.151097Z","shell.execute_reply":"2024-12-08T07:58:32.164808Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample['Premium Amount'] = prediction","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:32.167302Z","iopub.execute_input":"2024-12-08T07:58:32.167632Z","iopub.status.idle":"2024-12-08T07:58:32.181424Z","shell.execute_reply.started":"2024-12-08T07:58:32.167601Z","shell.execute_reply":"2024-12-08T07:58:32.180479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:32.182941Z","iopub.execute_input":"2024-12-08T07:58:32.183403Z","iopub.status.idle":"2024-12-08T07:58:32.198837Z","shell.execute_reply.started":"2024-12-08T07:58:32.183356Z","shell.execute_reply":"2024-12-08T07:58:32.197839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T07:58:32.200207Z","iopub.execute_input":"2024-12-08T07:58:32.20058Z","iopub.status.idle":"2024-12-08T07:58:33.851927Z","shell.execute_reply.started":"2024-12-08T07:58:32.200546Z","shell.execute_reply":"2024-12-08T07:58:33.851039Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Scrap\n\n## All the old code that isn't really needed but I'd like to keep in case I want to refer to it sometimes","metadata":{}},{"cell_type":"markdown","source":"## Adding a bunch of crazy combinations to features.\n\n\nRatios of two float features. Factors of two float features, etc. Didn't help","metadata":{}},{"cell_type":"markdown","source":"for model_key in model_dict:\n    pass\n\nglobal transformations    \n\ntransformations = {}\nstart_time = time.time()\n\nprint(model_key + ' started')\ntransformations['model'] = model_key\n\nX = data.drop('Premium Amount', axis = 1).copy()\ny = data['Premium Amount'].copy()\n\nX = all_transformations(X)\ny = log_transform(y)\n\nX_cols = float_cols + ['v2'+i for i in float_cols] + obj_col_names\nlen(X_cols)\n\nX_cols = float_cols + obj_col_names\nlen(X_cols)\n\nX.shape\n\nX_df = pd.DataFrame(X, columns=X_cols)\nX_df.head()\n\nmin(1,2)\n\nX_df[float_cols] = X_df[float_cols].fillna(.5)\n#X_df[float_cols].apply(lambda x: min(max(x,.01),.99) )\n#X_df[float_cols].map(lambda x: max(x,.01) )\n\nX_df[float_cols] = X_df[float_cols].map(lambda x: min(max(x,.01),.99) )\n\nX_df['Age'].isnull().sum()\n\nimport warnings\n\nwarnings.simplefilter(action='ignore', category=pd.errors.PerformanceWarning)\n\nfor col1 in float_cols:\n    for col2 in float_cols:\n        if col1 == col2:\n            pass\n        else:\n            # X_df['Plus_' + col1 + col2] = X_df[col1] + X_df[col2]\n            # X_df['Div_' + col1 + col2] = X_df[col1] / X_df[col2]\n            # X_df['Mult_' + col1 + col2] = X_df[col1] * X_df[col2]\n            X_df['Mult1plus_' + col1 + col2] = (1 + X_df[col1]) * (1 + X_df[col2])\n            \n\nX = X_df.copy()\n\n#add_trans('feature_combinations_plus_div_mult_mult1plus',X)\n#add_trans('feature_combinations_div_mult1plus',X)\n#add_trans('feature_combinations_div',X)\nadd_trans('feature_combinations_mult1plus',X)\n\ncv_score = cross_val_score(model_dict[model_key], X, y.values.ravel(), scoring='neg_root_mean_squared_error', cv=cv, n_jobs=-1)\n\ntransformations['score'] = cv_score.mean()\ntransformations['std'] = cv_score.std()\ntransformations['time'] = '{} sec'.format(round((time.time() - start_time),0))\n\nmodel_counter += 1\ntransformations_new = pd.DataFrame.from_dict(transformations, orient = 'index', columns = [str(model_counter)])\n\nif 'transformations_log' in globals():\n    transformations_log = pd.concat([transformations_log, transformations_new], join = 'outer', axis = 1)\nelse:\n    transformations_log = transformations_new\n\nprint('\\n\\n')\nprint(model_key + ' done')\nprint('{} sec'.format(round((time.time() - start_time),0)))\nprint(cv_score)\nprint(cv_score.mean())\n\ntransformations_log        ","metadata":{"execution":{"iopub.status.busy":"2024-12-07T08:16:19.112077Z","iopub.execute_input":"2024-12-07T08:16:19.112625Z","iopub.status.idle":"2024-12-07T08:16:19.118411Z","shell.execute_reply.started":"2024-12-07T08:16:19.11258Z","shell.execute_reply":"2024-12-07T08:16:19.117113Z"}},"attachments":{}},{"cell_type":"markdown","source":"## Misc","metadata":{}},{"cell_type":"markdown","source":"global transformations\ntransformations = {}\nstart_time = time.time()\n\nfor model_key\ntransformations","metadata":{"execution":{"iopub.status.busy":"2024-12-05T19:55:57.382942Z","iopub.execute_input":"2024-12-05T19:55:57.383358Z","iopub.status.idle":"2024-12-05T19:55:57.392127Z","shell.execute_reply.started":"2024-12-05T19:55:57.383321Z","shell.execute_reply":"2024-12-05T19:55:57.390859Z"}}},{"cell_type":"markdown","source":"run_cv_scores = 0\n\nif run_cv_scores:\n    cv_score = cross_val_score(catboost, X, y.values.ravel(), scoring='neg_root_mean_squared_error', cv=cv, n_jobs=-1)\n    print(cv_score)\n    print(cv_score.mean())","metadata":{"execution":{"iopub.status.busy":"2024-12-05T19:30:14.427361Z","iopub.execute_input":"2024-12-05T19:30:14.427848Z","iopub.status.idle":"2024-12-05T19:30:14.440167Z","shell.execute_reply.started":"2024-12-05T19:30:14.427758Z","shell.execute_reply":"2024-12-05T19:30:14.438779Z"}}}]}