{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","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"}],"dockerImageVersionId":30822,"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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:45.348555Z","iopub.execute_input":"2025-10-10T21:24:45.348865Z","iopub.status.idle":"2025-10-10T21:24:45.355615Z","shell.execute_reply.started":"2025-10-10T21:24:45.348841Z","shell.execute_reply":"2025-10-10T21:24:45.354502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')\n\n#original_dataset_insurance = pd.read_csv('Insurance Premium Prediction Dataset.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:45.356786Z","iopub.execute_input":"2025-10-10T21:24:45.357077Z","iopub.status.idle":"2025-10-10T21:24:51.697842Z","shell.execute_reply.started":"2025-10-10T21:24:45.357049Z","shell.execute_reply":"2025-10-10T21:24:51.696735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.decomposition import PCA\nfrom scipy import stats as st","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.700164Z","iopub.execute_input":"2025-10-10T21:24:51.700504Z","iopub.status.idle":"2025-10-10T21:24:51.704643Z","shell.execute_reply.started":"2025-10-10T21:24:51.700483Z","shell.execute_reply":"2025-10-10T21:24:51.703587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.706527Z","iopub.execute_input":"2025-10-10T21:24:51.706806Z","iopub.status.idle":"2025-10-10T21:24:51.723326Z","shell.execute_reply.started":"2025-10-10T21:24:51.706786Z","shell.execute_reply":"2025-10-10T21:24:51.722411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.724208Z","iopub.execute_input":"2025-10-10T21:24:51.724461Z","iopub.status.idle":"2025-10-10T21:24:51.739823Z","shell.execute_reply.started":"2025-10-10T21:24:51.724441Z","shell.execute_reply":"2025-10-10T21:24:51.738732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.741174Z","iopub.execute_input":"2025-10-10T21:24:51.741486Z","iopub.status.idle":"2025-10-10T21:24:51.760069Z","shell.execute_reply.started":"2025-10-10T21:24:51.741437Z","shell.execute_reply":"2025-10-10T21:24:51.75903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import SGDRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.760929Z","iopub.execute_input":"2025-10-10T21:24:51.761154Z","iopub.status.idle":"2025-10-10T21:24:51.780048Z","shell.execute_reply.started":"2025-10-10T21:24:51.761135Z","shell.execute_reply":"2025-10-10T21:24:51.779067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from catboost import CatBoostRegressor\nfrom sklearn.model_selection import KFold","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.781172Z","iopub.execute_input":"2025-10-10T21:24:51.781573Z","iopub.status.idle":"2025-10-10T21:24:51.797519Z","shell.execute_reply.started":"2025-10-10T21:24:51.781537Z","shell.execute_reply":"2025-10-10T21:24:51.796446Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.800271Z","iopub.execute_input":"2025-10-10T21:24:51.800576Z","iopub.status.idle":"2025-10-10T21:24:51.814047Z","shell.execute_reply.started":"2025-10-10T21:24:51.800548Z","shell.execute_reply":"2025-10-10T21:24:51.813096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.815317Z","iopub.execute_input":"2025-10-10T21:24:51.815618Z","iopub.status.idle":"2025-10-10T21:24:51.831719Z","shell.execute_reply.started":"2025-10-10T21:24:51.815595Z","shell.execute_reply":"2025-10-10T21:24:51.830632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Basic correlogram\n\"\"\"\nsns.pairplot(train)\nplt.show()\n\"\"\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.832744Z","iopub.execute_input":"2025-10-10T21:24:51.833149Z","iopub.status.idle":"2025-10-10T21:24:51.848649Z","shell.execute_reply.started":"2025-10-10T21:24:51.833112Z","shell.execute_reply":"2025-10-10T21:24:51.847458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#imp_mean = SimpleImputer(missing_values=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.849657Z","iopub.execute_input":"2025-10-10T21:24:51.850017Z","iopub.status.idle":"2025-10-10T21:24:51.864636Z","shell.execute_reply.started":"2025-10-10T21:24:51.849981Z","shell.execute_reply":"2025-10-10T21:24:51.863431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Substituir valores nulos e vazios por 0\ntrain = train.replace(r'^\\s*$', np.nan, regex=True)  # Substituir strings vazias por NaN\ntrain.fillna(0, inplace=True)  # Preencher NaN com -999","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:24:51.865588Z","iopub.execute_input":"2025-10-10T21:24:51.865982Z","iopub.status.idle":"2025-10-10T21:25:00.052823Z","shell.execute_reply.started":"2025-10-10T21:24:51.865952Z","shell.execute_reply":"2025-10-10T21:25:00.051748Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Substituir valores nulos e vazios por 0\ntest = test.replace(r'^\\s*$', np.nan, regex=True)  # Substituir strings vazias por NaN\ntest.fillna(0, inplace=True)  # Preencher NaN com -999","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:00.053727Z","iopub.execute_input":"2025-10-10T21:25:00.054001Z","iopub.status.idle":"2025-10-10T21:25:05.503594Z","shell.execute_reply.started":"2025-10-10T21:25:00.05398Z","shell.execute_reply":"2025-10-10T21:25:05.50265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.504785Z","iopub.execute_input":"2025-10-10T21:25:05.505196Z","iopub.status.idle":"2025-10-10T21:25:05.509095Z","shell.execute_reply.started":"2025-10-10T21:25:05.505158Z","shell.execute_reply":"2025-10-10T21:25:05.508022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef RMSLE(true,pred):\n    true_log = np.log1p(true)\n    pred_log = np.log1p(pred)\n    m = np.sqrt(np.mean( (true_log-pred_log)**2.0 ))\n    return m\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.510128Z","iopub.execute_input":"2025-10-10T21:25:05.510709Z","iopub.status.idle":"2025-10-10T21:25:05.527961Z","shell.execute_reply.started":"2025-10-10T21:25:05.510677Z","shell.execute_reply":"2025-10-10T21:25:05.526986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\npred = np.exp( np.mean( np.log1p(train[\"Premium Amount\"]) ) )-1\nm = RMSLE(train[\"Premium Amount\"].values, pred)\nprint(f\"Exponented Mean Log 1p produces CV RMSLE = {m}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.529237Z","iopub.execute_input":"2025-10-10T21:25:05.529601Z","iopub.status.idle":"2025-10-10T21:25:05.603865Z","shell.execute_reply.started":"2025-10-10T21:25:05.529563Z","shell.execute_reply":"2025-10-10T21:25:05.602697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"Premium Amount\"][~train[\"Premium Amount\"].isna()].max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.605269Z","iopub.execute_input":"2025-10-10T21:25:05.605706Z","iopub.status.idle":"2025-10-10T21:25:05.625354Z","shell.execute_reply.started":"2025-10-10T21:25:05.605666Z","shell.execute_reply":"2025-10-10T21:25:05.624131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"Premium Amount\"][~train[\"Premium Amount\"].isna()].min()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.626335Z","iopub.execute_input":"2025-10-10T21:25:05.626751Z","iopub.status.idle":"2025-10-10T21:25:05.643078Z","shell.execute_reply.started":"2025-10-10T21:25:05.626722Z","shell.execute_reply":"2025-10-10T21:25:05.641931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[:100000]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.64418Z","iopub.execute_input":"2025-10-10T21:25:05.644722Z","iopub.status.idle":"2025-10-10T21:25:05.714331Z","shell.execute_reply.started":"2025-10-10T21:25:05.644683Z","shell.execute_reply":"2025-10-10T21:25:05.713105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\"\"\"\ntrain1 = train\ntrain = pd.concat([train,test])\n\"\"\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.715442Z","iopub.execute_input":"2025-10-10T21:25:05.71572Z","iopub.status.idle":"2025-10-10T21:25:05.721649Z","shell.execute_reply.started":"2025-10-10T21:25:05.7157Z","shell.execute_reply":"2025-10-10T21:25:05.720587Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#train.drop(['Policy Start Date'], axis=1)\n#test.drop(['Policy Start Date'], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.72281Z","iopub.execute_input":"2025-10-10T21:25:05.723253Z","iopub.status.idle":"2025-10-10T21:25:05.741788Z","shell.execute_reply.started":"2025-10-10T21:25:05.723212Z","shell.execute_reply":"2025-10-10T21:25:05.740625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Import date\nfrom datetime import date\n\ndef extract_day_month_year(dataframe, column):\n    dataframe[column] = pd.to_datetime(dataframe[column])\n    dataframe[column+'_day'] = dataframe[column].dt.day\n    dataframe[column+'_month'] = dataframe[column].dt.month\n    dataframe[column+'_year'] = dataframe[column].dt.year\n    dataframe = dataframe.drop(columns=[column])\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.742803Z","iopub.execute_input":"2025-10-10T21:25:05.743234Z","iopub.status.idle":"2025-10-10T21:25:05.765686Z","shell.execute_reply.started":"2025-10-10T21:25:05.743201Z","shell.execute_reply":"2025-10-10T21:25:05.764346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"extract_day_month_year(train, 'Policy Start Date')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:05.766932Z","iopub.execute_input":"2025-10-10T21:25:05.767389Z","iopub.status.idle":"2025-10-10T21:25:06.462925Z","shell.execute_reply.started":"2025-10-10T21:25:05.767364Z","shell.execute_reply":"2025-10-10T21:25:06.461801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"extract_day_month_year(test, 'Policy Start Date')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:06.463996Z","iopub.execute_input":"2025-10-10T21:25:06.464356Z","iopub.status.idle":"2025-10-10T21:25:06.910038Z","shell.execute_reply.started":"2025-10-10T21:25:06.464328Z","shell.execute_reply":"2025-10-10T21:25:06.908928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:06.911206Z","iopub.execute_input":"2025-10-10T21:25:06.911614Z","iopub.status.idle":"2025-10-10T21:25:07.528885Z","shell.execute_reply.started":"2025-10-10T21:25:06.91159Z","shell.execute_reply":"2025-10-10T21:25:07.527214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:07.536118Z","iopub.execute_input":"2025-10-10T21:25:07.53649Z","iopub.status.idle":"2025-10-10T21:25:07.545061Z","shell.execute_reply.started":"2025-10-10T21:25:07.536466Z","shell.execute_reply":"2025-10-10T21:25:07.543654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.drop(columns=['Policy Start Date'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:07.547651Z","iopub.execute_input":"2025-10-10T21:25:07.547978Z","iopub.status.idle":"2025-10-10T21:25:07.674603Z","shell.execute_reply.started":"2025-10-10T21:25:07.547951Z","shell.execute_reply":"2025-10-10T21:25:07.673648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Identificar colunas categóricas automaticamente (opcional)\ncategorical_columns = train.select_dtypes(include=['object', 'category']).columns\n\n# Aplicar get_dummies nas colunas categóricas\ntrain_encoded = pd.get_dummies(train, columns=categorical_columns, drop_first=False)  # drop_first=True para evitar colinearidade","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:07.675812Z","iopub.execute_input":"2025-10-10T21:25:07.676128Z","iopub.status.idle":"2025-10-10T21:25:09.159697Z","shell.execute_reply.started":"2025-10-10T21:25:07.676103Z","shell.execute_reply":"2025-10-10T21:25:09.158743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_encoded","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:09.160792Z","iopub.execute_input":"2025-10-10T21:25:09.16107Z","iopub.status.idle":"2025-10-10T21:25:09.342397Z","shell.execute_reply.started":"2025-10-10T21:25:09.16105Z","shell.execute_reply":"2025-10-10T21:25:09.341304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Identificar colunas categóricas automaticamente (opcional)\ncategorical_columns = test.select_dtypes(include=['object', 'category']).columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:09.343256Z","iopub.execute_input":"2025-10-10T21:25:09.343506Z","iopub.status.idle":"2025-10-10T21:25:09.644704Z","shell.execute_reply.started":"2025-10-10T21:25:09.343486Z","shell.execute_reply":"2025-10-10T21:25:09.64366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:09.645661Z","iopub.execute_input":"2025-10-10T21:25:09.646009Z","iopub.status.idle":"2025-10-10T21:25:09.652554Z","shell.execute_reply.started":"2025-10-10T21:25:09.645977Z","shell.execute_reply":"2025-10-10T21:25:09.651421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:09.653624Z","iopub.execute_input":"2025-10-10T21:25:09.653968Z","iopub.status.idle":"2025-10-10T21:25:10.100856Z","shell.execute_reply.started":"2025-10-10T21:25:09.653936Z","shell.execute_reply":"2025-10-10T21:25:10.099853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded = pd.DataFrame()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:10.101613Z","iopub.execute_input":"2025-10-10T21:25:10.101855Z","iopub.status.idle":"2025-10-10T21:25:10.106487Z","shell.execute_reply.started":"2025-10-10T21:25:10.101837Z","shell.execute_reply":"2025-10-10T21:25:10.105372Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Aplicar get_dummies nas colunas categóricas\ntest_encoded = pd.concat([test_encoded, pd.get_dummies(test, columns=['Gender', 'Marital Status', 'Education Level'], drop_first=False) ])  # drop_first=True para evitar colinearidade","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:10.107616Z","iopub.execute_input":"2025-10-10T21:25:10.108014Z","iopub.status.idle":"2025-10-10T21:25:10.656921Z","shell.execute_reply.started":"2025-10-10T21:25:10.107984Z","shell.execute_reply":"2025-10-10T21:25:10.656024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:10.657806Z","iopub.execute_input":"2025-10-10T21:25:10.658147Z","iopub.status.idle":"2025-10-10T21:25:10.825502Z","shell.execute_reply.started":"2025-10-10T21:25:10.658124Z","shell.execute_reply":"2025-10-10T21:25:10.823742Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Aplicar get_dummies nas colunas categóricas\ntest_encoded = pd.concat([test_encoded, pd.get_dummies(test, columns=['Occupation', 'Location','Policy Type'], drop_first=False)])  # drop_first=True para evitar colinearidade","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:10.826491Z","iopub.execute_input":"2025-10-10T21:25:10.826785Z","iopub.status.idle":"2025-10-10T21:25:12.532053Z","shell.execute_reply.started":"2025-10-10T21:25:10.826764Z","shell.execute_reply":"2025-10-10T21:25:12.530928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:12.533515Z","iopub.execute_input":"2025-10-10T21:25:12.533804Z","iopub.status.idle":"2025-10-10T21:25:15.331019Z","shell.execute_reply.started":"2025-10-10T21:25:12.53378Z","shell.execute_reply":"2025-10-10T21:25:15.329784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Aplicar get_dummies nas colunas categóricas\n#test_encoded = pd.get_dummies(test, columns=['Policy Start Date'], drop_first=False)  # drop_first=True para evitar colinearidade","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:15.33176Z","iopub.execute_input":"2025-10-10T21:25:15.332166Z","iopub.status.idle":"2025-10-10T21:25:15.33594Z","shell.execute_reply.started":"2025-10-10T21:25:15.33213Z","shell.execute_reply":"2025-10-10T21:25:15.334745Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Aplicar get_dummies nas colunas categóricas\ntest_encoded = pd.concat([test_encoded, pd.get_dummies(test, columns=['Customer Feedback','Smoking Status'], drop_first=False)]) # drop_first=True para evitar colinearidade","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:15.336854Z","iopub.execute_input":"2025-10-10T21:25:15.33712Z","iopub.status.idle":"2025-10-10T21:25:17.472667Z","shell.execute_reply.started":"2025-10-10T21:25:15.337101Z","shell.execute_reply":"2025-10-10T21:25:17.471367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Aplicar get_dummies nas colunas categóricas\ntest_encoded = pd.concat([test_encoded, pd.get_dummies(test, columns=['Exercise Frequency', 'Property Type'], drop_first=False) ])  # drop_first=True para evitar colinearidade","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:17.473768Z","iopub.execute_input":"2025-10-10T21:25:17.474088Z","iopub.status.idle":"2025-10-10T21:25:20.998955Z","shell.execute_reply.started":"2025-10-10T21:25:17.474064Z","shell.execute_reply":"2025-10-10T21:25:20.998009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Verificar e adicionar colunas ausentes\nfor col in train_encoded.columns:\n    if col not in test_encoded.columns:\n        test_encoded[col] = 0  # Adicionar coluna ausente preenchida com 0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:21.000089Z","iopub.execute_input":"2025-10-10T21:25:21.000408Z","iopub.status.idle":"2025-10-10T21:25:21.008015Z","shell.execute_reply.started":"2025-10-10T21:25:21.000383Z","shell.execute_reply":"2025-10-10T21:25:21.006927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#separando dados do alvo\nX = train_encoded.drop('Premium Amount', axis=1)\ny = train_encoded['Premium Amount']\n\ny_log = np.log1p(y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:21.00937Z","iopub.execute_input":"2025-10-10T21:25:21.009932Z","iopub.status.idle":"2025-10-10T21:25:21.102401Z","shell.execute_reply.started":"2025-10-10T21:25:21.009869Z","shell.execute_reply":"2025-10-10T21:25:21.101171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def rmsle1(y_true, y_pred):\n    return np.sqrt(mean_squared_log_error(y_true, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:21.103548Z","iopub.execute_input":"2025-10-10T21:25:21.103983Z","iopub.status.idle":"2025-10-10T21:25:21.109837Z","shell.execute_reply.started":"2025-10-10T21:25:21.103942Z","shell.execute_reply":"2025-10-10T21:25:21.108226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train_model():\n    kf = KFold(n_splits=5, shuffle=True, random_state=42)\n    oof = np.zeros(len(X))\n    models = []\n\n    for fold, (train_idx, valid_idx) in enumerate(kf.split(X)):\n        print(f\"Fold {fold + 1}\")\n        X_train, X_valid = X.iloc[train_idx], X.iloc[valid_idx]\n        y_train, y_valid = y_log.iloc[train_idx], y_log.iloc[valid_idx]\n\n        model = CatBoostRegressor(\n            iterations=3000,\n            learning_rate=0.05,\n            depth=6,\n            eval_metric=\"RMSE\",\n            random_seed=42,\n            verbose=200,\n            #task_type='GPU',\n            l2_leaf_reg =  0.7,\n        )\n        \n        model.fit(X_train,\n                  y_train,\n                  eval_set=(X_valid, y_valid), \n                  early_stopping_rounds=300,\n                  # cat_features=cat_cols,\n                 )\n        models.append(model)\n        oof[valid_idx] = np.maximum(0, model.predict(X_valid))\n        fold_rmsle = rmsle1(np.expm1(y_valid), np.expm1(oof[valid_idx]))\n        print(f\"Fold {fold + 1} RMSLE: {fold_rmsle}\")\n        \n    return models, oof","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:21.110887Z","iopub.execute_input":"2025-10-10T21:25:21.111384Z","iopub.status.idle":"2025-10-10T21:25:21.138252Z","shell.execute_reply.started":"2025-10-10T21:25:21.111349Z","shell.execute_reply":"2025-10-10T21:25:21.136638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"models,oof = train_model()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:25:21.139127Z","iopub.execute_input":"2025-10-10T21:25:21.139382Z","iopub.status.idle":"2025-10-10T21:40:17.769785Z","shell.execute_reply.started":"2025-10-10T21:25:21.139362Z","shell.execute_reply":"2025-10-10T21:40:17.76886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(rmsle1(y, np.expm1(oof)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:17.771617Z","iopub.execute_input":"2025-10-10T21:40:17.771978Z","iopub.status.idle":"2025-10-10T21:40:17.84866Z","shell.execute_reply.started":"2025-10-10T21:40:17.771948Z","shell.execute_reply":"2025-10-10T21:40:17.847736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean_squared_log_error(y, oof)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:17.849665Z","iopub.execute_input":"2025-10-10T21:40:17.849913Z","iopub.status.idle":"2025-10-10T21:40:17.908847Z","shell.execute_reply.started":"2025-10-10T21:40:17.849879Z","shell.execute_reply":"2025-10-10T21:40:17.90756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions = np.zeros(len(test_encoded))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:17.909981Z","iopub.execute_input":"2025-10-10T21:40:17.910621Z","iopub.status.idle":"2025-10-10T21:40:17.919Z","shell.execute_reply.started":"2025-10-10T21:40:17.910588Z","shell.execute_reply":"2025-10-10T21:40:17.917319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:17.921883Z","iopub.execute_input":"2025-10-10T21:40:17.922796Z","iopub.status.idle":"2025-10-10T21:40:17.94408Z","shell.execute_reply.started":"2025-10-10T21:40:17.922752Z","shell.execute_reply":"2025-10-10T21:40:17.942836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded['id']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:17.945448Z","iopub.execute_input":"2025-10-10T21:40:17.945801Z","iopub.status.idle":"2025-10-10T21:40:17.97225Z","shell.execute_reply.started":"2025-10-10T21:40:17.945777Z","shell.execute_reply":"2025-10-10T21:40:17.971241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:17.973526Z","iopub.execute_input":"2025-10-10T21:40:17.973791Z","iopub.status.idle":"2025-10-10T21:40:17.991748Z","shell.execute_reply.started":"2025-10-10T21:40:17.97377Z","shell.execute_reply":"2025-10-10T21:40:17.990606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded.dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:17.99298Z","iopub.execute_input":"2025-10-10T21:40:17.993312Z","iopub.status.idle":"2025-10-10T21:40:18.01685Z","shell.execute_reply.started":"2025-10-10T21:40:17.99328Z","shell.execute_reply":"2025-10-10T21:40:18.01579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#test_encoded['Gender_Female']\n\n# Selecionando colunas de tipos específicos\n#selected_columns = train_encoded.select_dtypes(include=['int16', 'float64', 'int64', 'int32']).columns.tolist()\nselected_columns = train_encoded.columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:18.01835Z","iopub.execute_input":"2025-10-10T21:40:18.01892Z","iopub.status.idle":"2025-10-10T21:40:18.037822Z","shell.execute_reply.started":"2025-10-10T21:40:18.018854Z","shell.execute_reply":"2025-10-10T21:40:18.036692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:18.039209Z","iopub.execute_input":"2025-10-10T21:40:18.03969Z","iopub.status.idle":"2025-10-10T21:40:18.061502Z","shell.execute_reply.started":"2025-10-10T21:40:18.039653Z","shell.execute_reply":"2025-10-10T21:40:18.060563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_columns.remove('Premium Amount')\nselected_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:18.062683Z","iopub.execute_input":"2025-10-10T21:40:18.063115Z","iopub.status.idle":"2025-10-10T21:40:18.085458Z","shell.execute_reply.started":"2025-10-10T21:40:18.063079Z","shell.execute_reply":"2025-10-10T21:40:18.083878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_encoded[selected_columns]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:18.086776Z","iopub.execute_input":"2025-10-10T21:40:18.08718Z","iopub.status.idle":"2025-10-10T21:40:18.404437Z","shell.execute_reply.started":"2025-10-10T21:40:18.087151Z","shell.execute_reply":"2025-10-10T21:40:18.403539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:18.405324Z","iopub.execute_input":"2025-10-10T21:40:18.405573Z","iopub.status.idle":"2025-10-10T21:40:23.4717Z","shell.execute_reply.started":"2025-10-10T21:40:18.405553Z","shell.execute_reply":"2025-10-10T21:40:23.470531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(oof)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:23.472884Z","iopub.execute_input":"2025-10-10T21:40:23.473228Z","iopub.status.idle":"2025-10-10T21:40:23.480097Z","shell.execute_reply.started":"2025-10-10T21:40:23.473185Z","shell.execute_reply":"2025-10-10T21:40:23.478781Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded[selected_columns]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:23.481085Z","iopub.execute_input":"2025-10-10T21:40:23.481454Z","iopub.status.idle":"2025-10-10T21:40:29.263049Z","shell.execute_reply.started":"2025-10-10T21:40:23.481412Z","shell.execute_reply":"2025-10-10T21:40:29.261996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#selected_columns.append('Gender_Female')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:29.263922Z","iopub.execute_input":"2025-10-10T21:40:29.264162Z","iopub.status.idle":"2025-10-10T21:40:29.26945Z","shell.execute_reply.started":"2025-10-10T21:40:29.264142Z","shell.execute_reply":"2025-10-10T21:40:29.268004Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:29.270501Z","iopub.execute_input":"2025-10-10T21:40:29.270957Z","iopub.status.idle":"2025-10-10T21:40:29.30053Z","shell.execute_reply.started":"2025-10-10T21:40:29.270919Z","shell.execute_reply":"2025-10-10T21:40:29.299287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#test_encoded['Gender_Female'] = 0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:29.301437Z","iopub.execute_input":"2025-10-10T21:40:29.30177Z","iopub.status.idle":"2025-10-10T21:40:29.323108Z","shell.execute_reply.started":"2025-10-10T21:40:29.301742Z","shell.execute_reply":"2025-10-10T21:40:29.321756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#test_encoded['Gender_Female']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:29.324431Z","iopub.execute_input":"2025-10-10T21:40:29.32522Z","iopub.status.idle":"2025-10-10T21:40:29.349945Z","shell.execute_reply.started":"2025-10-10T21:40:29.325132Z","shell.execute_reply":"2025-10-10T21:40:29.348735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:29.351077Z","iopub.execute_input":"2025-10-10T21:40:29.351343Z","iopub.status.idle":"2025-10-10T21:40:34.244114Z","shell.execute_reply.started":"2025-10-10T21:40:29.351322Z","shell.execute_reply":"2025-10-10T21:40:34.24314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_encoded[selected_columns]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:34.245051Z","iopub.execute_input":"2025-10-10T21:40:34.245329Z","iopub.status.idle":"2025-10-10T21:40:40.047601Z","shell.execute_reply.started":"2025-10-10T21:40:34.245309Z","shell.execute_reply":"2025-10-10T21:40:40.046649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:40.04843Z","iopub.execute_input":"2025-10-10T21:40:40.048691Z","iopub.status.idle":"2025-10-10T21:40:40.055317Z","shell.execute_reply.started":"2025-10-10T21:40:40.048671Z","shell.execute_reply":"2025-10-10T21:40:40.05442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfor model in models:\n    test_predictions += np.maximum(0, np.expm1(model.predict(test_encoded[selected_columns]))) / len(models)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:40:40.056318Z","iopub.execute_input":"2025-10-10T21:40:40.056611Z","iopub.status.idle":"2025-10-10T21:41:43.53968Z","shell.execute_reply.started":"2025-10-10T21:40:40.056591Z","shell.execute_reply":"2025-10-10T21:41:43.537986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds = model.predict(test_encoded[selected_columns])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:41:43.541154Z","iopub.execute_input":"2025-10-10T21:41:43.541506Z","iopub.status.idle":"2025-10-10T21:41:56.27073Z","shell.execute_reply.started":"2025-10-10T21:41:43.541483Z","shell.execute_reply":"2025-10-10T21:41:56.269795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:41:56.272088Z","iopub.execute_input":"2025-10-10T21:41:56.272501Z","iopub.status.idle":"2025-10-10T21:41:56.280292Z","shell.execute_reply.started":"2025-10-10T21:41:56.272471Z","shell.execute_reply":"2025-10-10T21:41:56.2787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:41:56.281224Z","iopub.execute_input":"2025-10-10T21:41:56.281591Z","iopub.status.idle":"2025-10-10T21:41:56.312287Z","shell.execute_reply.started":"2025-10-10T21:41:56.281563Z","shell.execute_reply":"2025-10-10T21:41:56.311015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample = pd.DataFrame()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:41:56.313945Z","iopub.execute_input":"2025-10-10T21:41:56.314432Z","iopub.status.idle":"2025-10-10T21:41:56.338693Z","shell.execute_reply.started":"2025-10-10T21:41:56.314378Z","shell.execute_reply":"2025-10-10T21:41:56.337554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample['id'] = test_encoded['id']\nsample['Premium Amount'] = test_predictions\nsample.to_csv('6_submission_CatBoostRegressor.csv', index = False)\nsample.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:41:56.339917Z","iopub.execute_input":"2025-10-10T21:41:56.340313Z","iopub.status.idle":"2025-10-10T21:42:01.581798Z","shell.execute_reply.started":"2025-10-10T21:41:56.340269Z","shell.execute_reply":"2025-10-10T21:42:01.580834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Submit notebooks to the challenge. Final\n\n\nsubmission_final = pd.DataFrame({\n\n        \"id\":test_encoded['id'],\n\n        \"Premium Amount\":test_predictions\n\n    })\n\nsubmission_final.to_csv('novo_arquivo_submission.csv', index=False)\n\n\nprint(\" Arquivo submission v2.csv pronto \")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-10T21:42:01.582786Z","iopub.execute_input":"2025-10-10T21:42:01.583052Z","iopub.status.idle":"2025-10-10T21:42:06.514242Z","shell.execute_reply.started":"2025-10-10T21:42:01.583032Z","shell.execute_reply":"2025-10-10T21:42:06.51331Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## print('end...')","metadata":{}}]}