{"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":"markdown","source":"# Preface\nIn this note, I would like to perform some EDA on cryptocurrencies, hope that I can get some insight from it.\n* Import needed packages\n* Load data for further analysis\n* Convert timestamp to date format\n* EDA with \"seaborn\" package\n* EDA with \"plotly\" package\n* Market Size of Cryptocurrencies","metadata":{}},{"cell_type":"markdown","source":"# Import needed packages","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport time\nfrom bokeh.plotting import figure, show\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-29T08:44:56.219710Z","iopub.execute_input":"2022-01-29T08:44:56.220518Z","iopub.status.idle":"2022-01-29T08:44:56.828070Z","shell.execute_reply.started":"2022-01-29T08:44:56.220403Z","shell.execute_reply":"2022-01-29T08:44:56.827165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data for further analysis","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv').head(1000)","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:44:56.829987Z","iopub.execute_input":"2022-01-29T08:44:56.830226Z","iopub.status.idle":"2022-01-29T08:45:55.860935Z","shell.execute_reply.started":"2022-01-29T08:44:56.830199Z","shell.execute_reply":"2022-01-29T08:45:55.859945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/asset_details.csv')\nasset_details.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:55.862081Z","iopub.execute_input":"2022-01-29T08:45:55.862293Z","iopub.status.idle":"2022-01-29T08:45:55.890590Z","shell.execute_reply.started":"2022-01-29T08:45:55.862268Z","shell.execute_reply":"2022-01-29T08:45:55.889489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:55.893333Z","iopub.execute_input":"2022-01-29T08:45:55.894030Z","iopub.status.idle":"2022-01-29T08:45:55.914459Z","shell.execute_reply.started":"2022-01-29T08:45:55.893981Z","shell.execute_reply":"2022-01-29T08:45:55.913518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert timestamp to date format","metadata":{}},{"cell_type":"code","source":"train_prep1 = train[['timestamp']].applymap(lambda x: time.localtime(x))\ntrain_prep2 = train_prep1[['timestamp']].applymap(lambda x: time.strftime(\"%Y-%m-%d %H:%M:%S\", x))\ntrain['timestamp'] = train_prep2","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:55.934150Z","iopub.execute_input":"2022-01-29T08:45:55.934360Z","iopub.status.idle":"2022-01-29T08:45:55.959699Z","shell.execute_reply.started":"2022-01-29T08:45:55.934334Z","shell.execute_reply":"2022-01-29T08:45:55.958622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_2018 = train[train['timestamp']<'2018-02-01 00:00:00']","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:55.961526Z","iopub.execute_input":"2022-01-29T08:45:55.962741Z","iopub.status.idle":"2022-01-29T08:45:55.974354Z","shell.execute_reply.started":"2022-01-29T08:45:55.962694Z","shell.execute_reply":"2022-01-29T08:45:55.973327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_2018_ass1 = train_2018[train_2018['Asset_ID']==5]","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:55.976007Z","iopub.execute_input":"2022-01-29T08:45:55.976248Z","iopub.status.idle":"2022-01-29T08:45:55.987482Z","shell.execute_reply.started":"2022-01-29T08:45:55.976220Z","shell.execute_reply":"2022-01-29T08:45:55.986463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_2018_ass1.tail()","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:55.988816Z","iopub.execute_input":"2022-01-29T08:45:55.989292Z","iopub.status.idle":"2022-01-29T08:45:56.014420Z","shell.execute_reply.started":"2022-01-29T08:45:55.989257Z","shell.execute_reply":"2022-01-29T08:45:56.013442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA with \"seaborn\" package\nI tried to use bokeh to plot, but somehow it didn't response, so I have to change to seaborn","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nsns.lineplot(x='timestamp',y='Close',data = train_2018_ass1)","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:56.016100Z","iopub.execute_input":"2022-01-29T08:45:56.016371Z","iopub.status.idle":"2022-01-29T08:45:59.353416Z","shell.execute_reply.started":"2022-01-29T08:45:56.016312Z","shell.execute_reply":"2022-01-29T08:45:59.352508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(x='timestamp',y='Close',data = train_2018_ass1)\nsns.barplot(x=\"timestamp\", y=\"Volume\", data=train_2018_ass1, color=\"b\")","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:45:59.354743Z","iopub.execute_input":"2022-01-29T08:45:59.355032Z","iopub.status.idle":"2022-01-29T08:46:01.990088Z","shell.execute_reply.started":"2022-01-29T08:45:59.355002Z","shell.execute_reply":"2022-01-29T08:46:01.989447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA with \"plotly\" package","metadata":{}},{"cell_type":"code","source":"#import plotly.express as px\n\n#fig = px.line(train_2018_ass1, x=\"timestamp\", y=\"Close\", title='Cryptocurrency Price Trend')\n#fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:01.991060Z","iopub.execute_input":"2022-01-29T08:46:01.991569Z","iopub.status.idle":"2022-01-29T08:46:04.447114Z","shell.execute_reply.started":"2022-01-29T08:46:01.991536Z","shell.execute_reply":"2022-01-29T08:46:04.446338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Market Size of Cryptocurrencies","metadata":{}},{"cell_type":"code","source":"# Step1 join asset detail information to the train table\ntrain_join = pd.merge(train, asset_details, on = 'Asset_ID', how = 'left')","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.448246Z","iopub.execute_input":"2022-01-29T08:46:04.448453Z","iopub.status.idle":"2022-01-29T08:46:04.461191Z","shell.execute_reply.started":"2022-01-29T08:46:04.448428Z","shell.execute_reply":"2022-01-29T08:46:04.460234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_join['Market_Size'] = train_join['Close']*train_join['Volume']\n","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.462706Z","iopub.execute_input":"2022-01-29T08:46:04.463008Z","iopub.status.idle":"2022-01-29T08:46:04.468491Z","shell.execute_reply.started":"2022-01-29T08:46:04.462977Z","shell.execute_reply":"2022-01-29T08:46:04.467749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_join['year'] = pd.DatetimeIndex(train_join['timestamp']).year\ntrain_join['month'] = pd.DatetimeIndex(train_join['timestamp']).month\ntrain_join['year_month'] = train_join['year'].astype (str) + '/' + train_join['month'].astype (str)\ntrain_join","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.469681Z","iopub.execute_input":"2022-01-29T08:46:04.470099Z","iopub.status.idle":"2022-01-29T08:46:04.515221Z","shell.execute_reply.started":"2022-01-29T08:46:04.470006Z","shell.execute_reply":"2022-01-29T08:46:04.514490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = train_join\ntest = pd.DataFrame(test)\ntest['try'] = test['timestamp']\ntest['year'] = pd.DatetimeIndex(test['timestamp']).year\ntest['month'] = pd.DatetimeIndex(test['timestamp']).month#pd.DatetimeIndex(df['Date Attribute']).year\ntest['year_month'] = test['year'].astype (str) + '/' + test['month'].astype (str)\ngroup_table = test['timestamp'].groupby([test['year_month'],test['Asset_ID']]).max()","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.516314Z","iopub.execute_input":"2022-01-29T08:46:04.516635Z","iopub.status.idle":"2022-01-29T08:46:04.536727Z","shell.execute_reply.started":"2022-01-29T08:46:04.516607Z","shell.execute_reply":"2022-01-29T08:46:04.535966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group_table","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.537932Z","iopub.execute_input":"2022-01-29T08:46:04.538616Z","iopub.status.idle":"2022-01-29T08:46:04.545960Z","shell.execute_reply.started":"2022-01-29T08:46:04.538576Z","shell.execute_reply":"2022-01-29T08:46:04.545285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_join_join = pd.merge(train_join, group_table, on=['year_month','Asset_ID'], how='left')\ntrain_join_join","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.547166Z","iopub.execute_input":"2022-01-29T08:46:04.547523Z","iopub.status.idle":"2022-01-29T08:46:04.589676Z","shell.execute_reply.started":"2022-01-29T08:46:04.547492Z","shell.execute_reply":"2022-01-29T08:46:04.589051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_join_join_new = train_join_join[train_join_join['timestamp_x']==train_join_join['timestamp_y']]\ntrain_join_join_new","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.590769Z","iopub.execute_input":"2022-01-29T08:46:04.591123Z","iopub.status.idle":"2022-01-29T08:46:04.618817Z","shell.execute_reply.started":"2022-01-29T08:46:04.591094Z","shell.execute_reply":"2022-01-29T08:46:04.617920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"table_sub = train_join_join_new[['year_month','Asset_Name','Market_Size']]\ntable_sub = table_sub.set_index(['year_month','Asset_Name'])\ntable_sub","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.620131Z","iopub.execute_input":"2022-01-29T08:46:04.620397Z","iopub.status.idle":"2022-01-29T08:46:04.633910Z","shell.execute_reply.started":"2022-01-29T08:46:04.620370Z","shell.execute_reply":"2022-01-29T08:46:04.633042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"table_sub_stack = table_sub.unstack()\ntable_sub_stack","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.635004Z","iopub.execute_input":"2022-01-29T08:46:04.635336Z","iopub.status.idle":"2022-01-29T08:46:04.663544Z","shell.execute_reply.started":"2022-01-29T08:46:04.635309Z","shell.execute_reply":"2022-01-29T08:46:04.662827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ntable_sub_stack.plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-01-29T08:46:04.664793Z","iopub.execute_input":"2022-01-29T08:46:04.665128Z","iopub.status.idle":"2022-01-29T08:46:04.929682Z","shell.execute_reply.started":"2022-01-29T08:46:04.665094Z","shell.execute_reply":"2022-01-29T08:46:04.928846Z"},"trusted":true},"execution_count":null,"outputs":[]}]}