{"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":"gpu","dataSources":[{"sourceId":82611,"databundleVersionId":9553358,"sourceType":"competition"}],"dockerImageVersionId":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"SEED = 42\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-05T18:39:36.319674Z","iopub.execute_input":"2024-10-05T18:39:36.319978Z","iopub.status.idle":"2024-10-05T18:39:37.363918Z","shell.execute_reply.started":"2024-10-05T18:39:36.319941Z","shell.execute_reply":"2024-10-05T18:39:37.362880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/brist1d/train.csv',header = 0,index_col =0)\ntest = pd.read_csv('/kaggle/input/brist1d/test.csv',header = 0,index_col =0)\nsubmission = pd.read_csv('/kaggle/input/brist1d/sample_submission.csv',header = 0,index_col =0)\ntrain.shape, test.shape, submission.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:39:37.365900Z","iopub.execute_input":"2024-10-05T18:39:37.366447Z","iopub.status.idle":"2024-10-05T18:39:50.674502Z","shell.execute_reply.started":"2024-10-05T18:39:37.366402Z","shell.execute_reply":"2024-10-05T18:39:50.673570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:39:50.676021Z","iopub.execute_input":"2024-10-05T18:39:50.676624Z","iopub.status.idle":"2024-10-05T18:39:50.843066Z","shell.execute_reply.started":"2024-10-05T18:39:50.676588Z","shell.execute_reply":"2024-10-05T18:39:50.842111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.iloc[:,2:74]\ny = train['bg+1:00'].values\nZ = test.iloc[:,2:74]\n\nX.shape, y.shape, Z.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:39:50.845032Z","iopub.execute_input":"2024-10-05T18:39:50.845359Z","iopub.status.idle":"2024-10-05T18:39:50.883087Z","shell.execute_reply.started":"2024-10-05T18:39:50.845326Z","shell.execute_reply":"2024-10-05T18:39:50.882209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\n\nimputer = SimpleImputer(strategy='mean')\nX2 = imputer.fit_transform(X)\nZ2 = imputer.transform(Z.values)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:39:50.884288Z","iopub.execute_input":"2024-10-05T18:39:50.884608Z","iopub.status.idle":"2024-10-05T18:39:51.889572Z","shell.execute_reply.started":"2024-10-05T18:39:50.884576Z","shell.execute_reply":"2024-10-05T18:39:51.888602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import SimpleRNN, Dense, LSTM,Input, Dropout\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom sklearn.metrics import classification_report, confusion_matrix, accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:39:51.890995Z","iopub.execute_input":"2024-10-05T18:39:51.891584Z","iopub.status.idle":"2024-10-05T18:40:04.002427Z","shell.execute_reply.started":"2024-10-05T18:39:51.891538Z","shell.execute_reply":"2024-10-05T18:40:04.001483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_data = X2\ntime_steps = 72\nfeatures = 1\n\n#samples = original_data.shape[0] // time_steps \nsamples = train.shape[0] \nreshaped_data = original_data.reshape(samples, time_steps, -1)\n\nscaler = StandardScaler()\nreshaped_data = scaler.fit_transform(reshaped_data.reshape(-1, reshaped_data.shape[-1])).reshape(reshaped_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:40:04.003715Z","iopub.execute_input":"2024-10-05T18:40:04.004703Z","iopub.status.idle":"2024-10-05T18:40:04.231537Z","shell.execute_reply.started":"2024-10-05T18:40:04.004652Z","shell.execute_reply":"2024-10-05T18:40:04.230473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Keras (LSTM)","metadata":{}},{"cell_type":"code","source":"model_chkpt = ModelCheckpoint('best_mod.keras', save_best_only=True, monitor='val_loss')\nearly_stopping = EarlyStopping(monitor='val_loss', restore_best_weights=False, patience=3)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:40:04.232857Z","iopub.execute_input":"2024-10-05T18:40:04.233216Z","iopub.status.idle":"2024-10-05T18:40:04.240788Z","shell.execute_reply.started":"2024-10-05T18:40:04.233178Z","shell.execute_reply":"2024-10-05T18:40:04.239874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X3 = reshaped_data\ny3 = y \nX3_train, X3_test, y3_train, y3_test = train_test_split(X3, y3, test_size=0.2, random_state=42)\n\nmodel = Sequential()\nmodel.add(LSTM(5, activation='relu', input_shape=(time_steps, reshaped_data.shape[2]))) \nmodel.add(Dropout(0.2))\nmodel.add(Dense(1))  \n\nfrom tensorflow.keras.optimizers import Adam\noptimizer = Adam(learning_rate=0.001)\nmodel.compile(optimizer=optimizer, loss='mean_squared_error')\n\nhistory = model.fit(X3_train, y3_train, epochs=20, batch_size=32, validation_split=0.2,\n          shuffle=True,\n          callbacks=[model_chkpt ,early_stopping]\n         )","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:40:04.242046Z","iopub.execute_input":"2024-10-05T18:40:04.242423Z","iopub.status.idle":"2024-10-05T18:48:28.634633Z","shell.execute_reply.started":"2024-10-05T18:40:04.242383Z","shell.execute_reply":"2024-10-05T18:48:28.633869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X3_train.shape, X3_test.shape, y3_train.shape, y3_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:48:28.637704Z","iopub.execute_input":"2024-10-05T18:48:28.638072Z","iopub.status.idle":"2024-10-05T18:48:28.644063Z","shell.execute_reply.started":"2024-10-05T18:48:28.638040Z","shell.execute_reply":"2024-10-05T18:48:28.643208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize=(12, 3))\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:48:28.645292Z","iopub.execute_input":"2024-10-05T18:48:28.645929Z","iopub.status.idle":"2024-10-05T18:48:29.096862Z","shell.execute_reply.started":"2024-10-05T18:48:28.645877Z","shell.execute_reply":"2024-10-05T18:48:29.095983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('/kaggle/working/best_mod.keras')\n\nfrom sklearn.metrics import mean_squared_error\ny_pred = model.predict(X3_test)\nrmse = np.sqrt(mean_squared_error(y3_test, y_pred))\nrmse","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:49:30.497280Z","iopub.execute_input":"2024-10-05T18:49:30.497675Z","iopub.status.idle":"2024-10-05T18:49:32.677569Z","shell.execute_reply.started":"2024-10-05T18:49:30.497639Z","shell.execute_reply":"2024-10-05T18:49:32.676428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Keras(regressor)","metadata":{}},{"cell_type":"code","source":"model_chkpt2 = ModelCheckpoint('best_mod2.keras', save_best_only=True, monitor='val_loss')\nearly_stopping2 = EarlyStopping(monitor='val_loss', restore_best_weights=False, patience=3)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:58:25.500121Z","iopub.execute_input":"2024-10-05T18:58:25.500859Z","iopub.status.idle":"2024-10-05T18:58:25.505489Z","shell.execute_reply.started":"2024-10-05T18:58:25.500818Z","shell.execute_reply":"2024-10-05T18:58:25.504516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX4 = scaler.fit_transform(X2)\ny4 = y \nX4_train, X4_test, y4_train, y4_test = train_test_split(X4, y4, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:58:26.347739Z","iopub.execute_input":"2024-10-05T18:58:26.348089Z","iopub.status.idle":"2024-10-05T18:58:26.655940Z","shell.execute_reply.started":"2024-10-05T18:58:26.348057Z","shell.execute_reply":"2024-10-05T18:58:26.655097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(16, activation='relu', input_shape=(X4_train.shape[1],)))  \nmodel.add(Dense(4, activation='relu'))  \nmodel.add(Dense(1))  \n\noptimizer = Adam(learning_rate=0.001)\nmodel.compile(optimizer=optimizer, loss='mean_squared_error')","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:58:27.236389Z","iopub.execute_input":"2024-10-05T18:58:27.237177Z","iopub.status.idle":"2024-10-05T18:58:27.280955Z","shell.execute_reply.started":"2024-10-05T18:58:27.237124Z","shell.execute_reply":"2024-10-05T18:58:27.280039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X4_train, y4_train, epochs=20, batch_size=32, validation_split=0.2,\n          shuffle=True,\n          callbacks=[model_chkpt2 ,early_stopping2]\n         )","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:58:28.227411Z","iopub.execute_input":"2024-10-05T18:58:28.227809Z","iopub.status.idle":"2024-10-05T18:59:20.262481Z","shell.execute_reply.started":"2024-10-05T18:58:28.227771Z","shell.execute_reply":"2024-10-05T18:59:20.261636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2, figsize=(12, 3))\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:59:20.264210Z","iopub.execute_input":"2024-10-05T18:59:20.264529Z","iopub.status.idle":"2024-10-05T18:59:20.708039Z","shell.execute_reply.started":"2024-10-05T18:59:20.264494Z","shell.execute_reply":"2024-10-05T18:59:20.707103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('/kaggle/working/best_mod2.keras')\n\nfrom sklearn.metrics import mean_squared_error\ny_pred = model.predict(X4_test)\nrmse = np.sqrt(mean_squared_error(y4_test, y_pred))\nrmse","metadata":{"execution":{"iopub.status.busy":"2024-10-05T18:59:20.709362Z","iopub.execute_input":"2024-10-05T18:59:20.709731Z","iopub.status.idle":"2024-10-05T18:59:22.718058Z","shell.execute_reply.started":"2024-10-05T18:59:20.709695Z","shell.execute_reply":"2024-10-05T18:59:22.717054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"Z4 = scaler.transform(Z2)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T19:00:58.405050Z","iopub.execute_input":"2024-10-05T19:00:58.405949Z","iopub.status.idle":"2024-10-05T19:00:58.411215Z","shell.execute_reply.started":"2024-10-05T19:00:58.405908Z","shell.execute_reply":"2024-10-05T19:00:58.410296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(Z4)","metadata":{"execution":{"iopub.status.busy":"2024-10-05T19:01:06.276623Z","iopub.execute_input":"2024-10-05T19:01:06.277280Z","iopub.status.idle":"2024-10-05T19:01:06.616357Z","shell.execute_reply.started":"2024-10-05T19:01:06.277238Z","shell.execute_reply":"2024-10-05T19:01:06.615492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['bg+1:00'] = pred\nsubmission","metadata":{"execution":{"iopub.status.busy":"2024-10-05T19:01:15.883704Z","iopub.execute_input":"2024-10-05T19:01:15.884094Z","iopub.status.idle":"2024-10-05T19:01:15.895822Z","shell.execute_reply.started":"2024-10-05T19:01:15.884056Z","shell.execute_reply":"2024-10-05T19:01:15.894876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-05T19:01:40.810636Z","iopub.execute_input":"2024-10-05T19:01:40.811035Z","iopub.status.idle":"2024-10-05T19:01:40.826880Z","shell.execute_reply.started":"2024-10-05T19:01:40.810997Z","shell.execute_reply":"2024-10-05T19:01:40.826111Z"},"trusted":true},"execution_count":null,"outputs":[]}]}