{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport sys\nfrom decimal import ROUND_HALF_UP, Decimal\nimport warnings\nfrom tqdm import tqdm\nwarnings.filterwarnings(\"ignore\")\npd.set_option('display.max_columns',None)\npd.set_option('display.max_rows',None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-06T10:59:01.979569Z","iopub.execute_input":"2023-05-06T10:59:01.979912Z","iopub.status.idle":"2023-05-06T10:59:02.015474Z","shell.execute_reply.started":"2023-05-06T10:59:01.979885Z","shell.execute_reply":"2023-05-06T10:59:02.014702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices = pd.read_csv('../input/g-research-crypto-forecasting/train.csv')\nsupplemental_prices = pd.read_csv(\"../input/g-research-crypto-forecasting/supplemental_train.csv\")\ndetail = pd.read_csv(\"../input/g-research-crypto-forecasting/asset_details.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-06T10:59:02.017513Z","iopub.execute_input":"2023-05-06T10:59:02.017993Z","iopub.status.idle":"2023-05-06T11:00:36.016675Z","shell.execute_reply.started":"2023-05-06T10:59:02.017956Z","shell.execute_reply":"2023-05-06T11:00:36.013349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:36.02052Z","iopub.execute_input":"2023-05-06T11:00:36.020948Z","iopub.status.idle":"2023-05-06T11:00:36.084735Z","shell.execute_reply.started":"2023-05-06T11:00:36.020922Z","shell.execute_reply":"2023-05-06T11:00:36.082157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:36.09186Z","iopub.execute_input":"2023-05-06T11:00:36.094934Z","iopub.status.idle":"2023-05-06T11:00:36.10936Z","shell.execute_reply.started":"2023-05-06T11:00:36.094831Z","shell.execute_reply":"2023-05-06T11:00:36.106748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:36.143703Z","iopub.execute_input":"2023-05-06T11:00:36.144234Z","iopub.status.idle":"2023-05-06T11:00:36.199539Z","shell.execute_reply.started":"2023-05-06T11:00:36.14421Z","shell.execute_reply":"2023-05-06T11:00:36.197028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#查看数据时间跨度\ndef unique_df(df):\n    \n    n_dates = df['timestamp'].nunique()\n    date_min, date_max = df['timestamp'].astype('datetime64[s]').min(), df['timestamp'].astype('datetime64[s]').max()\n    print(f\"日期数量及范围: {n_dates} 【{date_min} --{date_max}】\\n\")","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:36.204209Z","iopub.execute_input":"2023-05-06T11:00:36.205473Z","iopub.status.idle":"2023-05-06T11:00:36.220863Z","shell.execute_reply.started":"2023-05-06T11:00:36.205338Z","shell.execute_reply":"2023-05-06T11:00:36.21873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_df(prices)\nunique_df(supplemental_prices)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:36.226518Z","iopub.execute_input":"2023-05-06T11:00:36.22802Z","iopub.status.idle":"2023-05-06T11:00:43.420797Z","shell.execute_reply.started":"2023-05-06T11:00:36.227803Z","shell.execute_reply":"2023-05-06T11:00:43.419586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:43.424126Z","iopub.execute_input":"2023-05-06T11:00:43.424533Z","iopub.status.idle":"2023-05-06T11:00:43.819271Z","shell.execute_reply.started":"2023-05-06T11:00:43.424507Z","shell.execute_reply":"2023-05-06T11:00:43.818235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices = pd.concat([prices,supplemental_prices], axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:43.82054Z","iopub.execute_input":"2023-05-06T11:00:43.820927Z","iopub.status.idle":"2023-05-06T11:00:48.23657Z","shell.execute_reply.started":"2023-05-06T11:00:43.8209Z","shell.execute_reply":"2023-05-06T11:00:48.234689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices.drop(prices[prices['Target'].isnull()].index, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:00:48.239292Z","iopub.execute_input":"2023-05-06T11:00:48.240575Z","iopub.status.idle":"2023-05-06T11:01:08.363252Z","shell.execute_reply.started":"2023-05-06T11:00:48.240531Z","shell.execute_reply":"2023-05-06T11:01:08.362012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prices=prices.sort_values(['timestamp','Asset_ID'])","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:01:08.364722Z","iopub.execute_input":"2023-05-06T11:01:08.365043Z","iopub.status.idle":"2023-05-06T11:01:18.980177Z","shell.execute_reply.started":"2023-05-06T11:01:08.365014Z","shell.execute_reply":"2023-05-06T11:01:18.978996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.model_selection import TimeSeriesSplit\nimport gc\n\ntscv_fold = TimeSeriesSplit(n_splits=5, gap=8000)\n\n\nX = prices.drop(['Target'], axis=1)\ny = prices['Target'].to_numpy()\n\n\nfor fold,(train_index, eval_index) in enumerate(tscv_fold.split(X, y)):\n    \n    X_train_fold,y_train_fold = X.iloc[train_index,:], y[train_index]\n        \n    X_val_fold, y_val_fold = X.iloc[eval_index,:], y[eval_index]\n    \n    print(\"训练集日期范围: {} -- {}\".format(X_train_fold.timestamp.astype('datetime64[s]').min(),X_train_fold.timestamp.astype('datetime64[s]').max()))\n    print(\"验证集日期范围: {} -- {}\".format(X_val_fold.timestamp.astype('datetime64[s]').min(),X_val_fold.timestamp.astype('datetime64[s]').max()))\n    \n    # 创建训练集和测试集\n    train_set = lgb.Dataset(X_train_fold.drop(['timestamp'], axis=1), label=y_train_fold)\n        \n    eval_set = lgb.Dataset(X_val_fold.drop(['timestamp'], axis=1), label=y_val_fold)\n\n    params = {\n        'learning_rate': 0.1,\n        'objective': 'regression_l1',\n        'boosting': 'gbdt',\n        'random_state': 13,\n        'verbosity': 1,\n        'metric': 'mae',\n        'n_jobs': -1,\n        'force_col_wise':True\n     }\n    \n    model = lgb.train(params = params, \n                train_set = train_set, \n                valid_sets = [train_set, eval_set], \n                num_boost_round = 600, \n                early_stopping_rounds=10)  \n\n    y_pred = model.predict(X_val_fold.drop(['timestamp'], axis=1))\n    \n    \n    gc.collect()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:01:18.981815Z","iopub.execute_input":"2023-05-06T11:01:18.982484Z","iopub.status.idle":"2023-05-06T11:03:27.515525Z","shell.execute_reply.started":"2023-05-06T11:01:18.982449Z","shell.execute_reply":"2023-05-06T11:03:27.514675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:04:13.619882Z","iopub.execute_input":"2023-05-06T11:04:13.620378Z","iopub.status.idle":"2023-05-06T11:04:13.651022Z","shell.execute_reply.started":"2023-05-06T11:04:13.620347Z","shell.execute_reply":"2023-05-06T11:04:13.650027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto\nenv = gresearch_crypto.make_env()   # initialize the environment\niter_test = env.iter_test()    # an iterator which loops over the test set and sample submission\n","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:03:27.516738Z","iopub.execute_input":"2023-05-06T11:03:27.517647Z","iopub.status.idle":"2023-05-06T11:03:27.744158Z","shell.execute_reply.started":"2023-05-06T11:03:27.517614Z","shell.execute_reply":"2023-05-06T11:03:27.742772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, (df_test, df_pred) in enumerate(iter_test):\n   \n    df_test.sort_values(['timestamp','row_id'],ascending=True,inplace=True)\n    y_pred = model.predict(df_test.drop(['timestamp'], axis=1))\n    df_pred.loc[\"Target\"] = y_pred\n        \n    if i == 0:\n        display(df_pred)\n\n    # Send submissions\n    env.predict(df_pred)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T11:03:27.745323Z","iopub.status.idle":"2023-05-06T11:03:27.745729Z","shell.execute_reply.started":"2023-05-06T11:03:27.745534Z","shell.execute_reply":"2023-05-06T11:03:27.745552Z"},"trusted":true},"execution_count":null,"outputs":[]}]}