{"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":"markdown","source":"**LB Projection - Linear Regression + Custom Value Prediction**\n\nThis definitely is not the most accurate way to predict LB ranking but it's also not the worst. \nHopefully this can of some use to others. Feel free to post your result for the test image and I'll update the notebook.\n\n1. Fits a LinearRegression model to predict LB\n2. Prints coefficients and R²\n3. Provides a function to predict LB from custom input values","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import r2_score\nimport matplotlib.pyplot as plt\n\ndata = [\n    # LB,    Dry_Clover,    Dry_Dead,      Dry_Green,     Dry_Total,     GDM,          PredictedLB (will be overwritten)\n    [0.58,  2.558577299,   20.15970993,   15.28732777,   41.29204178,   18.13201904,  0.50],\n    [0.57,  3.72712183,    24.5919075,    17.92996597,   47.04356384,   20.78431129,  0.59],\n    [0.58,  3.259704113,   22.81902695,   16.87291145,   44.74295807,   19.72339439,  0.55],\n    [0.54,  3.669714622,   22.07929331,   17.4599484,    47.00748792,   20.07738674,  0.58],\n    [0.60,  2.91443491,    23.29337311,   16.60941315,   44.95372772,   19.33857155,  0.55],\n    [0.60,  2.8921194,     23.353289,     16.62601,      45.092617,     19.376806,    0.55],\n    [0.58,  3.4464824,     22.786255,     13.110432,     42.629986,     17.219278,    0.52],\n    [0.60,  2.7290902,     22.691826,     17.092129,     45.233673,     19.893349,    0.54],\n    [0.64,  0.88776318,    27.32965843,   30.43461025,   55.94826284,   29.97483526,  0.62],\n    [0.62,  1.512121081,   27.3822422,    25.4311676,    53.61979294,   26.25754547,  0.61],\n    [0.61,  0.0,           28.531084,     25.439943,     53.54935,      25.018267,    0.61],\n    [0.63,  1.559820912,   24.45449907,   30.92301098,   52.22661422,   30.72606398,  0.60],\n    [0.41,  3.881119728,   28.80115509,   20.80769348,   55.33737183,   24.43947983,  0.62],\n    [0.52,  2.142392187,   24.15621873,   16.02903234,   45.3516429,    17.79019554,  0.57],\n    [0.39,  0.27386871,    15.59211257,   29.21345762,   45.07883628,   29.48732633,  0.46],\n    [0.34,  0.0,           12.76616922,   16.94626627,   28.8998079,    16.94626627,  0.41],\n    [0.28,  3.002070252,   19.77314176,   29.03622004,   60.16116581,   37.10327265,  0.35],\n    [0.22,  3.119873,      18.515196,     28.475609,     60.51724,      40.98496,     0.17],\n    [0.32,  1.1283996,     22.919573,     20.474012,     49.77112,      26.883503,    0.35],\n    [0.66,  1.211413622,   18.18782616,   27.04925537,   49.60133743,   25.34452248,  0.62],\n    [0.65,  1.7825888,     26.787958,     32.905235,     61.475784,     34.687828,    0.58],\n    [0.65,  0.0,           31.075018,     28.56366,      59.63868,      28.56366,     0.63],\n]\n\ncolumns = [\n    \"LB\",\n    \"ID1001187975__Dry_Clover_g\",\n    \"ID1001187975__Dry_Dead_g\",\n    \"ID1001187975__Dry_Green_g\",\n    \"ID1001187975__Dry_Total_g\",\n    \"ID1001187975__GDM_g\",\n    \"PredictedLB\",   # will be overwritten with model predictions\n]\n\ndf = pd.DataFrame(data, columns=columns)\n\nfeature_cols = [\n    \"ID1001187975__Dry_Clover_g\",\n    \"ID1001187975__Dry_Dead_g\",\n    \"ID1001187975__Dry_Green_g\",\n    \"ID1001187975__Dry_Total_g\",\n    \"ID1001187975__GDM_g\",\n]\n\nX = df[feature_cols].values\ny = df[\"LB\"].values\n\nmodel = LinearRegression()\nmodel.fit(X, y)\n\n# Recalculate predictions from the model (overwrite PredictedLB)\ndf[\"PredictedLB\"] = model.predict(X)\n\nr2_model = r2_score(df[\"LB\"], df[\"PredictedLB\"])\nprint(f\"In-sample R² (model): {r2_model:.4f}\")\n\n\n# Helper if you want to manually plug in values\ndef predict_lb_from_values(dry_clover, dry_dead, dry_green, dry_total, gdm):\n    X_new = [[dry_clover, dry_dead, dry_green, dry_total, gdm]]\n    return float(model.predict(X_new)[0])\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-01T23:49:17.580728Z","iopub.execute_input":"2025-12-01T23:49:17.581774Z","iopub.status.idle":"2025-12-01T23:49:17.600616Z","shell.execute_reply.started":"2025-12-01T23:49:17.581732Z","shell.execute_reply":"2025-12-01T23:49:17.599545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplt.figure(figsize=(8, 6))\nplt.scatter(df[\"LB\"], df[\"PredictedLB\"])\nplt.xlabel(\"Actual LB\")\nplt.ylabel(\"Predicted LB (model)\")\nplt.title(f\"Actual vs Predicted LB - R² = {r2_model:.3f}\")\n\nlb_min = min(df[\"LB\"].min(), df[\"PredictedLB\"].min())\nlb_max = max(df[\"LB\"].max(), df[\"PredictedLB\"].max())\nplt.plot([lb_min, lb_max], [lb_min, lb_max])\nplt.grid(True)\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-01T23:49:26.972765Z","iopub.execute_input":"2025-12-01T23:49:26.973890Z","iopub.status.idle":"2025-12-01T23:49:27.208409Z","shell.execute_reply.started":"2025-12-01T23:49:26.973847Z","shell.execute_reply":"2025-12-01T23:49:27.207395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Example usage: custom values\nexample_lb = predict_lb_from_values(\n    1.559821,   # Dry_Clover\n    24.454499,  # Dry_Dead\n    30.923011,  # Dry_Green\n    52.226614,  # Dry_Total\n    30.726064,  # GDM\n)\n\nprint(f\"\\nPredicted LB for custom values: {example_lb:.2f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-01T23:49:31.166012Z","iopub.execute_input":"2025-12-01T23:49:31.166737Z","iopub.status.idle":"2025-12-01T23:49:31.172552Z","shell.execute_reply.started":"2025-12-01T23:49:31.166705Z","shell.execute_reply":"2025-12-01T23:49:31.171503Z"}},"outputs":[],"execution_count":null}]}