{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.14"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":9756372,"sourceType":"datasetVersion","datasetId":5973876},{"sourceId":10245336,"sourceType":"datasetVersion","datasetId":6336300},{"sourceId":10252299,"sourceType":"datasetVersion","datasetId":6341559},{"sourceId":203900450,"sourceType":"kernelVersion"}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true},"papermill":{"default_parameters":{},"duration":7.594014,"end_time":"2024-10-10T11:58:36.355301","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-10-10T11:58:28.761287","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Baseline notebooks:\n\n- Preprocessing : https://www.kaggle.com/code/motono0223/js24-preprocessing-create-lags\n- Training (Code only) : https://www.kaggle.com/code/motono0223/js24-train-gbdt-model-with-lags-singlemodel\n  - trained model : https://www.kaggle.com/datasets/motono0223/js24-trained-gbdt-model\n- Inference : **this notebook**  https://www.kaggle.com/code/motono0223/js24-inference-gbdt-with-lags-singlemodel\n- EDA(1) : https://www.kaggle.com/code/motono0223/eda-jane-street-real-time-market-data-forecasting\n- EDA(2) : https://www.kaggle.com/code/motono0223/eda-v2-jane-street-real-time-market-forecasting","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport polars as pl\nimport numpy as np\nimport os, gc\nfrom tqdm.auto import tqdm\nfrom matplotlib import pyplot as plt\nimport pickle\n\nfrom sklearn.metrics import r2_score\nfrom lightgbm import LGBMRegressor\nimport lightgbm as lgb\nfrom xgboost import XGBRegressor\nfrom catboost import CatBoostRegressor\nfrom sklearn.ensemble import VotingRegressor\n\nimport warnings\nwarnings.filterwarnings('ignore')\npd.options.display.max_columns = None\n\nimport kaggle_evaluation.jane_street_inference_server","metadata":{"execution":{"iopub.status.busy":"2024-12-29T14:41:49.996096Z","iopub.execute_input":"2024-12-29T14:41:49.997029Z","iopub.status.idle":"2024-12-29T14:41:54.848245Z","shell.execute_reply.started":"2024-12-29T14:41:49.996977Z","shell.execute_reply":"2024-12-29T14:41:54.847511Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Configurations","metadata":{}},{"cell_type":"markdown","source":"# Load preprocessed data (to calculate CV)","metadata":{}},{"cell_type":"code","source":"train = pl.scan_parquet(\n    f\"/kaggle/input/20241219-data/training.parquet\"\n).collect().to_pandas()","metadata":{"execution":{"iopub.status.busy":"2024-12-29T15:37:32.573434Z","iopub.execute_input":"2024-12-29T15:37:32.574232Z","iopub.status.idle":"2024-12-29T15:37:39.514744Z","shell.execute_reply.started":"2024-12-29T15:37:32.574196Z","shell.execute_reply":"2024-12-29T15:37:39.513947Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# CV Score","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-12-29T14:42:06.740412Z","iopub.execute_input":"2024-12-29T14:42:06.741259Z","iopub.status.idle":"2024-12-29T14:42:06.832529Z","shell.execute_reply.started":"2024-12-29T14:42:06.741222Z","shell.execute_reply":"2024-12-29T14:42:06.831556Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T13:17:23.353458Z","iopub.execute_input":"2024-12-29T13:17:23.353919Z","iopub.status.idle":"2024-12-29T13:17:23.426473Z","shell.execute_reply.started":"2024-12-29T13:17:23.353882Z","shell.execute_reply":"2024-12-29T13:17:23.425535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['feature_07']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:54:22.716525Z","iopub.execute_input":"2024-12-29T14:54:22.717152Z","iopub.status.idle":"2024-12-29T14:54:22.724034Z","shell.execute_reply.started":"2024-12-29T14:54:22.717102Z","shell.execute_reply":"2024-12-29T14:54:22.723079Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filtered_data = X_valid[(X_valid['feature_07'] >= -2) & (X_valid['feature_07'] <= 2)]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T03:33:00.905523Z","iopub.execute_input":"2024-12-29T03:33:00.906284Z","iopub.status.idle":"2024-12-29T03:33:01.052554Z","shell.execute_reply.started":"2024-12-29T03:33:00.906237Z","shell.execute_reply":"2024-12-29T03:33:01.051754Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filtered_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T03:33:02.011881Z","iopub.execute_input":"2024-12-29T03:33:02.012607Z","iopub.status.idle":"2024-12-29T03:33:02.085457Z","shell.execute_reply.started":"2024-12-29T03:33:02.01257Z","shell.execute_reply":"2024-12-29T03:33:02.084336Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:42:40.503411Z","iopub.execute_input":"2024-12-29T14:42:40.504277Z","iopub.status.idle":"2024-12-29T14:42:40.696085Z","shell.execute_reply.started":"2024-12-29T14:42:40.504242Z","shell.execute_reply":"2024-12-29T14:42:40.695187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 8))\nsns.boxplot(y=train['feature_07'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:54:34.950633Z","iopub.execute_input":"2024-12-29T14:54:34.951501Z","iopub.status.idle":"2024-12-29T14:54:36.565073Z","shell.execute_reply.started":"2024-12-29T14:54:34.951464Z","shell.execute_reply":"2024-12-29T14:54:36.56419Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **箱式图中可以看出数据集中在-4~4之间大部分在零左右**","metadata":{}},{"cell_type":"code","source":"train['feature_07'].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:54:53.355887Z","iopub.execute_input":"2024-12-29T14:54:53.356209Z","iopub.status.idle":"2024-12-29T14:54:53.549129Z","shell.execute_reply.started":"2024-12-29T14:54:53.35618Z","shell.execute_reply":"2024-12-29T14:54:53.548105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplt.hist(train['feature_07'], bins = 10)\nplt.xlabel('feature_07')\nplt.ylabel('Frequency')\nplt.title('Histogram of Column Name')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:56:32.350261Z","iopub.execute_input":"2024-12-29T14:56:32.350957Z","iopub.status.idle":"2024-12-29T14:56:32.690075Z","shell.execute_reply.started":"2024-12-29T14:56:32.350922Z","shell.execute_reply":"2024-12-29T14:56:32.689295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['feature_07'].skew()#piand","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:56:42.445963Z","iopub.execute_input":"2024-12-29T14:56:42.446877Z","iopub.status.idle":"2024-12-29T14:56:42.514364Z","shell.execute_reply.started":"2024-12-29T14:56:42.446841Z","shell.execute_reply":"2024-12-29T14:56:42.513501Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**'feature_07'数据的偏度值为-0.36682907，小于 0，表明该数据分布是负偏态（左偏态）。这表明在train['feature_07']的数据中，较小值那一侧有较长的尾巴。也就是说，相对较小的值出现的频率比较高，并且有一些更小的值使得数据分布向左（较小值方向）产生了不对称性。例如，如果train['feature_07']是某种产品的价格数据，负偏态可能表示大部分产品价格集中在较高的区间，只有少数价格较低的产品，并且这些较低价格产品使得价格分布呈现左偏态。**","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['feature_07'].kurtosis()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T14:58:17.690677Z","iopub.execute_input":"2024-12-29T14:58:17.691277Z","iopub.status.idle":"2024-12-29T14:58:17.762023Z","shell.execute_reply.started":"2024-12-29T14:58:17.691243Z","shell.execute_reply":"2024-12-29T14:58:17.761218Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**峰度值高可能表示大部分产品的质量数据集中在均值附近，但有一些产品的质量数据偏离均值很远，这些偏离的数据造成了尖峰厚尾的分布形态**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.scatterplot(x=train['feature_07'], y=train['responder_6_lag_1'])\nplt.title('Scatter Plot of feature_08 vs responder_6')\nplt.xlabel('feature_07')\nplt.ylabel('responder_6—_lag_1')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:03:03.234024Z","iopub.execute_input":"2024-12-29T15:03:03.234886Z","iopub.status.idle":"2024-12-29T15:03:14.494667Z","shell.execute_reply.started":"2024-12-29T15:03:03.234851Z","shell.execute_reply":"2024-12-29T15:03:14.493826Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**表明该数据分布是尖峰厚尾分**","metadata":{}},{"cell_type":"code","source":"x = train[\"feature_07\"]\ny = train[\"responder_6\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:10:03.869032Z","iopub.execute_input":"2024-12-29T15:10:03.869875Z","iopub.status.idle":"2024-12-29T15:10:03.873945Z","shell.execute_reply.started":"2024-12-29T15:10:03.869838Z","shell.execute_reply":"2024-12-29T15:10:03.872899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:09:46.910825Z","iopub.execute_input":"2024-12-29T15:09:46.911585Z","iopub.status.idle":"2024-12-29T15:09:46.918239Z","shell.execute_reply.started":"2024-12-29T15:09:46.91155Z","shell.execute_reply":"2024-12-29T15:09:46.917435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:10:06.574517Z","iopub.execute_input":"2024-12-29T15:10:06.574855Z","iopub.status.idle":"2024-12-29T15:10:06.582054Z","shell.execute_reply.started":"2024-12-29T15:10:06.574826Z","shell.execute_reply":"2024-12-29T15:10:06.581105Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.stats import spearmanr\n\n# 计算斯皮尔曼等级相关系数和p - 值\nspearman_corr, p_value_spearman = spearmanr(x, y)\nprint(\"斯皮尔曼相关系数:\", spearman_corr)\nprint(\"p - 值:\", p_value_spearman)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:10:08.959543Z","iopub.execute_input":"2024-12-29T15:10:08.959894Z","iopub.status.idle":"2024-12-29T15:10:10.702736Z","shell.execute_reply.started":"2024-12-29T15:10:08.959861Z","shell.execute_reply":"2024-12-29T15:10:10.701864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.stats import kendalltau\n\n# 计算肯德尔等级相关系数和p - 值\nkendall_corr, p_value_kendall = kendalltau(x, y)\nprint(\"肯德尔相关系数:\", kendall_corr)\nprint(\"p - 值:\", p_value_kendall)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:10:59.829156Z","iopub.execute_input":"2024-12-29T15:10:59.829837Z","iopub.status.idle":"2024-12-29T15:11:03.066214Z","shell.execute_reply.started":"2024-12-29T15:10:59.829805Z","shell.execute_reply":"2024-12-29T15:11:03.065095Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"从计算结果来看，两者之间几乎不存在单调关系。","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"log_transformed_feature = np.log1p(train['feature_07'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:12:19.773745Z","iopub.execute_input":"2024-12-29T15:12:19.774062Z","iopub.status.idle":"2024-12-29T15:12:19.797188Z","shell.execute_reply.started":"2024-12-29T15:12:19.774036Z","shell.execute_reply":"2024-12-29T15:12:19.796497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"log_transformed_feature","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:12:21.233499Z","iopub.execute_input":"2024-12-29T15:12:21.234266Z","iopub.status.idle":"2024-12-29T15:12:21.240772Z","shell.execute_reply.started":"2024-12-29T15:12:21.234231Z","shell.execute_reply":"2024-12-29T15:12:21.239863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"log_single_feature_df = pd.DataFrame(log_transformed_feature, columns=['feature_07'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:12:22.813585Z","iopub.execute_input":"2024-12-29T15:12:22.81393Z","iopub.status.idle":"2024-12-29T15:12:23.14706Z","shell.execute_reply.started":"2024-12-29T15:12:22.813898Z","shell.execute_reply":"2024-12-29T15:12:23.146297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"log_single_feature_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:12:26.453455Z","iopub.execute_input":"2024-12-29T15:12:26.454155Z","iopub.status.idle":"2024-12-29T15:12:26.469051Z","shell.execute_reply.started":"2024-12-29T15:12:26.454094Z","shell.execute_reply":"2024-12-29T15:12:26.468252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.hist(log_single_feature_df['feature_07'], bins=20, color='salmon', edgecolor='black')\n\nplt.xlabel('log_feature_07')\nplt.ylabel('Frequency')\nplt.grid(True)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:12:28.793787Z","iopub.execute_input":"2024-12-29T15:12:28.794513Z","iopub.status.idle":"2024-12-29T15:12:29.15848Z","shell.execute_reply.started":"2024-12-29T15:12:28.794475Z","shell.execute_reply":"2024-12-29T15:12:29.157589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"feature_07\"]=log_single_feature_df[\"feature_07\"]\ny = train[\"responder_6\"]","metadata":{"execution":{"iopub.status.busy":"2024-12-29T15:19:18.011299Z","iopub.execute_input":"2024-12-29T15:19:18.011657Z","iopub.status.idle":"2024-12-29T15:19:18.024891Z","shell.execute_reply.started":"2024-12-29T15:19:18.011625Z","shell.execute_reply":"2024-12-29T15:19:18.024178Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=train[\"feature_07\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:19:29.073909Z","iopub.execute_input":"2024-12-29T15:19:29.074275Z","iopub.status.idle":"2024-12-29T15:19:29.07855Z","shell.execute_reply.started":"2024-12-29T15:19:29.074244Z","shell.execute_reply":"2024-12-29T15:19:29.077673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:19:51.623564Z","iopub.execute_input":"2024-12-29T15:19:51.623902Z","iopub.status.idle":"2024-12-29T15:19:51.631047Z","shell.execute_reply.started":"2024-12-29T15:19:51.623873Z","shell.execute_reply":"2024-12-29T15:19:51.630157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:19:53.423723Z","iopub.execute_input":"2024-12-29T15:19:53.424329Z","iopub.status.idle":"2024-12-29T15:19:53.43102Z","shell.execute_reply.started":"2024-12-29T15:19:53.424295Z","shell.execute_reply":"2024-12-29T15:19:53.430202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:20:29.788535Z","iopub.execute_input":"2024-12-29T15:20:29.789182Z","iopub.status.idle":"2024-12-29T15:20:29.859362Z","shell.execute_reply.started":"2024-12-29T15:20:29.789115Z","shell.execute_reply":"2024-12-29T15:20:29.858525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.scatterplot(x=train['feature_07'], y=train['responder_6_lag_1'])\nplt.title('Scatter Plot of feature_08 vs responder_6')\nplt.xlabel('feature_07')\nplt.ylabel('responder_6—_lag_1')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:21:05.333916Z","iopub.execute_input":"2024-12-29T15:21:05.334278Z","iopub.status.idle":"2024-12-29T15:21:15.619856Z","shell.execute_reply.started":"2024-12-29T15:21:05.334244Z","shell.execute_reply":"2024-12-29T15:21:15.618999Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"感谢这个[notebook](https://www.kaggle.com/code/ayoubchouikha/jane-street-eda-feature-understanding/notebook)给我灵感","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# 假设train是你的原始数据集DataFrame\n# 使用列表推导选择除responder_6之外要删除的responder列\ncols_to_drop = [col for col in train.columns if col.startswith('responder') and col!= 'responder_6']\ndata = train.drop(columns=cols_to_drop)\n\n# Set up the figure size for a large heatmap\nplt.figure(figsize=(20, 16))\n\n# Generate the heatmap for the dataset correlations\nsns.heatmap(data.corr(), annot=False, cmap='coolwarm', linewidths=0.5)\n\n# Add title\nplt.title('Heatmap of Correlations for All Columns (Including responder_6)')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:45:32.10854Z","iopub.execute_input":"2024-12-29T15:45:32.109233Z","iopub.status.idle":"2024-12-29T15:47:33.036004Z","shell.execute_reply.started":"2024-12-29T15:45:32.109197Z","shell.execute_reply":"2024-12-29T15:47:33.035099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"feature = \"feature_07\"\n\n# Let us now try to see how the responders move for this symbol\n\nfirst_symbol= 0\nlast_symbol=4\n\nfig, ax = plt.subplots(nrows=last_symbol - first_symbol, \n                       ncols=1, \n                       sharex=True)\n\ncurrent_symbol = first_symbol\nfor row in ax:\n    row.plot(train[train[\"symbol_id\"]==current_symbol][feature])\n    row.set_title(f\"Behavior of {feature} for symbol: {current_symbol}\")\n    current_symbol += 1\n\n    if current_symbol > last_symbol:\n        break\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:37:43.460493Z","iopub.execute_input":"2024-12-29T15:37:43.461048Z","iopub.status.idle":"2024-12-29T15:37:44.816563Z","shell.execute_reply.started":"2024-12-29T15:37:43.461013Z","shell.execute_reply":"2024-12-29T15:37:44.815496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train[['feature_07', 'feature_05']]\ny =train['responder_6']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:56:41.718623Z","iopub.execute_input":"2024-12-29T15:56:41.71929Z","iopub.status.idle":"2024-12-29T15:56:41.745226Z","shell.execute_reply.started":"2024-12-29T15:56:41.719255Z","shell.execute_reply":"2024-12-29T15:56:41.744496Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['interaction'] = train['feature_07'] * train['feature_05']\nX = train[['feature_07', 'feature_05', 'interaction']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:57:29.153971Z","iopub.execute_input":"2024-12-29T15:57:29.154669Z","iopub.status.idle":"2024-12-29T15:57:29.218427Z","shell.execute_reply.started":"2024-12-29T15:57:29.154632Z","shell.execute_reply":"2024-12-29T15:57:29.217465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import statsmodels.api as sm\nX = sm.add_constant(X)  # 添加常数项\nmodel = sm.OLS(y, X).fit()\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:57:42.493777Z","iopub.execute_input":"2024-12-29T15:57:42.49442Z","iopub.status.idle":"2024-12-29T15:57:46.2766Z","shell.execute_reply.started":"2024-12-29T15:57:42.494383Z","shell.execute_reply":"2024-12-29T15:57:46.273902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)\nmodel = LinearRegression().fit(X_train, y_train)\ny_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T15:58:03.99446Z","iopub.execute_input":"2024-12-29T15:58:03.995839Z","iopub.status.idle":"2024-12-29T15:58:05.984167Z","shell.execute_reply.started":"2024-12-29T15:58:03.995803Z","shell.execute_reply":"2024-12-29T15:58:05.982277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import statsmodels.api as sm\nimport pandas as pd\n\n\nX1 = train[['feature_07', 'feature_05']]\nX1 = sm.add_constant(X1)\ny = train['responder_6']\nmodel1 = sm.OLS(y, X1).fit()\n\ndata['interaction'] = data['feature_07'] * data['feature_05']\nX2 = train[['feature_07', 'feature_05', 'interaction']]\nX2 = sm.add_constant(X2)\nmodel2 = sm.OLS(y, X2).fit()\n\nprint(\"Model 1 R - squared:\", model1.rsquared)\nprint(\"Model 2 R - squared:\", model2.rsquared)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-29T16:01:15.795675Z","iopub.execute_input":"2024-12-29T16:01:15.796612Z","iopub.status.idle":"2024-12-29T16:01:18.700039Z","shell.execute_reply.started":"2024-12-29T16:01:15.79657Z","shell.execute_reply":"2024-12-29T16:01:18.698837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"模型2大于模型 1 的，说明加入交互项后，模型对response_6的变异解释能力有所提高。","metadata":{}},{"cell_type":"code","source":"lags_ : pl.DataFrame | None = None\n    \ndefFutureWarningict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    global lags_\n    if lags is not None:\n        lags_ = lags\n\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    symbol_ids = test.select('symbol_id').to_numpy()[:, 0]\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    preds = np.zeros((test.shape[0],))\n    for i, model in enumerate(tqdm(models)):\n        preds += model.predict(test[CONFIG.feature_cols].to_pandas()) / len(models)\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    # The predict function must return a DataFrame\n    assert isinstance(predictions, pl.DataFrame | pd.DataFrame)\n    # with columns 'row_id', 'responer_6'\n    assert list(predictions.columns) == ['row_id', 'responder_6']\n    # and as many rows as the test data.\n    assert len(predictions) == len(test)\n\n    return predictions","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.018344,"end_time":"2024-10-10T11:58:33.59684","exception":false,"start_time":"2024-10-10T11:58:33.578496","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"When your notebook is run on the hidden test set, inference_server.serve must be called within 15 minutes of the notebook starting or the gateway will throw an error. If you need more than 15 minutes to load your model you can do so during the very first `predict` call, which does not have the usual 10 minute response deadline.","metadata":{"papermill":{"duration":0.002521,"end_time":"2024-10-10T11:58:33.6023","exception":false,"start_time":"2024-10-10T11:58:33.599779","status":"completed"},"tags":[]}},{"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-realtime-marketdata-forecasting/test.parquet',\n            '/kaggle/input/jane-street-realtime-marketdata-forecasting/lags.parquet',\n        )\n    )","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":2.225871,"end_time":"2024-10-10T11:58:35.830964","exception":false,"start_time":"2024-10-10T11:58:33.605093","status":"completed"},"tags":[],"trusted":true},"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}]}