{"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":70367,"databundleVersionId":9188054,"sourceType":"competition"},{"sourceId":152078,"sourceType":"modelInstanceVersion","modelInstanceId":129154,"modelId":152014}],"dockerImageVersionId":30786,"isInternetEnabled":false,"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\n# for 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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-31T11:46:46.845524Z","iopub.execute_input":"2024-10-31T11:46:46.845923Z","iopub.status.idle":"2024-10-31T11:46:47.915749Z","shell.execute_reply.started":"2024-10-31T11:46:46.845884Z","shell.execute_reply":"2024-10-31T11:46:47.914656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nfrom sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2024-10-31T11:46:47.917356Z","iopub.execute_input":"2024-10-31T11:46:47.917929Z","iopub.status.idle":"2024-10-31T11:46:49.279309Z","shell.execute_reply.started":"2024-10-31T11:46:47.917892Z","shell.execute_reply":"2024-10-31T11:46:49.278233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"/kaggle/input/final_model/scikitlearn/version1/1/final_model.sav\"\n\nwith open(filename, mode='rb') as file:\n    model_linear = pickle.load(file)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T11:49:58.567348Z","iopub.execute_input":"2024-10-31T11:49:58.567935Z","iopub.status.idle":"2024-10-31T11:49:58.784117Z","shell.execute_reply.started":"2024-10-31T11:49:58.567885Z","shell.execute_reply":"2024-10-31T11:49:58.783001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ntest_df = pd.DataFrame()\nfor planet_id in os.listdir(\"/kaggle/input/ariel-data-challenge-2024/test/\"):\n    fgs1_signal = pd.read_parquet(f\"/kaggle/input/ariel-data-challenge-2024/test/{planet_id}/FGS1_signal.parquet\")\n    \n    final_image = np.zeros((1,1024))\n    for i in range(len(fgs1_signal)):\n        array = np.array(fgs1_signal.iloc[i])\n        final_image += array\n    \n    final_row = pd.DataFrame(final_image/len(fgs1_signal)) # Calculating Mean\n    planet_id_col = pd.DataFrame(data=[int(planet_id)], columns=[\"planet_id\"])\n    final_row = pd.concat([planet_id_col, final_row], axis=1)\n    test_df = pd.concat([test_df, final_row])\n\ntest_df","metadata":{"execution":{"iopub.status.busy":"2024-10-31T11:50:13.767157Z","iopub.execute_input":"2024-10-31T11:50:13.767967Z","iopub.status.idle":"2024-10-31T11:50:24.321909Z","shell.execute_reply.started":"2024-10-31T11:50:13.767924Z","shell.execute_reply":"2024-10-31T11:50:24.320908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = test_df.drop(columns=\"planet_id\")\npred_linear = model_linear.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T11:50:30.91948Z","iopub.execute_input":"2024-10-31T11:50:30.919901Z","iopub.status.idle":"2024-10-31T11:50:30.944514Z","shell.execute_reply.started":"2024-10-31T11:50:30.919861Z","shell.execute_reply":"2024-10-31T11:50:30.943175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make Negative predictions become zero\n\nfor planet_no in range(len(pred_linear)):\n    for pred_no in range(len(pred_linear[planet_no])):\n        if pred_linear[planet_no][pred_no] < 0:\n            pred_linear[planet_no][pred_no] = 0","metadata":{"execution":{"iopub.status.busy":"2024-10-31T11:59:53.234126Z","iopub.execute_input":"2024-10-31T11:59:53.234553Z","iopub.status.idle":"2024-10-31T11:59:53.240153Z","shell.execute_reply.started":"2024-10-31T11:59:53.234513Z","shell.execute_reply":"2024-10-31T11:59:53.23916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_linear.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-31T12:00:18.66541Z","iopub.execute_input":"2024-10-31T12:00:18.665832Z","iopub.status.idle":"2024-10-31T12:00:18.671936Z","shell.execute_reply.started":"2024-10-31T12:00:18.665794Z","shell.execute_reply":"2024-10-31T12:00:18.670888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \"Test Ground Truth\"\n\ntest_labels = []\nfor i in range(len(pred_linear)):\n    test_median = np.full((283), np.median(pred_linear[i]))\n    test_labels.append(test_median)\n\ntest_labels = np.array(test_labels)\ntest_labels.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-31T12:00:23.387481Z","iopub.execute_input":"2024-10-31T12:00:23.387903Z","iopub.status.idle":"2024-10-31T12:00:23.39659Z","shell.execute_reply.started":"2024-10-31T12:00:23.387864Z","shell.execute_reply":"2024-10-31T12:00:23.395569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sigma_test = []\nfor i in range(len(pred_linear)):\n    sigma = mean_squared_error(test_labels[i], pred_linear[i], squared=False)\n    sigma = np.full((283),sigma)\n    sigma_test.append(sigma)\n\nsigma_test = np.array(sigma_test)\nsigma_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-31T12:00:25.035645Z","iopub.execute_input":"2024-10-31T12:00:25.036054Z","iopub.status.idle":"2024-10-31T12:00:25.047354Z","shell.execute_reply.started":"2024-10-31T12:00:25.036015Z","shell.execute_reply":"2024-10-31T12:00:25.046253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predictions = np.concatenate((pred_linear,sigma_test),axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T12:00:27.031727Z","iopub.execute_input":"2024-10-31T12:00:27.032557Z","iopub.status.idle":"2024-10-31T12:00:27.037239Z","shell.execute_reply.started":"2024-10-31T12:00:27.03251Z","shell.execute_reply":"2024-10-31T12:00:27.036126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submitting to Competition","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/ariel-data-challenge-2024/sample_submission.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2024-10-31T12:00:29.981222Z","iopub.execute_input":"2024-10-31T12:00:29.982034Z","iopub.status.idle":"2024-10-31T12:00:30.026863Z","shell.execute_reply.started":"2024-10-31T12:00:29.981988Z","shell.execute_reply":"2024-10-31T12:00:30.02574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.iloc[:,1:] = final_predictions\nsubmission","metadata":{"execution":{"iopub.status.busy":"2024-10-31T12:00:31.392564Z","iopub.execute_input":"2024-10-31T12:00:31.393483Z","iopub.status.idle":"2024-10-31T12:00:31.42311Z","shell.execute_reply.started":"2024-10-31T12:00:31.39344Z","shell.execute_reply":"2024-10-31T12:00:31.42211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-31T12:00:40.643538Z","iopub.execute_input":"2024-10-31T12:00:40.644241Z","iopub.status.idle":"2024-10-31T12:00:40.654418Z","shell.execute_reply.started":"2024-10-31T12:00:40.644201Z","shell.execute_reply":"2024-10-31T12:00:40.653553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}