{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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":112509,"databundleVersionId":14254895,"sourceType":"competition"}],"dockerImageVersionId":31192,"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 time\n\nfrom pathlib import Path","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-21T04:34:59.564453Z","iopub.execute_input":"2025-11-21T04:34:59.564774Z","iopub.status.idle":"2025-11-21T04:34:59.569891Z","shell.execute_reply.started":"2025-11-21T04:34:59.564752Z","shell.execute_reply":"2025-11-21T04:34:59.568860Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_DIR = Path(\"/kaggle/input/csiro-biomass\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-21T04:34:59.571478Z","iopub.execute_input":"2025-11-21T04:34:59.571807Z","iopub.status.idle":"2025-11-21T04:34:59.592797Z","shell.execute_reply.started":"2025-11-21T04:34:59.571777Z","shell.execute_reply":"2025-11-21T04:34:59.591367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(DATA_DIR / \"train.csv\")\n\nexample_output = train_df[[\"target_name\", \"target\"]].copy()\nexample_output[\"prediction\"] = train_df[\"target\"] ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-21T04:34:59.593870Z","iopub.execute_input":"2025-11-21T04:34:59.594294Z","iopub.status.idle":"2025-11-21T04:34:59.625881Z","shell.execute_reply.started":"2025-11-21T04:34:59.594165Z","shell.execute_reply":"2025-11-21T04:34:59.624611Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def score_pd(df,\n          target_type_col=\"target_name\",\n          target_col=\"target\",\n          pred_col=\"prediction\"):\n\n    weights = {\n        \"Dry_Green_g\": 0.1,\n        \"Dry_Dead_g\": 0.1,\n        \"Dry_Clover_g\": 0.1,\n        \"GDM_g\": 0.2,\n        \"Dry_Total_g\": 0.5,\n    }\n\n    w = df[target_type_col].map(weights).values\n    y = df[target_col].values\n    y_pred = df[pred_col].values\n\n    # Weighted mean\n    y_wmean = (w * y).sum() / w.sum()\n\n    # Residuals\n    ss_res = ((y - y_pred) ** 2 * w).sum()\n    ss_tot = ((y - y_wmean) ** 2 * w).sum()\n\n    return 1 - ss_res / ss_tot\n\ndef score_numpy(target_type, y, y_pred):\n    weights = {\n        \"Dry_Green_g\": 0.1,\n        \"Dry_Dead_g\": 0.1,\n        \"Dry_Clover_g\": 0.1,\n        \"GDM_g\": 0.2,\n        \"Dry_Total_g\": 0.5,\n    }\n\n    # Map types → weight array\n    w = np.vectorize(weights.get)(target_type)\n\n    # Weighted mean\n    y_wmean = np.sum(w * y) / np.sum(w)\n\n    # SS_res and SS_tot\n    ss_res = np.sum(w * (y - y_pred) ** 2)\n    ss_tot = np.sum(w * (y - y_wmean) ** 2)\n\n    return 1 - ss_res / ss_tot\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-21T04:34:59.627789Z","iopub.execute_input":"2025-11-21T04:34:59.628163Z","iopub.status.idle":"2025-11-21T04:34:59.637554Z","shell.execute_reply.started":"2025-11-21T04:34:59.628138Z","shell.execute_reply":"2025-11-21T04:34:59.636099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nn_iterations = 1000\n\n# Pandas version \nt1 = time.perf_counter()    \nfor i in range(n_iterations):\n    score_pd(example_output)\nt2 = time.perf_counter()\nprint(f\"Pandas score time: {t2 - t1:.6f} sec\")\n\n# Numpy version \nt1 = time.perf_counter()\nfor i in range(n_iterations):\n    score_numpy(\n        example_output[\"target_name\"].to_numpy(),\n        example_output[\"target\"].to_numpy(),\n        example_output[\"prediction\"].to_numpy()\n    )\nt2 = time.perf_counter()\nprint(f\"Numpy score time: {t2 - t1:.6f} sec\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-21T04:34:59.638647Z","iopub.execute_input":"2025-11-21T04:34:59.638971Z","iopub.status.idle":"2025-11-21T04:35:00.384788Z","shell.execute_reply.started":"2025-11-21T04:34:59.638942Z","shell.execute_reply":"2025-11-21T04:35:00.383474Z"}},"outputs":[],"execution_count":null}]}