{"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"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import LabelEncoder, StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error, r2_score\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Input, Dense\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, BatchNormalization","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:32:58.956541Z","iopub.execute_input":"2024-12-08T13:32:58.956975Z","iopub.status.idle":"2024-12-08T13:33:12.711601Z","shell.execute_reply.started":"2024-12-08T13:32:58.956936Z","shell.execute_reply":"2024-12-08T13:33:12.710746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/playground-series-s4e12/train.csv')\ntest = pd.read_csv('/kaggle/input/playground-series-s4e12/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:12.71319Z","iopub.execute_input":"2024-12-08T13:33:12.713793Z","iopub.status.idle":"2024-12-08T13:33:22.649535Z","shell.execute_reply.started":"2024-12-08T13:33:12.713749Z","shell.execute_reply":"2024-12-08T13:33:22.648483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def split_date(policy_start_date):\n  if ' ' in policy_start_date:\n    return policy_start_date.split(' ')[0]\n  else:\n    return None\n\ndf['Policy Start Date'] = df['Policy Start Date'].apply(split_date)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:22.650895Z","iopub.execute_input":"2024-12-08T13:33:22.651307Z","iopub.status.idle":"2024-12-08T13:33:23.152478Z","shell.execute_reply.started":"2024-12-08T13:33:22.651262Z","shell.execute_reply":"2024-12-08T13:33:23.151327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Policy Start Date'] = pd.to_datetime(df['Policy Start Date'], format='%Y-%m-%d')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:23.153932Z","iopub.execute_input":"2024-12-08T13:33:23.15436Z","iopub.status.idle":"2024-12-08T13:33:23.449957Z","shell.execute_reply.started":"2024-12-08T13:33:23.154317Z","shell.execute_reply":"2024-12-08T13:33:23.449044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.set_index('Policy Start Date', inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:23.452108Z","iopub.execute_input":"2024-12-08T13:33:23.452426Z","iopub.status.idle":"2024-12-08T13:33:23.459829Z","shell.execute_reply.started":"2024-12-08T13:33:23.452394Z","shell.execute_reply":"2024-12-08T13:33:23.458784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numeric_columns = df.select_dtypes(include=[np.number]).columns\ndf[numeric_columns] = df[numeric_columns].fillna(df[numeric_columns].median())\ncategorical_columns = df.select_dtypes(include=['object']).columns\ndf[categorical_columns] = df[categorical_columns].fillna(df[categorical_columns].mode().iloc[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:23.461125Z","iopub.execute_input":"2024-12-08T13:33:23.461419Z","iopub.status.idle":"2024-12-08T13:33:26.999359Z","shell.execute_reply.started":"2024-12-08T13:33:23.461388Z","shell.execute_reply":"2024-12-08T13:33:26.998505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"le = LabelEncoder()\nfor col in categorical_columns:\n    df[col] = le.fit_transform(df[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:27.000741Z","iopub.execute_input":"2024-12-08T13:33:27.001083Z","iopub.status.idle":"2024-12-08T13:33:29.270283Z","shell.execute_reply.started":"2024-12-08T13:33:27.001049Z","shell.execute_reply":"2024-12-08T13:33:29.269282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()\ndf[numeric_columns] = scaler.fit_transform(df[numeric_columns])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:29.271657Z","iopub.execute_input":"2024-12-08T13:33:29.271988Z","iopub.status.idle":"2024-12-08T13:33:29.591995Z","shell.execute_reply.started":"2024-12-08T13:33:29.271955Z","shell.execute_reply":"2024-12-08T13:33:29.59089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.drop(['Premium Amount', 'id'], axis=1)\ny = df['Premium Amount']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:29.593409Z","iopub.execute_input":"2024-12-08T13:33:29.593856Z","iopub.status.idle":"2024-12-08T13:33:29.710281Z","shell.execute_reply.started":"2024-12-08T13:33:29.59381Z","shell.execute_reply":"2024-12-08T13:33:29.709218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:29.711566Z","iopub.execute_input":"2024-12-08T13:33:29.711873Z","iopub.status.idle":"2024-12-08T13:33:30.20007Z","shell.execute_reply.started":"2024-12-08T13:33:29.711843Z","shell.execute_reply":"2024-12-08T13:33:30.19902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"input_dimension = X.shape[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:30.201328Z","iopub.execute_input":"2024-12-08T13:33:30.201673Z","iopub.status.idle":"2024-12-08T13:33:30.206307Z","shell.execute_reply.started":"2024-12-08T13:33:30.201641Z","shell.execute_reply":"2024-12-08T13:33:30.205205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Input(shape=(X_train.shape[1],)),\n    Dense(64, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.3),\n    Dense(32, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.2),\n    Dense(16, activation='relu'),\n    Dense(1)\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:30.20786Z","iopub.execute_input":"2024-12-08T13:33:30.208174Z","iopub.status.idle":"2024-12-08T13:33:30.322757Z","shell.execute_reply.started":"2024-12-08T13:33:30.208132Z","shell.execute_reply":"2024-12-08T13:33:30.32169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='mean_squared_error')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:30.324289Z","iopub.execute_input":"2024-12-08T13:33:30.324646Z","iopub.status.idle":"2024-12-08T13:33:30.339334Z","shell.execute_reply.started":"2024-12-08T13:33:30.324612Z","shell.execute_reply":"2024-12-08T13:33:30.338232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T13:33:30.343249Z","iopub.execute_input":"2024-12-08T13:33:30.343697Z","iopub.status.idle":"2024-12-08T14:42:10.482661Z","shell.execute_reply.started":"2024-12-08T13:33:30.343663Z","shell.execute_reply":"2024-12-08T14:42:10.481549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_loss = model.evaluate(X_test, y_test)\nprint(f\"Test Loss: {test_loss}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:42:10.484283Z","iopub.execute_input":"2024-12-08T14:42:10.484681Z","iopub.status.idle":"2024-12-08T14:42:17.428071Z","shell.execute_reply.started":"2024-12-08T14:42:10.484643Z","shell.execute_reply":"2024-12-08T14:42:17.427041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict_with_model(model, input):\n    predictions = model.predict(input)\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:04.798046Z","iopub.execute_input":"2024-12-08T14:43:04.799082Z","iopub.status.idle":"2024-12-08T14:43:04.804107Z","shell.execute_reply.started":"2024-12-08T14:43:04.799025Z","shell.execute_reply":"2024-12-08T14:43:04.803179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def split_date(policy_start_date):\n  if ' ' in policy_start_date:\n    return policy_start_date.split(' ')[0]\n  else:\n    return None\n\ntest['Policy Start Date'] = test['Policy Start Date'].apply(split_date)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:07.308116Z","iopub.execute_input":"2024-12-08T14:43:07.309314Z","iopub.status.idle":"2024-12-08T14:43:07.64516Z","shell.execute_reply.started":"2024-12-08T14:43:07.309266Z","shell.execute_reply":"2024-12-08T14:43:07.644106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['Policy Start Date'] = pd.to_datetime(test['Policy Start Date'], format='%Y-%m-%d')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:12.27142Z","iopub.execute_input":"2024-12-08T14:43:12.271861Z","iopub.status.idle":"2024-12-08T14:43:12.461839Z","shell.execute_reply.started":"2024-12-08T14:43:12.271815Z","shell.execute_reply":"2024-12-08T14:43:12.460862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.set_index('Policy Start Date', inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:15.112302Z","iopub.execute_input":"2024-12-08T14:43:15.113257Z","iopub.status.idle":"2024-12-08T14:43:15.118795Z","shell.execute_reply.started":"2024-12-08T14:43:15.113218Z","shell.execute_reply":"2024-12-08T14:43:15.117528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numeric_columns_test = test.select_dtypes(include=[np.number]).columns\ntest[numeric_columns_test] = test[numeric_columns_test].fillna(test[numeric_columns_test].median())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:17.717323Z","iopub.execute_input":"2024-12-08T14:43:17.718269Z","iopub.status.idle":"2024-12-08T14:43:18.086982Z","shell.execute_reply.started":"2024-12-08T14:43:17.718226Z","shell.execute_reply":"2024-12-08T14:43:18.085825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"categorical_columns_test = test.select_dtypes(include=['object']).columns\ntest[categorical_columns_test] = test[categorical_columns_test].fillna(test[categorical_columns_test].mode().iloc[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:29.082579Z","iopub.execute_input":"2024-12-08T14:43:29.083003Z","iopub.status.idle":"2024-12-08T14:43:31.027745Z","shell.execute_reply.started":"2024-12-08T14:43:29.082954Z","shell.execute_reply":"2024-12-08T14:43:31.026297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"le = LabelEncoder()\nfor col in categorical_columns_test:\n    test[col] = le.fit_transform(test[col])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:33.132392Z","iopub.execute_input":"2024-12-08T14:43:33.133062Z","iopub.status.idle":"2024-12-08T14:43:34.64198Z","shell.execute_reply.started":"2024-12-08T14:43:33.13302Z","shell.execute_reply":"2024-12-08T14:43:34.641048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()\ndf[numeric_columns_test] = scaler.fit_transform(df[numeric_columns_test])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:37.990618Z","iopub.execute_input":"2024-12-08T14:43:37.991006Z","iopub.status.idle":"2024-12-08T14:43:38.281039Z","shell.execute_reply.started":"2024-12-08T14:43:37.990968Z","shell.execute_reply":"2024-12-08T14:43:38.279737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = predict_with_model(model, test.drop('id', axis=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:43:44.206025Z","iopub.execute_input":"2024-12-08T14:43:44.20641Z","iopub.status.idle":"2024-12-08T14:44:22.029342Z","shell.execute_reply.started":"2024-12-08T14:43:44.206373Z","shell.execute_reply":"2024-12-08T14:44:22.028383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction = pd.DataFrame(predictions, columns=['Premium Amount'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T14:47:48.42282Z","iopub.execute_input":"2024-12-08T14:47:48.423221Z","iopub.status.idle":"2024-12-08T14:47:48.428825Z","shell.execute_reply.started":"2024-12-08T14:47:48.423183Z","shell.execute_reply":"2024-12-08T14:47:48.427508Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_df = pd.concat([test_1['id'], prediction], axis=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}