{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":9640394,"sourceType":"datasetVersion","datasetId":5882430},{"sourceId":9651408,"sourceType":"datasetVersion","datasetId":5887570},{"sourceId":201255000,"sourceType":"kernelVersion"},{"sourceId":201377683,"sourceType":"kernelVersion"}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!ls /kaggle/input/js-models","metadata":{"execution":{"iopub.status.busy":"2024-10-17T13:31:10.518292Z","iopub.execute_input":"2024-10-17T13:31:10.51858Z","iopub.status.idle":"2024-10-17T13:31:11.549509Z","shell.execute_reply.started":"2024-10-17T13:31:10.518547Z","shell.execute_reply":"2024-10-17T13:31:11.548228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **IMPORTS**","metadata":{}},{"cell_type":"markdown","source":"引用了之前的模型训练工作，加载并使用已经保存的模型进行预测，并将结果提交到Kaggle。\nIt references the previous model training work, loads and uses the saved model for predictions, and then submits the results to Kaggle.","metadata":{}},{"cell_type":"code","source":"%%time \n\n!pip install polars[gpu]==1.9.0 -q --no-index --find-links=/kaggle/input/janestreet2024-imports-v1/polars\n!pip install lightgbm==4.5.0 -q --no-index --find-links=/kaggle/input/janestreet2024-imports-v1/packages\n!pip install scikit-learn==1.5.2 -q --no-index --find-links=/kaggle/input/janestreet2024-imports-v1/packages\n\nexec(\n    open(\"/kaggle/input/janestreet2024-imports-v1/myimports.py\", \"r\"\n        ).read()\n)\n\nprint()","metadata":{"execution":{"iopub.status.busy":"2024-10-17T13:31:11.551946Z","iopub.execute_input":"2024-10-17T13:31:11.552799Z","iopub.status.idle":"2024-10-17T13:32:16.028483Z","shell.execute_reply.started":"2024-10-17T13:31:11.552749Z","shell.execute_reply":"2024-10-17T13:32:16.027523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **CONFIGURATION**","metadata":{}},{"cell_type":"code","source":"%%time\n\ntarget     = \"responder_6\"\nop_path    = f\"/kaggle/working\"\nip_path    = f\"/kaggle/input/janestreet2024-dataload-v1\"\nstate      = 42\nmethod     = \"CB1R\"\n","metadata":{"execution":{"iopub.status.busy":"2024-10-17T13:32:16.02975Z","iopub.execute_input":"2024-10-17T13:32:16.030337Z","iopub.status.idle":"2024-10-17T13:32:16.036157Z","shell.execute_reply.started":"2024-10-17T13:32:16.0303Z","shell.execute_reply":"2024-10-17T13:32:16.035203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **PREPROCESSING**\n\nWe load the models and feature lists here and prepare for inference in the next stsp <br>","metadata":{}},{"cell_type":"code","source":"%%time \n# This section defines the sel_cols variable, which is used to select specific feature columns. These column names typically correspond to various features in the dataset and will be used during the model prediction process.\n# 这一部分定义了sel_cols变量，用于选择特定的特征列。这些列名对应于数据集中的各个特征，将在模型预测过程中使用\nsel_cols = \\\n[\n'symbol_id', 'weight',\n'feature_00', 'feature_01', 'feature_02', 'feature_03', 'feature_04',\n'feature_05', 'feature_06', 'feature_07', 'feature_08', 'feature_09', 'feature_10',\n'feature_11', 'feature_12', 'feature_13', 'feature_14', 'feature_15', 'feature_16',\n'feature_17', 'feature_18', 'feature_19', 'feature_20', 'feature_21', 'feature_22',\n'feature_23', 'feature_24', 'feature_25', 'feature_26', 'feature_27', 'feature_28',\n'feature_29', 'feature_30', 'feature_31', 'feature_32', 'feature_33', 'feature_34',\n'feature_35', 'feature_36', 'feature_37', 'feature_38', 'feature_39', 'feature_40',\n'feature_41', 'feature_42', 'feature_43', 'feature_44', 'feature_45', 'feature_46',\n'feature_47', 'feature_48', 'feature_49', 'feature_50', 'feature_51', 'feature_52',\n'feature_53', 'feature_54', 'feature_55', 'feature_56', 'feature_57', 'feature_58',\n'feature_59', 'feature_60', 'feature_61', 'feature_62', 'feature_63', 'feature_64',\n'feature_65', 'feature_66', 'feature_67', 'feature_68', 'feature_69', 'feature_70',\n'feature_71', 'feature_72', 'feature_73', 'feature_74', 'feature_75', 'feature_76',\n'feature_77', 'feature_78'\n]\n# 先列出了/kaggle/input/js-models目录中的所有文件，随后使用sel_models列表筛选出感兴趣的模型文件（这里是LGBMV1_2.joblib）。通过set的交集操作，确保只选择指定的模型文件。\n# It first lists all files in the /kaggle/input/js-models directory and then uses the sel_models list to filter out the relevant model files (in this case, LGBMV1_2.joblib). The set intersection operation ensures that only the specified model files are selected.\nall_files  = sorted(os.listdir(f\"/kaggle/input/js-models\"))\nsel_models = [ \"LGBMV1_2.joblib\" ]\nall_files  = list(set(all_files).intersection(set(sel_models)))\n\nmodels = []\nfor file in sorted(all_files):\n    PrintColor(f\"---> Current model file - {file}\", color = Fore.CYAN)\n    fitted_model = \\\n    joblib.load(\n        os.path.join(f\"/kaggle/input/js-models\", file)\n    )[\"Online\"]\n        \n    models.append(fitted_model)\n    del fitted_model\n     \nPrintColor(f\"\\n---> Models for inference\\n\")\npprint(models)\n\nprint()\ncollect();","metadata":{"execution":{"iopub.status.busy":"2024-10-17T13:32:16.038152Z","iopub.execute_input":"2024-10-17T13:32:16.038508Z","iopub.status.idle":"2024-10-17T13:32:16.464712Z","shell.execute_reply.started":"2024-10-17T13:32:16.038476Z","shell.execute_reply":"2024-10-17T13:32:16.463977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **INFERENCE AND SUBMISSION**","metadata":{}},{"cell_type":"code","source":"%%time \n\nimport kaggle_evaluation.jane_street_inference_server\n\nlags_ : pl.DataFrame | None = None\n\ndef predict(\n    test: pl.DataFrame, \n    lags: pl.DataFrame | None\n) -> pl.DataFrame | pd.DataFrame:\n    \"This is the inference and submission function used to predict the test set for the competition\"\n\n    global lags_, models, sel_cols, target\n    \n    if lags is not None:\n        lags_ = lags\n        \n    test_preds = []\n    for model in tqdm(models):\n        test_preds.append(\n            model.predict(\n                test.select(pl.col(sel_cols)).to_pandas()\n            )\n        )\n        \n    test_preds = \\\n    np.mean(\n        np.stack(test_preds, axis=1), axis=1\n    )\n    \n    predictions = \\\n    test.select('row_id').\\\n    with_columns(\n        pl.Series(\n            name   = 'responder_6', \n            values = np.clip(test_preds, a_min = -5, a_max = 5),\n            dtype  = pl.Float64,\n        )\n    )  \n            \n    assert isinstance(predictions, pl.DataFrame | pd.DataFrame)\n    assert predictions.columns == ['row_id', 'responder_6']\n    assert len(predictions) == len(test)\n    return predictions","metadata":{"execution":{"iopub.status.busy":"2024-10-17T13:32:16.46804Z","iopub.execute_input":"2024-10-17T13:32:16.468763Z","iopub.status.idle":"2024-10-17T13:32:16.716714Z","shell.execute_reply.started":"2024-10-17T13:32:16.468709Z","shell.execute_reply":"2024-10-17T13:32:16.71578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inference_server = \\\nkaggle_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-17T13:32:16.717759Z","iopub.execute_input":"2024-10-17T13:32:16.718276Z","iopub.status.idle":"2024-10-17T13:32:17.133334Z","shell.execute_reply.started":"2024-10-17T13:32:16.718242Z","shell.execute_reply":"2024-10-17T13:32:17.132425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}