{"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":"# 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":"2021-12-05T14:12:18.076362Z","iopub.execute_input":"2021-12-05T14:12:18.077222Z","iopub.status.idle":"2021-12-05T14:12:18.090854Z","shell.execute_reply.started":"2021-12-05T14:12:18.077164Z","shell.execute_reply":"2021-12-05T14:12:18.089824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns;sns.set()\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objects as go\nimport plotly.express as px\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import cm\nfrom pandas import read_csv, set_option\nfrom sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer,RobustScaler\nfrom sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.svm import SVC\nfrom sklearn.neural_network import MLPClassifier\nfrom catboost import CatBoostClassifier,CatBoostRegressor\nfrom sklearn.ensemble import RandomForestClassifier,RandomForestRegressor\nfrom sklearn.feature_selection import SelectKBest,f_regression\nfrom xgboost import plot_importance,XGBClassifier,XGBRegressor\nfrom sklearn.model_selection import learning_curve\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier, ExtraTreesClassifier\nfrom sklearn.metrics import classification_report, confusion_matrix, accuracy_score\nfrom sklearn import preprocessing\nimport shap\n\nfrom sklearn.decomposition import PCA\nfrom sklearn.decomposition import SparsePCA\nfrom sklearn.decomposition import KernelPCA\nfrom sklearn.decomposition import IncrementalPCA\nfrom sklearn.decomposition import TruncatedSVD\nfrom sklearn.decomposition import MiniBatchDictionaryLearning\nfrom sklearn.decomposition import FastICA\nfrom sklearn.manifold import Isomap\nfrom sklearn.manifold import MDS\nfrom sklearn.manifold import LocallyLinearEmbedding\nfrom sklearn.manifold import TSNE\nfrom sklearn.random_projection import GaussianRandomProjection\nfrom sklearn.random_projection import SparseRandomProjection\n\n# for dirname, _, filenames in os.walk('/kaggle/'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n        \nimport time\nimport warnings\nwarnings.filterwarnings('ignore')\nsns.set(style='whitegrid')\n%matplotlib inline","metadata":{"_kg_hide-input":true,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2021-12-05T14:12:18.093459Z","iopub.execute_input":"2021-12-05T14:12:18.093851Z","iopub.status.idle":"2021-12-05T14:12:21.396733Z","shell.execute_reply.started":"2021-12-05T14:12:18.093806Z","shell.execute_reply":"2021-12-05T14:12:21.395806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/example_sample_submission.csv')\nsample_sub.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:12:21.398236Z","iopub.execute_input":"2021-12-05T14:12:21.398569Z","iopub.status.idle":"2021-12-05T14:12:21.417187Z","shell.execute_reply.started":"2021-12-05T14:12:21.398526Z","shell.execute_reply":"2021-12-05T14:12:21.41632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv')\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:12:21.4194Z","iopub.execute_input":"2021-12-05T14:12:21.419943Z","iopub.status.idle":"2021-12-05T14:12:53.189338Z","shell.execute_reply.started":"2021-12-05T14:12:21.419897Z","shell.execute_reply":"2021-12-05T14:12:53.188494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## About the dataset\n\nThis dataset contains information on historic trades for several cryptoassets, such as Bitcoin and Ethereum. Your challenge is to predict their future returns.\n\n* timestamp - A timestamp for the minute covered by the row.\n* Asset_ID - An ID code for the cryptoasset.\n* Count - The number of trades that took place this minute.\n* Open - The USD price at the beginning of the minute.\n* High - The highest USD price during the minute.\n* Low - The lowest USD price during the minute.\n* Close - The USD price at the end of the minute.\n* Volume - The number of cryptoasset units traded during the minute.\n* VWAP - The volume weighted average price for the minute.\n* Target - 15 minute residualized returns","metadata":{}},{"cell_type":"markdown","source":"# Data cleaning ","metadata":{}},{"cell_type":"code","source":"!pip install datacleaner\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-05T14:12:53.190592Z","iopub.execute_input":"2021-12-05T14:12:53.190856Z","iopub.status.idle":"2021-12-05T14:13:01.847349Z","shell.execute_reply.started":"2021-12-05T14:12:53.190811Z","shell.execute_reply":"2021-12-05T14:13:01.846475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datacleaner import autoclean\ncc = autoclean(df)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:01.84881Z","iopub.execute_input":"2021-12-05T14:13:01.849103Z","iopub.status.idle":"2021-12-05T14:13:04.985937Z","shell.execute_reply.started":"2021-12-05T14:13:01.849063Z","shell.execute_reply":"2021-12-05T14:13:04.98501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cc.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:04.987304Z","iopub.execute_input":"2021-12-05T14:13:04.987551Z","iopub.status.idle":"2021-12-05T14:13:05.004639Z","shell.execute_reply.started":"2021-12-05T14:13:04.987521Z","shell.execute_reply":"2021-12-05T14:13:05.003728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cc['timestamp'] = pd.to_datetime(cc['timestamp'])\ncc['timestamp']","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:05.006134Z","iopub.execute_input":"2021-12-05T14:13:05.006372Z","iopub.status.idle":"2021-12-05T14:13:06.861962Z","shell.execute_reply.started":"2021-12-05T14:13:05.006343Z","shell.execute_reply":"2021-12-05T14:13:06.861014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cc.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:06.86529Z","iopub.execute_input":"2021-12-05T14:13:06.865966Z","iopub.status.idle":"2021-12-05T14:13:06.871861Z","shell.execute_reply.started":"2021-12-05T14:13:06.865919Z","shell.execute_reply":"2021-12-05T14:13:06.870872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize the target values with increase in time","metadata":{}},{"cell_type":"code","source":"# cc.set_index(\"timestamp\").Target.plot(figsize=(14,7), title=\"Bitcoin Target\")","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:06.873258Z","iopub.execute_input":"2021-12-05T14:13:06.873559Z","iopub.status.idle":"2021-12-05T14:13:06.881657Z","shell.execute_reply.started":"2021-12-05T14:13:06.873516Z","shell.execute_reply":"2021-12-05T14:13:06.880997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check for missing values","metadata":{}},{"cell_type":"code","source":"cc.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:06.883511Z","iopub.execute_input":"2021-12-05T14:13:06.883738Z","iopub.status.idle":"2021-12-05T14:13:07.373133Z","shell.execute_reply.started":"2021-12-05T14:13:06.883712Z","shell.execute_reply":"2021-12-05T14:13:07.372063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cc.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:07.374609Z","iopub.execute_input":"2021-12-05T14:13:07.374875Z","iopub.status.idle":"2021-12-05T14:13:07.84511Z","shell.execute_reply.started":"2021-12-05T14:13:07.374844Z","shell.execute_reply":"2021-12-05T14:13:07.844047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_date = (str(cc['timestamp'].dt.year.min()))\nend_date = (str(cc['timestamp'].dt.year.max()))\n\nax = cc['Target'].plot(title = 'Bitcoin target return value',\n                      grid = True, figsize = (15,15))\nax.set_xlabel('Year')\nax.set_ylabel('Target Returns')\nax.axvspan(start_date, end_date, color = 'green',alpha = 0.3)\nax.axhspan(cc['Target'].min(),cc['Target'].max(), color = 'pink',alpha = 0.3)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:07.846576Z","iopub.execute_input":"2021-12-05T14:13:07.846842Z","iopub.status.idle":"2021-12-05T14:13:18.609383Z","shell.execute_reply.started":"2021-12-05T14:13:07.846811Z","shell.execute_reply":"2021-12-05T14:13:18.608764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Seaborn dense plots \n\n- To check where the max data lies for Target return values","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nsns.kdeplot(round(cc['Target'],2),shade=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:13:18.610646Z","iopub.execute_input":"2021-12-05T14:13:18.611012Z","iopub.status.idle":"2021-12-05T14:14:40.353688Z","shell.execute_reply.started":"2021-12-05T14:13:18.61098Z","shell.execute_reply":"2021-12-05T14:14:40.352754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model generation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import KFold\nfrom sklearn.svm import SVR\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:14:40.355025Z","iopub.execute_input":"2021-12-05T14:14:40.355247Z","iopub.status.idle":"2021-12-05T14:14:40.35996Z","shell.execute_reply.started":"2021-12-05T14:14:40.35522Z","shell.execute_reply":"2021-12-05T14:14:40.358983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cc['timestamp'] = (cc['timestamp']).astype(int)\nx = cc.drop(['Target'],axis = 1)\n\ny = cc['Target'].values\n\n\nxtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size = 0.3, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:14:40.361504Z","iopub.execute_input":"2021-12-05T14:14:40.36184Z","iopub.status.idle":"2021-12-05T14:14:44.628Z","shell.execute_reply.started":"2021-12-05T14:14:40.361795Z","shell.execute_reply":"2021-12-05T14:14:44.62705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\n\nmodel = lgb.LGBMRegressor()\nmodel.fit(xtrain, ytrain)\nypred = model.predict(xtest)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:14:44.629289Z","iopub.execute_input":"2021-12-05T14:14:44.629526Z","iopub.status.idle":"2021-12-05T14:15:25.254417Z","shell.execute_reply.started":"2021-12-05T14:14:44.629496Z","shell.execute_reply":"2021-12-05T14:15:25.253471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\n\nprint('Rsquared error : ',metrics.r2_score(ytest,ypred))\nprint('Mean squared error : ',metrics.mean_squared_error(ytest, ypred))","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:15:25.25596Z","iopub.execute_input":"2021-12-05T14:15:25.256308Z","iopub.status.idle":"2021-12-05T14:15:25.412716Z","shell.execute_reply.started":"2021-12-05T14:15:25.256246Z","shell.execute_reply":"2021-12-05T14:15:25.411548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(10,10))\n# plt.title('Ploting the regressor model')\n# sns.regplot(ytest, ypred, fit_reg=True,scatter_kws = {'s':100})","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:15:25.41432Z","iopub.execute_input":"2021-12-05T14:15:25.414644Z","iopub.status.idle":"2021-12-05T14:15:25.419903Z","shell.execute_reply.started":"2021-12-05T14:15:25.414601Z","shell.execute_reply":"2021-12-05T14:15:25.418812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature importance\n","metadata":{}},{"cell_type":"code","source":"lgb.plot_importance(model)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:15:25.42161Z","iopub.execute_input":"2021-12-05T14:15:25.422317Z","iopub.status.idle":"2021-12-05T14:15:25.732273Z","shell.execute_reply.started":"2021-12-05T14:15:25.422267Z","shell.execute_reply":"2021-12-05T14:15:25.73113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"import gresearch_crypto\n# env = gresearch_crypto.make_env()   \n# iter_test = env.iter_test() \nfor (test_df, sample_sub) in iter_test:\n\n    sample_sub['Target'] = model.predict(xtest) \n    \n    env.predict(sample_sub)   ","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:41:37.11924Z","iopub.execute_input":"2021-12-05T14:41:37.119949Z","iopub.status.idle":"2021-12-05T14:41:37.124651Z","shell.execute_reply.started":"2021-12-05T14:41:37.119903Z","shell.execute_reply":"2021-12-05T14:41:37.1239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-12-05T14:35:52.858903Z","iopub.execute_input":"2021-12-05T14:35:52.859188Z","iopub.status.idle":"2021-12-05T14:35:52.86899Z","shell.execute_reply.started":"2021-12-05T14:35:52.859158Z","shell.execute_reply":"2021-12-05T14:35:52.867946Z"},"trusted":true},"execution_count":null,"outputs":[]}]}