{"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":12328662,"sourceType":"datasetVersion","datasetId":7771543},{"sourceId":247897800,"sourceType":"kernelVersion"},{"sourceId":248025963,"sourceType":"kernelVersion"},{"sourceId":248100797,"sourceType":"kernelVersion"},{"sourceId":248107037,"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\nimport pickle","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-30T11:47:45.825912Z","iopub.execute_input":"2025-06-30T11:47:45.826203Z","iopub.status.idle":"2025-06-30T11:47:48.371753Z","shell.execute_reply.started":"2025-06-30T11:47:45.826179Z","shell.execute_reply":"2025-06-30T11:47:48.370469Z"}},"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-30T12:19:20.275397Z","iopub.execute_input":"2025-06-30T12:19:20.275718Z","iopub.status.idle":"2025-06-30T12:19:20.417585Z","shell.execute_reply.started":"2025-06-30T12:19:20.275695Z","shell.execute_reply":"2025-06-30T12:19:20.416353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_1 = pickle.load(open('/kaggle/input/gwi-val-ensemble-flip/val_ensemble.p', 'br'))\nval_2 = pickle.load(open('/kaggle/input/gwi-val-ensemble-noflip/val_ensemble.p', 'br'))\nval_style = pickle.load(open('/kaggle/input/gwi-val-ensemble-style/val_ensemble.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T11:48:23.881683Z","iopub.execute_input":"2025-06-30T11:48:23.882046Z","iopub.status.idle":"2025-06-30T11:48:26.090405Z","shell.execute_reply.started":"2025-06-30T11:48:23.882019Z","shell.execute_reply":"2025-06-30T11:48:26.089161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"weights_list = []\nfor i in range(10):\n    print(f'i: {i}********************')\n    min_score = [100]\n    weights = [-1,-1,-1]\n    for val_1_weight in range(5):\n        print(f'Round: {val_1_weight}')\n        for val_2_weight in range(5):\n            for val_3_weight in range(5):\n                ensemble = (val_1_weight*val_1+val_2_weight*val_2+val_3_weight*val_style)/(val_1_weight+val_2_weight+val_3_weight)\n                score = np.mean(np.abs(ensemble-val_labels)[i*1000:(i+1)*1000])\n                if score<min_score:\n                    min_score = score\n                    weights = [val_1_weight, val_2_weight, val_3_weight]\n                    print(score)\n    weights_list.append(weights)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T11:59:45.066826Z","iopub.execute_input":"2025-06-30T11:59:45.067241Z","iopub.status.idle":"2025-06-30T12:05:44.833720Z","shell.execute_reply.started":"2025-06-30T11:59:45.067215Z","shell.execute_reply":"2025-06-30T12:05:44.832824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds_labels = pickle.load(open('/kaggle/input/gwi-test-labels/test_preds.p', 'br'))\nval_preds_labels = pickle.load(open('/kaggle/input/gwi-test-labels/metrics_2.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:13:23.389488Z","iopub.execute_input":"2025-06-30T12:13:23.389802Z","iopub.status.idle":"2025-06-30T12:13:23.458655Z","shell.execute_reply.started":"2025-06-30T12:13:23.389780Z","shell.execute_reply":"2025-06-30T12:13:23.457658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_kind_labels = np.argmax(val_preds_labels, axis = 1)\nplt.plot(val_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:19:36.845446Z","iopub.execute_input":"2025-06-30T12:19:36.846369Z","iopub.status.idle":"2025-06-30T12:19:37.018072Z","shell.execute_reply.started":"2025-06-30T12:19:36.846339Z","shell.execute_reply":"2025-06-30T12:19:37.017026Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_ensemble(set_1, set_2, set_3, weights, labels):\n    preds = np.zeros((set_1.shape))\n    for i in range(10):\n        batch_indices = labels==i\n        batch_ensemble = (set_1[batch_indices]*weights[i][0]+\n                          set_2[batch_indices]*weights[i][1]+\n                          set_3[batch_indices]*weights[i][2])/(weights[i][0]+weights[i][1]+weights[i][2])\n        preds[batch_indices] = batch_ensemble\n    return preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:24:37.286188Z","iopub.execute_input":"2025-06-30T12:24:37.286501Z","iopub.status.idle":"2025-06-30T12:24:37.293415Z","shell.execute_reply.started":"2025-06-30T12:24:37.286478Z","shell.execute_reply":"2025-06-30T12:24:37.292396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_ensemble = get_ensemble(val_1, val_2, val_style, weights_list, val_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:24:41.910525Z","iopub.execute_input":"2025-06-30T12:24:41.910829Z","iopub.status.idle":"2025-06-30T12:24:42.510134Z","shell.execute_reply.started":"2025-06-30T12:24:41.910806Z","shell.execute_reply":"2025-06-30T12:24:42.509269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ensemble = val_ensemble\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-30T12:24:44.423469Z","iopub.execute_input":"2025-06-30T12:24:44.423819Z","iopub.status.idle":"2025-06-30T12:24:46.440642Z","shell.execute_reply.started":"2025-06-30T12:24:44.423792Z","shell.execute_reply":"2025-06-30T12:24:46.439217Z"}},"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-30T12:25:39.853942Z","iopub.execute_input":"2025-06-30T12:25:39.854345Z","iopub.status.idle":"2025-06-30T12:25:40.652625Z","shell.execute_reply.started":"2025-06-30T12:25:39.854314Z","shell.execute_reply":"2025-06-30T12:25:40.651457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_1 = pickle.load(open('/kaggle/input/gwi-val-ensemble-flip/test_ensemble.p', 'br'))\ntest_2 = pickle.load(open('/kaggle/input/gwi-val-ensemble-noflip/test_ensemble.p', 'br'))\ntest_style = pickle.load(open('/kaggle/input/gwi-val-ensemble-style/test_ensemble.p', 'br'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:26:25.692290Z","iopub.execute_input":"2025-06-30T12:26:25.692844Z","iopub.status.idle":"2025-06-30T12:26:51.809713Z","shell.execute_reply.started":"2025-06-30T12:26:25.692817Z","shell.execute_reply":"2025-06-30T12:26:51.808766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_kind_labels = np.argmax(test_preds_labels, axis = 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:27:36.928821Z","iopub.execute_input":"2025-06-30T12:27:36.929672Z","iopub.status.idle":"2025-06-30T12:27:36.937497Z","shell.execute_reply.started":"2025-06-30T12:27:36.929644Z","shell.execute_reply":"2025-06-30T12:27:36.936472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(10):\n    print(np.sum(test_kind_labels == i)/len(test_kind_labels))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:29:32.366995Z","iopub.execute_input":"2025-06-30T12:29:32.367395Z","iopub.status.idle":"2025-06-30T12:29:32.376370Z","shell.execute_reply.started":"2025-06-30T12:29:32.367370Z","shell.execute_reply":"2025-06-30T12:29:32.375122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(test_kind_labels, bins = 20)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:27:53.113549Z","iopub.execute_input":"2025-06-30T12:27:53.113950Z","iopub.status.idle":"2025-06-30T12:27:53.317415Z","shell.execute_reply.started":"2025-06-30T12:27:53.113918Z","shell.execute_reply":"2025-06-30T12:27:53.315937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = get_ensemble(test_1, test_2, test_style, weights_list, test_kind_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:30:44.752251Z","iopub.execute_input":"2025-06-30T12:30:44.752602Z","iopub.status.idle":"2025-06-30T12:30:49.480194Z","shell.execute_reply.started":"2025-06-30T12:30:44.752577Z","shell.execute_reply":"2025-06-30T12:30:49.478988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(test[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-30T12:30:53.181736Z","iopub.execute_input":"2025-06-30T12:30:53.182486Z","iopub.status.idle":"2025-06-30T12:30:53.384996Z","shell.execute_reply.started":"2025-06-30T12:30:53.182450Z","shell.execute_reply":"2025-06-30T12:30:53.383813Z"}},"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-30T11:47:54.238611Z","iopub.status.idle":"2025-06-30T11:47:54.239052Z","shell.execute_reply.started":"2025-06-30T11:47:54.238786Z","shell.execute_reply":"2025-06-30T11:47:54.238803Z"}},"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))\nfor i in range(65818//batch_size+1):\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])\npickle.dump(test, open('test_ensemble.p', 'bw'))\nsample_submission.iloc[:, -35:] = all_preds\nsample_submission.to_parquet('submission.parquet', index=False)\nsubmission = 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-30T11:47:54.240539Z","iopub.status.idle":"2025-06-30T11:47:54.240938Z","shell.execute_reply.started":"2025-06-30T11:47:54.240725Z","shell.execute_reply":"2025-06-30T11:47:54.240743Z"}},"outputs":[],"execution_count":null}]}