{"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":149077,"sourceType":"modelInstanceVersion","modelInstanceId":126535,"modelId":149503}],"dockerImageVersionId":30786,"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","execution":{"iopub.status.busy":"2024-10-28T11:47:08.481183Z","iopub.execute_input":"2024-10-28T11:47:08.481636Z","iopub.status.idle":"2024-10-28T11:47:09.670149Z","shell.execute_reply.started":"2024-10-28T11:47:08.481592Z","shell.execute_reply":"2024-10-28T11:47:09.668725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import kaggle_evaluation.jane_street_inference_server","metadata":{"execution":{"iopub.status.busy":"2024-10-28T11:48:24.131255Z","iopub.execute_input":"2024-10-28T11:48:24.131723Z","iopub.status.idle":"2024-10-28T11:48:24.348308Z","shell.execute_reply.started":"2024-10-28T11:48:24.131678Z","shell.execute_reply":"2024-10-28T11:48:24.347240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\n# Save the model\nimport joblib\n\nmodel = joblib.load('/kaggle/input/js_v1/scikitlearn/default/1/final_model.joblib')\nrfe = joblib.load('/kaggle/input/js_v1/scikitlearn/default/1/rfe_selector.joblib')  # If you haven't saved this, you'll need to retrain","metadata":{"execution":{"iopub.status.busy":"2024-10-28T11:47:52.912015Z","iopub.execute_input":"2024-10-28T11:47:52.912649Z","iopub.status.idle":"2024-10-28T11:47:55.078720Z","shell.execute_reply.started":"2024-10-28T11:47:52.912604Z","shell.execute_reply":"2024-10-28T11:47:55.077300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\nimport numpy as np\nfrom sklearn.preprocessing import PolynomialFeatures\n\ndef predict(data, lags):\n    # Define features\n    feature_nums = [4,6,15,16,17,36,45,56]\n    features = [f'feature_{x:02}' for x in feature_nums]\n    \n    # Convert Polars to Pandas for preprocessing\n    df = data.to_pandas()\n    \n    # Create lag features\n    for feature in features:\n        df[f'{feature}_lag1'] = df.groupby('symbol_id')[feature].shift(1)\n    \n    # Fill NaN values\n    df = df.fillna(3)  # Using 3 as per your original code\n    \n    # Create polynomial features\n    poly = PolynomialFeatures(degree=2, include_bias=False)\n    poly_features = poly.fit_transform(df[features + [f'{f}_lag1' for f in features]])\n    \n    # Combine features\n    X = np.hstack([df[features + [f'{f}_lag1' for f in features]].values, poly_features])\n    \n    # Transform features using RFE\n    X_selected = rfe.transform(X)\n    \n    # Make predictions\n    predictions = data.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    \n    # Get model predictions\n    test_preds = model.predict(X_selected)\n    \n    # Update predictions column\n    predictions = predictions.with_columns(pl.Series('responder_6', test_preds.ravel()))\n    \n    return predictions\n\ninference_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":{"execution":{"iopub.status.busy":"2024-10-28T11:48:27.034541Z","iopub.execute_input":"2024-10-28T11:48:27.035240Z","iopub.status.idle":"2024-10-28T11:48:27.417488Z","shell.execute_reply.started":"2024-10-28T11:48:27.035191Z","shell.execute_reply":"2024-10-28T11:48:27.416337Z"},"trusted":true},"execution_count":null,"outputs":[]}]}