{"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":198847,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":169609,"modelId":191953},{"sourceId":207889,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":177231,"modelId":199538}],"dockerImageVersionId":30804,"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\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-26T09:11:53.070905Z","iopub.execute_input":"2024-12-26T09:11:53.071265Z","iopub.status.idle":"2024-12-26T09:11:54.288436Z","shell.execute_reply.started":"2024-12-26T09:11:53.071207Z","shell.execute_reply":"2024-12-26T09:11:54.287084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import polars as pl\nimport joblib\nimport kaggle_evaluation.jane_street_inference_server","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T09:11:54.291100Z","iopub.execute_input":"2024-12-26T09:11:54.291794Z","iopub.status.idle":"2024-12-26T09:11:54.876151Z","shell.execute_reply.started":"2024-12-26T09:11:54.291745Z","shell.execute_reply":"2024-12-26T09:11:54.874398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# cat_model = joblib.load(\"/kaggle/input/catboost/scikitlearn/iter10000_depth10_lr0.001/1/catboost_baseline\")\ncat_model_with_lag = joblib.load(\"/kaggle/input/catboost_with_lag/scikitlearn/10000iter_10depth_0.001lr/1/catboost_baseline_with_lag\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T09:11:54.877334Z","iopub.execute_input":"2024-12-26T09:11:54.879003Z","iopub.status.idle":"2024-12-26T09:11:58.390951Z","shell.execute_reply.started":"2024-12-26T09:11:54.878950Z","shell.execute_reply":"2024-12-26T09:11:58.389813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict_baseline(test):\n    cols = [f'feature_0{i}' if i<10 else f'feature_{i}' for i in range(79)] + [f\"responder_{i}_lag_1\" for i in range(9)]\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    test_preds = cat_model_with_lag.predict(test[cols].to_pandas().fillna(3).values)\n    predictions = predictions.with_columns(pl.Series('responder_6', test_preds.ravel()))\n    \n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-26T09:11:58.392637Z","iopub.execute_input":"2024-12-26T09:11:58.393060Z","iopub.status.idle":"2024-12-26T09:11:58.399479Z","shell.execute_reply.started":"2024-12-26T09:11:58.393017Z","shell.execute_reply":"2024-12-26T09:11:58.398322Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\n\nlags_ : pl.DataFrame | None = None\n\n\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        # lags = lags.group_by([\"date_id\", \"symbol_id\"], maintain_order=True).last() \n    test = test.join(lags_, on=[\"date_id\", \"time_id\", \"symbol_id\"], how=\"left\")\n    # else:\n    #     for idx in range(9):\n    #         test = test.with_columns(\n    #             pl.lit(0.0).alias(f'responder_{idx}_lag_1')\n    #         )\n            \n    # Replace this section with your own predictions\n    pds = predict_baseline(test).to_pandas()\n    predictions = test.select('row_id', pl.lit(0.0).alias('responder_6'))\n    pred = pds['responder_6'].to_numpy()\n    predictions = predictions.with_columns(pl.Series('responder_6', pred.ravel()))\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-12-26T09:11:58.401963Z","iopub.execute_input":"2024-12-26T09:11:58.402363Z","iopub.status.idle":"2024-12-26T09:11:58.417613Z","shell.execute_reply.started":"2024-12-26T09:11:58.402317Z","shell.execute_reply":"2024-12-26T09:11:58.416395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\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-12-26T09:11:58.422547Z","iopub.execute_input":"2024-12-26T09:11:58.423023Z","iopub.status.idle":"2024-12-26T09:11:58.833170Z","shell.execute_reply.started":"2024-12-26T09:11:58.422975Z","shell.execute_reply":"2024-12-26T09:11:58.831810Z"}},"outputs":[],"execution_count":null}]}