{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":119699,"databundleVersionId":14318436,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":9.738491,"end_time":"2024-06-11T15:39:33.970538","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-06-11T15:39:24.232047","version":"2.5.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"f9955482","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)\nimport seaborn as sns # plotting\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\nfor 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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2025-11-02T15:17:55.133267Z","iopub.execute_input":"2025-11-02T15:17:55.133463Z","iopub.status.idle":"2025-11-02T15:17:56.420080Z","shell.execute_reply.started":"2025-11-02T15:17:55.133445Z","shell.execute_reply":"2025-11-02T15:17:56.418899Z"},"papermill":{"duration":2.410722,"end_time":"2024-06-11T15:39:29.511077","exception":false,"start_time":"2024-06-11T15:39:27.100355","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"929f765d","cell_type":"code","source":"def load_data(crop: str, mode: str=\"train\"):\n    # note that years represent an offset from model spinup;\n    # soil co2 dataset has real year;\n    # 0-30 are days before sowing, 31-238 are days after sowing\n    #tasmax = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/tasmax_{crop}_{mode}.parquet\")\n    #tasmin = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/tasmin_{crop}_{mode}.parquet\")\n    #pr = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/pr_{crop}_{mode}.parquet\")\n    #rsds = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/rsds_{crop}_{mode}.parquet\")\n    soil_co2 = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/soil_co2_{crop}_{mode}.parquet\")\n    target = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/{mode}_solutions_{crop}.parquet\")\n    return {\n        #'tasmax': tasmax,\n        #'tasmin': tasmin,\n        #'pr': pr,\n        #'rsds': rsds,\n        'soil_co2': soil_co2,\n        'target': target,\n    }","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:06.311463Z","iopub.execute_input":"2025-11-02T15:18:06.311778Z","iopub.status.idle":"2025-11-02T15:18:06.317322Z","shell.execute_reply.started":"2025-11-02T15:18:06.311751Z","shell.execute_reply":"2025-11-02T15:18:06.316414Z"},"papermill":{"duration":0.013809,"end_time":"2024-06-11T15:39:29.528755","exception":false,"start_time":"2024-06-11T15:39:29.514946","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"9f447229","cell_type":"code","source":"maize_train = load_data(\"maize\", \"train\")\nmaize_train.keys()","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:12.783682Z","iopub.execute_input":"2025-11-02T15:18:12.784028Z","iopub.status.idle":"2025-11-02T15:18:13.105654Z","shell.execute_reply.started":"2025-11-02T15:18:12.784001Z","shell.execute_reply":"2025-11-02T15:18:13.104782Z"},"papermill":{"duration":0.261492,"end_time":"2024-06-11T15:39:29.793880","exception":false,"start_time":"2024-06-11T15:39:29.532388","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"ef603523","cell_type":"code","source":"wheat_train = load_data(\"wheat\", \"train\")\nwheat_train.keys()","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:14.432618Z","iopub.execute_input":"2025-11-02T15:18:14.432968Z","iopub.status.idle":"2025-11-02T15:18:14.564434Z","shell.execute_reply.started":"2025-11-02T15:18:14.432922Z","shell.execute_reply":"2025-11-02T15:18:14.563460Z"},"papermill":{"duration":0.10015,"end_time":"2024-06-11T15:39:29.897771","exception":false,"start_time":"2024-06-11T15:39:29.797621","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"a6b949df","cell_type":"code","source":"# calculate mean maize yield at each grid cell\nmean_maize_per_gridcell = maize_train['soil_co2'].join(maize_train['target']).groupby(\n    ['lon','lat'])['yield'].mean()","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:18.545434Z","iopub.execute_input":"2025-11-02T15:18:18.545746Z","iopub.status.idle":"2025-11-02T15:18:18.645522Z","shell.execute_reply.started":"2025-11-02T15:18:18.545721Z","shell.execute_reply":"2025-11-02T15:18:18.644556Z"},"papermill":{"duration":0.103869,"end_time":"2024-06-11T15:39:30.005557","exception":false,"start_time":"2024-06-11T15:39:29.901688","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"4e733ea4","cell_type":"code","source":"# calculate mean wheat yield at each grid cell\nmean_wheat_per_gridcell = wheat_train['soil_co2'].join(wheat_train['target']).groupby(\n    ['lon','lat'])['yield'].mean()","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:20.658183Z","iopub.execute_input":"2025-11-02T15:18:20.658974Z","iopub.status.idle":"2025-11-02T15:18:20.714947Z","shell.execute_reply.started":"2025-11-02T15:18:20.658914Z","shell.execute_reply":"2025-11-02T15:18:20.713855Z"},"papermill":{"duration":0.054929,"end_time":"2024-06-11T15:39:30.064416","exception":false,"start_time":"2024-06-11T15:39:30.009487","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"cc42db80","cell_type":"code","source":"soil_co2_maize_test = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/soil_co2_maize_test.parquet\")","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:26.233481Z","iopub.execute_input":"2025-11-02T15:18:26.233790Z","iopub.status.idle":"2025-11-02T15:18:26.353500Z","shell.execute_reply.started":"2025-11-02T15:18:26.233765Z","shell.execute_reply":"2025-11-02T15:18:26.352699Z"},"papermill":{"duration":0.119509,"end_time":"2024-06-11T15:39:30.187953","exception":false,"start_time":"2024-06-11T15:39:30.068444","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"976413c7","cell_type":"code","source":"# join with IDs of datapoints to be predicted\nmaize_predictions = pd.merge(soil_co2_maize_test.reset_index(), \n                             mean_maize_per_gridcell.reset_index(), \n                             how='left', on=['lat','lon']).set_index('ID')[['yield']]","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:28.089704Z","iopub.execute_input":"2025-11-02T15:18:28.090058Z","iopub.status.idle":"2025-11-02T15:18:28.336973Z","shell.execute_reply.started":"2025-11-02T15:18:28.090033Z","shell.execute_reply":"2025-11-02T15:18:28.336035Z"},"papermill":{"duration":0.253214,"end_time":"2024-06-11T15:39:30.445280","exception":false,"start_time":"2024-06-11T15:39:30.192066","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"954b8b9d","cell_type":"code","source":"soil_co2_wheat_test = pd.read_parquet(f\"/kaggle/input/ag-ml-leipzig-2025-future-crop/soil_co2_wheat_test.parquet\")","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:32.414556Z","iopub.execute_input":"2025-11-02T15:18:32.414856Z","iopub.status.idle":"2025-11-02T15:18:32.522274Z","shell.execute_reply.started":"2025-11-02T15:18:32.414836Z","shell.execute_reply":"2025-11-02T15:18:32.521267Z"},"papermill":{"duration":0.091379,"end_time":"2024-06-11T15:39:30.540795","exception":false,"start_time":"2024-06-11T15:39:30.449416","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"c223b887","cell_type":"code","source":"# join with IDs of datapoints to be predicted\nwheat_predictions = pd.merge(soil_co2_wheat_test.reset_index(), \n                             mean_wheat_per_gridcell.reset_index(), \n                             how='left', on=['lat','lon']).set_index('ID')[['yield']]","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:34.068913Z","iopub.execute_input":"2025-11-02T15:18:34.069239Z","iopub.status.idle":"2025-11-02T15:18:34.194594Z","shell.execute_reply.started":"2025-11-02T15:18:34.069215Z","shell.execute_reply":"2025-11-02T15:18:34.193382Z"},"papermill":{"duration":0.187846,"end_time":"2024-06-11T15:39:30.732609","exception":false,"start_time":"2024-06-11T15:39:30.544763","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"b97b99f2","cell_type":"code","source":"pd.concat([maize_predictions, wheat_predictions]).to_csv('submission.csv', index_label='ID')","metadata":{"execution":{"iopub.status.busy":"2025-11-02T15:18:35.097192Z","iopub.execute_input":"2025-11-02T15:18:35.097508Z","iopub.status.idle":"2025-11-02T15:18:37.758219Z","shell.execute_reply.started":"2025-11-02T15:18:35.097483Z","shell.execute_reply":"2025-11-02T15:18:37.757271Z"},"papermill":{"duration":2.609104,"end_time":"2024-06-11T15:39:33.345563","exception":false,"start_time":"2024-06-11T15:39:30.736459","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"id":"1e379f9f-bb93-4575-b424-fd7aa6c72f44","cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}