{"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":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":210330224,"sourceType":"kernelVersion"}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Inference\n\nIn this notebook I'm just going to do inference for the competition.\nIf you would like to have more details on the models please check:\n\n- EDA: https://www.kaggle.com/code/simonedegasperis/starter-eda\n- train: https://www.kaggle.com/code/simonedegasperis/2-train-with-lags-xgb/edit/run/209874401\n\nTwo boosting models (xgboost and lightgbm) have been trained. The final prediction is just heir average.","metadata":{}},{"cell_type":"code","source":"# imports\nimport os\nimport numpy as np\nimport pandas as pd\nimport polars as pl\nimport lightgbm as lgb\nimport xgboost as xgb\nimport pickle\nimport kaggle_evaluation.jane_street_inference_server","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:44.171349Z","iopub.execute_input":"2024-11-29T16:30:44.171746Z","iopub.status.idle":"2024-11-29T16:30:49.665242Z","shell.execute_reply.started":"2024-11-29T16:30:44.171709Z","shell.execute_reply":"2024-11-29T16:30:49.664124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/input/2-train-with-lags/\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:49.667129Z","iopub.execute_input":"2024-11-29T16:30:49.667891Z","iopub.status.idle":"2024-11-29T16:30:49.680955Z","shell.execute_reply.started":"2024-11-29T16:30:49.667854Z","shell.execute_reply":"2024-11-29T16:30:49.679732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_model = xgb.Booster()\nxgb_model.load_model(\"/kaggle/input/2-train-with-lags/xgboost_model.json\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:49.682358Z","iopub.execute_input":"2024-11-29T16:30:49.682730Z","iopub.status.idle":"2024-11-29T16:30:49.732436Z","shell.execute_reply.started":"2024-11-29T16:30:49.682694Z","shell.execute_reply":"2024-11-29T16:30:49.731560Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm_model = lgb.Booster(model_file='/kaggle/input/2-train-with-lags/lgbm_model.json')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:49.734240Z","iopub.execute_input":"2024-11-29T16:30:49.734604Z","iopub.status.idle":"2024-11-29T16:30:49.809051Z","shell.execute_reply.started":"2024-11-29T16:30:49.734568Z","shell.execute_reply":"2024-11-29T16:30:49.807397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"/kaggle/input/2-train-with-lags/meta_model.pkl\", \"rb\") as f:\n    meta_model = pickle.load(f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:49.810750Z","iopub.execute_input":"2024-11-29T16:30:49.811185Z","iopub.status.idle":"2024-11-29T16:30:49.937310Z","shell.execute_reply.started":"2024-11-29T16:30:49.811148Z","shell.execute_reply":"2024-11-29T16:30:49.936126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CONFIG:\n    seed = 42\n    target_col = \"responder_6\"\n    feature_cols = [f\"feature_{idx:02d}\" for idx in range(79)]+ [f\"responder_{idx}_lag_1\" for idx in range(9)]\n    # feature_cols = [\"symbol_id\", \"time_id\"] + [f\"feature_{idx:02d}\" for idx in range(79)]+ [f\"responder_{idx}_lag_1\" for idx in range(9)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:49.938688Z","iopub.execute_input":"2024-11-29T16:30:49.939091Z","iopub.status.idle":"2024-11-29T16:30:49.947273Z","shell.execute_reply.started":"2024-11-29T16:30:49.939045Z","shell.execute_reply":"2024-11-29T16:30:49.946104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lags_ : pl.DataFrame | None = None\n# Replace this function with your inference code.\n# You can return either a Pandas or Polars dataframe, though Polars is recommended.\n# Each batch of predictions (except the very first) must be returned within 1 minute of the batch features being provided.\ndef predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"Make a prediction.\"\"\"\n    # All the responders from the previous day are passed in at time_id == 0. We save them in a global variable for access at every time_id.\n    # Use them as extra features, if you like.\n    global lags_\n    if lags is not None:\n        lags_ = lags\n\n    # Replace this section with your own predictions\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n\n    if not lags is None:\n        lags = lags.group_by([\"date_id\", \"symbol_id\"], maintain_order=True).last() # pick up last record of previous date\n        test = test.join(lags, on=[\"date_id\", \"symbol_id\"],  how=\"left\")\n    else:\n        test = test.with_columns(\n            ( pl.lit(0.0).alias(f'responder_{idx}_lag_1') for idx in range(9) )\n        )\n\n    # initialize preds\n    preds = np.zeros((test.shape[0],))\n\n    # xgb model\n    X = test[CONFIG.feature_cols].to_numpy()\n    X_dmatrix = xgb.DMatrix(X)\n    y_pred1 = xgb_model.predict(X_dmatrix)\n\n    # lightgbm model\n    y_pred2 = lgbm_model.predict(X, num_iteration=lgbm_model.best_iteration)\n\n    # meta model\n    # Combine predictions into a feature matrix\n    # stacked_features = np.column_stack((y_pred1, y_pred2))  # Shape: (n_samples, 2)\n\n    # Final prediction using the meta-model\n    # preds = meta_model.predict(stacked_features)\n\n    # simple average\n    preds = (y_pred1+y_pred2)/2\n    \n    print(f\"predict> preds.shape =\", preds.shape)\n    \n    predictions = \\\n    test.select('row_id').\\\n    with_columns(\n        pl.Series(\n            name   = 'responder_6', \n            values = np.clip(preds, a_min = -5, a_max = 5),\n            dtype  = pl.Float64,\n        )\n    )\n\n    if isinstance(predictions, pl.DataFrame):\n        assert predictions.columns == ['row_id', 'responder_6']\n    elif isinstance(predictions, pd.DataFrame):\n        assert (predictions.columns == ['row_id', 'responder_6']).all()\n    else:\n        raise TypeError('The predict function must return a DataFrame')\n    # Confirm has as many rows as the test data.\n    assert len(predictions) == len(test)\n\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:49.948950Z","iopub.execute_input":"2024-11-29T16:30:49.949410Z","iopub.status.idle":"2024-11-29T16:30:49.965838Z","shell.execute_reply.started":"2024-11-29T16:30:49.949347Z","shell.execute_reply":"2024-11-29T16:30:49.964367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        (\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/test.parquet',\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet',\n        )\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T16:30:49.967210Z","iopub.execute_input":"2024-11-29T16:30:49.967726Z","iopub.status.idle":"2024-11-29T16:30:50.465933Z","shell.execute_reply.started":"2024-11-29T16:30:49.967686Z","shell.execute_reply":"2024-11-29T16:30:50.464479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}