{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":84493,"databundleVersionId":9871156},{"sourceType":"datasetVersion","sourceId":10245336,"datasetId":6336300,"databundleVersionId":10541613}],"dockerImageVersionId":30822,"isInternetEnabled":true,"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)\nfrom sklearn.metrics import r2_score\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-28T15:34:15.363710Z","iopub.execute_input":"2024-12-28T15:34:15.364218Z","iopub.status.idle":"2024-12-28T15:34:17.252870Z","shell.execute_reply.started":"2024-12-28T15:34:15.364178Z","shell.execute_reply":"2024-12-28T15:34:17.251724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import kagglehub\n\n# Download latest version\npath = kagglehub.dataset_download(\"bruceqdu/20241219-data\")\n\nprint(\"Path to dataset files:\", path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:34:22.317726Z","iopub.execute_input":"2024-12-28T15:34:22.318425Z","iopub.status.idle":"2024-12-28T15:34:22.927056Z","shell.execute_reply.started":"2024-12-28T15:34:22.318380Z","shell.execute_reply":"2024-12-28T15:34:22.926086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import r2_score\nimport polars as pl","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:35:50.544450Z","iopub.execute_input":"2024-12-28T15:35:50.544988Z","iopub.status.idle":"2024-12-28T15:35:50.830492Z","shell.execute_reply.started":"2024-12-28T15:35:50.544929Z","shell.execute_reply":"2024-12-28T15:35:50.829410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pl.scan_parquet(\n    f\"/kaggle/input/20241219-data/training.parquet\"\n).collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:35:53.349366Z","iopub.execute_input":"2024-12-28T15:35:53.349823Z","iopub.status.idle":"2024-12-28T15:36:03.269313Z","shell.execute_reply.started":"2024-12-28T15:35:53.349786Z","shell.execute_reply":"2024-12-28T15:36:03.268196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.to_pandas()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:36:38.914246Z","iopub.execute_input":"2024-12-28T15:36:38.914643Z","iopub.status.idle":"2024-12-28T15:36:45.374622Z","shell.execute_reply.started":"2024-12-28T15:36:38.914615Z","shell.execute_reply":"2024-12-28T15:36:45.373556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:36:48.331503Z","iopub.execute_input":"2024-12-28T15:36:48.332116Z","iopub.status.idle":"2024-12-28T15:36:49.906892Z","shell.execute_reply.started":"2024-12-28T15:36:48.332078Z","shell.execute_reply":"2024-12-28T15:36:49.902963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:37:57.094297Z","iopub.execute_input":"2024-12-28T15:37:57.094834Z","iopub.status.idle":"2024-12-28T15:37:57.105138Z","shell.execute_reply.started":"2024-12-28T15:37:57.094791Z","shell.execute_reply":"2024-12-28T15:37:57.103600Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 查看 feature_18 的分布\n","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nplt.hist(train['feature_18'], bins=30)\nplt.xlabel('Feature 18')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:46:15.290603Z","iopub.execute_input":"2024-12-28T15:46:15.291539Z","iopub.status.idle":"2024-12-28T15:46:15.664221Z","shell.execute_reply.started":"2024-12-28T15:46:15.291492Z","shell.execute_reply":"2024-12-28T15:46:15.663051Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#  对特征18进行时间序列分解","metadata":{}},{"cell_type":"code","source":"from statsmodels.tsa.seasonal import seasonal_decompose\ngrouped = train.groupby(['symbol_id', 'time_id'])['feature_18'].mean().reset_index()\ntime_series = grouped.groupby('time_id')['feature_18'].mean()\ndecomposition = seasonal_decompose(time_series, model='additive', period=1) \ndecomposition.plot()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:41:42.692519Z","iopub.execute_input":"2024-12-28T15:41:42.693055Z","iopub.status.idle":"2024-12-28T15:41:43.989260Z","shell.execute_reply.started":"2024-12-28T15:41:42.693013Z","shell.execute_reply":"2024-12-28T15:41:43.988028Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 比较不同 symbol_id 下 feature_18 的分布","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nsns.boxplot(x='symbol_id', y='feature_18', data=train)\nplt.xlabel('Symbol ID')\nplt.ylabel('Feature 18')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:43:39.057967Z","iopub.execute_input":"2024-12-28T15:43:39.058526Z","iopub.status.idle":"2024-12-28T15:43:41.168117Z","shell.execute_reply.started":"2024-12-28T15:43:39.058489Z","shell.execute_reply":"2024-12-28T15:43:41.166817Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nplt.hist(train['feature_16'], bins=30)\nplt.title('Histogram of Feature 16')\nplt.xlabel('Feature 16')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:47:48.875951Z","iopub.execute_input":"2024-12-28T15:47:48.876462Z","iopub.status.idle":"2024-12-28T15:47:50.235579Z","shell.execute_reply.started":"2024-12-28T15:47:48.876430Z","shell.execute_reply":"2024-12-28T15:47:50.234163Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 特征18与特征17的交互","metadata":{}},{"cell_type":"code","source":"#两者相加取平均值\ntrain['feature_18_17_avg'] = (train['feature_18'] + train['feature_17']) / 2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:52:16.883608Z","iopub.execute_input":"2024-12-28T15:52:16.884166Z","iopub.status.idle":"2024-12-28T15:52:16.915853Z","shell.execute_reply.started":"2024-12-28T15:52:16.884126Z","shell.execute_reply":"2024-12-28T15:52:16.914556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:52:22.412886Z","iopub.execute_input":"2024-12-28T15:52:22.413393Z","iopub.status.idle":"2024-12-28T15:52:24.659343Z","shell.execute_reply.started":"2024-12-28T15:52:22.413351Z","shell.execute_reply":"2024-12-28T15:52:24.658192Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 新特征值的直方图","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nplt.hist(train['feature_18_17_avg'], bins=30, color='skyblue', edgecolor='black')\nplt.xlabel('Averaged Interaction Value')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:53:03.727866Z","iopub.execute_input":"2024-12-28T15:53:03.728409Z","iopub.status.idle":"2024-12-28T15:53:04.106080Z","shell.execute_reply.started":"2024-12-28T15:53:03.728374Z","shell.execute_reply":"2024-12-28T15:53:04.104877Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 每个 symbol_id 对应的 feature_19 平均值随 date_id 变化的折线图","metadata":{}},{"cell_type":"code","source":"import matplotlib.ticker as ticker\ngrouped = train.groupby(['symbol_id', 'date_id'])['feature_18_17_avg'].mean().reset_index()\n\nfig = plt.figure(figsize=(15, 100))\n\nfor symbol in grouped['symbol_id'].unique():\n    symbol_data = grouped[grouped['symbol_id'] == symbol]\n    \n    ax = fig.add_subplot(39,1,symbol+1)\n\n    ax.plot(symbol_data['date_id'], symbol_data['feature_18_17_avg'], marker='o', linestyle='-')\n    ax.set_xlabel('Date')\n    ax.set_ylabel('feature_18_17_avg Average')\n\n    ax.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-28T15:55:39.282769Z","iopub.execute_input":"2024-12-28T15:55:39.283294Z","iopub.status.idle":"2024-12-28T15:55:45.396130Z","shell.execute_reply.started":"2024-12-28T15:55:39.283247Z","shell.execute_reply":"2024-12-28T15:55:45.394031Z"}},"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},{"cell_type":"code","source":"","metadata":{"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},{"cell_type":"code","source":"","metadata":{"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},{"cell_type":"code","source":"","metadata":{"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},{"cell_type":"code","source":"","metadata":{"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}]}