{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\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":"2021-11-30T08:14:17.925715Z","iopub.execute_input":"2021-11-30T08:14:17.92624Z","iopub.status.idle":"2021-11-30T08:14:17.959705Z","shell.execute_reply.started":"2021-11-30T08:14:17.926114Z","shell.execute_reply":"2021-11-30T08:14:17.959064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv')\nprint(train.size)\ntrain.replace([np.inf, -np.inf], np.nan, inplace=True)\ntrain.dropna(inplace=True)\nprint(train.size)\n# take a sample of the training data\ntrain = train.sample(frac=0.01)\nprint(train.size)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T08:26:04.056747Z","iopub.execute_input":"2021-11-30T08:26:04.057147Z","iopub.status.idle":"2021-11-30T08:27:12.777301Z","shell.execute_reply.started":"2021-11-30T08:26:04.057108Z","shell.execute_reply":"2021-11-30T08:27:12.776025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Two new features from the competition tutorial\ndef upper_shadow(df):\n    return df['High'] - np.maximum(df['Close'], df['Open'])\n\ndef lower_shadow(df):\n    return np.minimum(df['Close'], df['Open']) - df['Low']\n\n# Helper function for adding all the engineered features\ndef feature_engineering(df):\n    df['Upper_Shadow'] = upper_shadow(df)\n    df['Lower_Shadow'] = lower_shadow(df)\n    return df\n\ntrain = feature_engineering(train)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T08:36:46.07578Z","iopub.execute_input":"2021-11-30T08:36:46.076318Z","iopub.status.idle":"2021-11-30T08:36:46.092174Z","shell.execute_reply.started":"2021-11-30T08:36:46.076239Z","shell.execute_reply":"2021-11-30T08:36:46.091166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nmodel = RandomForestRegressor()\nmodel.fit(train[['Asset_ID', 'Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP', 'Upper_Shadow', 'Lower_Shadow']].values, train['Target'].values)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T08:36:48.991176Z","iopub.execute_input":"2021-11-30T08:36:48.991521Z","iopub.status.idle":"2021-11-30T08:36:51.736322Z","shell.execute_reply.started":"2021-11-30T08:36:48.991487Z","shell.execute_reply":"2021-11-30T08:36:51.735586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Start to predict\nimport math\n\nimport gresearch_crypto\nenv = gresearch_crypto.make_env()   # initialize the environment\n\ndef predict(row):\n    row = feature_engineering(row)\n    p_val = model.predict([row[['Asset_ID', 'Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP', 'Upper_Shadow', 'Lower_Shadow']].values])[0]\n    return p_val\n\niter_test = env.iter_test()    # an iterator which loops over the test set and sample submission\nfor (test_df, sample_prediction_df) in iter_test:\n    sample_prediction_df['Target'] = test_df.apply(lambda row: predict(row), axis = 1)\n    #print(sample_prediction_df)\n    #sample_prediction_df['Target'] = 0  # make your predictions here\n    env.predict(sample_prediction_df)   # register your predictions\n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-30T08:37:22.83536Z","iopub.execute_input":"2021-11-30T08:37:22.836384Z","iopub.status.idle":"2021-11-30T08:37:23.551693Z","shell.execute_reply.started":"2021-11-30T08:37:22.83634Z","shell.execute_reply":"2021-11-30T08:37:23.550596Z"},"trusted":true},"execution_count":null,"outputs":[]}]}