{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":39763,"databundleVersionId":11756775,"sourceType":"competition"},{"sourceId":247897800,"sourceType":"kernelVersion"},{"sourceId":248022342,"sourceType":"kernelVersion"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pickle\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:57:55.907981Z","iopub.execute_input":"2025-06-29T21:57:55.908277Z","iopub.status.idle":"2025-06-29T21:57:56.235723Z","shell.execute_reply.started":"2025-06-29T21:57:55.908247Z","shell.execute_reply":"2025-06-29T21:57:56.234726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"indices_32_36 = [32,33,34,35,36]\nindices_37_42 = [37,38,39,41,42]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:57:56.237477Z","iopub.execute_input":"2025-06-29T21:57:56.238218Z","iopub.status.idle":"2025-06-29T21:57:56.242208Z","shell.execute_reply.started":"2025-06-29T21:57:56.238195Z","shell.execute_reply":"2025-06-29T21:57:56.241250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_labels = pickle.load(open('/kaggle/input/gwi-preds-37-52-ds-to-notebook/val_labels.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:57:56.243126Z","iopub.execute_input":"2025-06-29T21:57:56.243405Z","iopub.status.idle":"2025-06-29T21:57:57.233308Z","shell.execute_reply.started":"2025-06-29T21:57:56.243385Z","shell.execute_reply":"2025-06-29T21:57:57.232323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vals_preds = ([pickle.load(open(f'/kaggle/input/gwi-preds-32-36-ds-to-notebook/{x}/val_preds_best.p', 'br')) for x in indices_32_36]+\n              [pickle.load(open(f'/kaggle/input/gwi-preds-37-52-ds-to-notebook/{x}/val_preds_best.p', 'br')) for x in indices_37_42])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:57:57.234277Z","iopub.execute_input":"2025-06-29T21:57:57.234565Z","iopub.status.idle":"2025-06-29T21:58:08.762376Z","shell.execute_reply.started":"2025-06-29T21:57:57.234535Z","shell.execute_reply":"2025-06-29T21:58:08.761695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vals_preds = vals_preds[::-1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:08.764482Z","iopub.execute_input":"2025-06-29T21:58:08.764690Z","iopub.status.idle":"2025-06-29T21:58:08.768784Z","shell.execute_reply.started":"2025-06-29T21:58:08.764674Z","shell.execute_reply":"2025-06-29T21:58:08.767984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble = np.median(vals_preds[:4], axis = 0)\nsub_dataset_scores = []\nfor i in range(10):\n    score = np.mean(np.abs(ensemble-val_labels)[i*1000:(i+1)*1000])\n    print(score)\n    sub_dataset_scores.append(score)\nprint(np.mean(sub_dataset_scores))\nweighted_score = (np.sum(sub_dataset_scores[:4])*3+\n                  np.sum(sub_dataset_scores[4:8])*5.4+\n                  np.sum(sub_dataset_scores[8:10])*6.7)/(4*3+4*5.4+2*6.7)\nprint(weighted_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:08.769501Z","iopub.execute_input":"2025-06-29T21:58:08.769690Z","iopub.status.idle":"2025-06-29T21:58:14.377917Z","shell.execute_reply.started":"2025-06-29T21:58:08.769675Z","shell.execute_reply":"2025-06-29T21:58:14.376983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble = (vals_preds[0]*10 + vals_preds[1]*10 + vals_preds[2]*10 + vals_preds[3]*2 + vals_preds[4]*2 +\n            vals_preds[5]*2+vals_preds[6]*1+vals_preds[7]*1)/(10+10+10+2+2+2+1+1)\nsub_dataset_scores = []\nfor i in range(10):\n    score = np.mean(np.abs(ensemble-val_labels)[i*1000:(i+1)*1000])\n    print(score)\n    sub_dataset_scores.append(score)\nprint(np.mean(sub_dataset_scores))\nweighted_score = (np.sum(sub_dataset_scores[:4])*3+\n                  np.sum(sub_dataset_scores[4:8])*5.4+\n                  np.sum(sub_dataset_scores[8:10])*6.7)/(4*3+4*5.4+2*6.7)\nprint(weighted_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T22:01:31.627381Z","iopub.execute_input":"2025-06-29T22:01:31.628284Z","iopub.status.idle":"2025-06-29T22:01:32.851459Z","shell.execute_reply.started":"2025-06-29T22:01:31.628244Z","shell.execute_reply":"2025-06-29T22:01:32.850601Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_test(test_sets_path, weights):\n    test = pickle.load(open(test_sets_path[0], 'br'))\n    \n    test = test*weights[0]\n    \n    def update_test(test, path, weight):\n        test_temp = pickle.load(open(path, 'br'))\n        test_temp = test_temp*weight\n        return test+test_temp\n        \n    for i in range(1,len(test_sets_path)):\n        test = update_test(test, test_sets_path[i], weights[i])\n    \n    test = test/np.sum(weights)\n    return test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T22:02:31.591562Z","iopub.execute_input":"2025-06-29T22:02:31.591881Z","iopub.status.idle":"2025-06-29T22:02:31.597610Z","shell.execute_reply.started":"2025-06-29T22:02:31.591857Z","shell.execute_reply":"2025-06-29T22:02:31.596884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_sets_path = ['/kaggle/input/gwi-preds-37-52-ds-to-notebook/42/val_preds_best.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/41/val_preds_best.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/39/val_preds_best.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/38/val_preds_best.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/37/val_preds_best.p',\n                  '/kaggle/input/gwi-preds-32-36-ds-to-notebook/36/val_preds_best.p',\n                  '/kaggle/input/gwi-preds-32-36-ds-to-notebook/35/val_preds_best.p',\n                  '/kaggle/input/gwi-preds-32-36-ds-to-notebook/34/val_preds_best.p']\nweights = [10,10,10,2,2,2,1,1]\n\ntest = get_test(test_sets_path, weights)\nensemble = test\n\nsub_dataset_scores = []\nfor i in range(10):\n    score = np.mean(np.abs(ensemble-val_labels)[i*1000:(i+1)*1000])\n    print(score)\n    sub_dataset_scores.append(score)\nprint(np.mean(sub_dataset_scores))\nweighted_score = (np.sum(sub_dataset_scores[:4])*3+\n                  np.sum(sub_dataset_scores[4:8])*5.4+\n                  np.sum(sub_dataset_scores[8:10])*6.7)/(4*3+4*5.4+2*6.7)\nprint(weighted_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T22:02:57.207417Z","iopub.execute_input":"2025-06-29T22:02:57.207729Z","iopub.status.idle":"2025-06-29T22:03:00.014917Z","shell.execute_reply.started":"2025-06-29T22:02:57.207705Z","shell.execute_reply":"2025-06-29T22:03:00.013986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pickle.dump(ensemble, open('val_ensemble.p', 'bw'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.644863Z","iopub.status.idle":"2025-06-29T21:58:15.645181Z","shell.execute_reply.started":"2025-06-29T21:58:15.645060Z","shell.execute_reply":"2025-06-29T21:58:15.645074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_sets_path = ['/kaggle/input/gwi-preds-37-52-ds-to-notebook/42/test_preds.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/41/test_preds.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/39/test_preds.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/38/test_preds.p',\n                  '/kaggle/input/gwi-preds-37-52-ds-to-notebook/37/test_preds.p',\n                  '/kaggle/input/gwi-preds-32-36-ds-to-notebook/36/test_preds.p',\n                  '/kaggle/input/gwi-preds-32-36-ds-to-notebook/35/test_preds.p',\n                  '/kaggle/input/gwi-preds-32-36-ds-to-notebook/34/test_preds.p']\nweights = [10,10,10,2,2,2,1,1]\ntest = get_test(test_sets_path, weights)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.646614Z","iopub.status.idle":"2025-06-29T21:58:15.646977Z","shell.execute_reply.started":"2025-06-29T21:58:15.646789Z","shell.execute_reply":"2025-06-29T21:58:15.646825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.648529Z","iopub.status.idle":"2025-06-29T21:58:15.648917Z","shell.execute_reply.started":"2025-06-29T21:58:15.648699Z","shell.execute_reply":"2025-06-29T21:58:15.648717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(test[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.650275Z","iopub.status.idle":"2025-06-29T21:58:15.650594Z","shell.execute_reply.started":"2025-06-29T21:58:15.650404Z","shell.execute_reply":"2025-06-29T21:58:15.650420Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/waveform-inversion/sample_submission.csv')\nsample_submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.651419Z","iopub.status.idle":"2025-06-29T21:58:15.651718Z","shell.execute_reply.started":"2025-06-29T21:58:15.651580Z","shell.execute_reply":"2025-06-29T21:58:15.651595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"all_preds = np.zeros((4607260,35))\nbatch_size = 128\nx_odds = np.asarray(range(1,71,2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.654004Z","iopub.status.idle":"2025-06-29T21:58:15.654342Z","shell.execute_reply.started":"2025-06-29T21:58:15.654155Z","shell.execute_reply":"2025-06-29T21:58:15.654170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(65818//batch_size+1):\n    if i%10==0:\n        print(i)\n    preds = test[i*batch_size:(i+1)*batch_size]\n    preds = preds*100+3000\n    all_preds[i*70*batch_size:(i+1)*70*batch_size] = np.reshape(np.asarray(preds)[:, :, x_odds], [-1,35])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.655405Z","iopub.status.idle":"2025-06-29T21:58:15.655735Z","shell.execute_reply.started":"2025-06-29T21:58:15.655622Z","shell.execute_reply":"2025-06-29T21:58:15.655632Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pickle.dump(test, open('test_ensemble.p', 'bw'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.657414Z","iopub.status.idle":"2025-06-29T21:58:15.657753Z","shell.execute_reply.started":"2025-06-29T21:58:15.657579Z","shell.execute_reply":"2025-06-29T21:58:15.657595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nsample_submission.iloc[:, -35:] = all_preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.658677Z","iopub.status.idle":"2025-06-29T21:58:15.659026Z","shell.execute_reply.started":"2025-06-29T21:58:15.658853Z","shell.execute_reply":"2025-06-29T21:58:15.658869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nsample_submission.to_parquet('submission.parquet', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.660104Z","iopub.status.idle":"2025-06-29T21:58:15.660348Z","shell.execute_reply.started":"2025-06-29T21:58:15.660227Z","shell.execute_reply":"2025-06-29T21:58:15.660237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.read_parquet('/kaggle/working/submission.parquet')\nplt.imshow(np.asarray(submission[:70])[:, 1:].astype(float))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-29T21:58:15.661540Z","iopub.status.idle":"2025-06-29T21:58:15.661944Z","shell.execute_reply.started":"2025-06-29T21:58:15.661755Z","shell.execute_reply":"2025-06-29T21:58:15.661771Z"}},"outputs":[],"execution_count":null}]}