{"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":"<a class=\"anchor\" id=\"0\"></a>\n# Dataset \"[Forecasting Top Cryptocurrencies](https://www.kaggle.com/datasets/vbmokin/forecasting-top-cryptocurrencies)\"\n\n## Data download from API [Yahoo.Finance](https://finance.yahoo.com/cryptocurrencies/)","metadata":{"papermill":{"duration":0.011631,"end_time":"2022-01-18T11:30:35.94757","exception":false,"start_time":"2022-01-18T11:30:35.935939","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## Acknowledgements:\n* FE - the notebook [GResearch Simple LGB Starter](https://www.kaggle.com/code1110/gresearch-simple-lgb-starter)\n* about cryptocurrencies - dataset [Forecasting Top Cryptocurrencies](https://www.kaggle.com/datasets/vbmokin/forecasting-top-cryptocurrencies)\n* data source via API: https://finance.yahoo.com/cryptocurrencies/","metadata":{"papermill":{"duration":0.010188,"end_time":"2022-01-18T11:30:35.968942","exception":false,"start_time":"2022-01-18T11:30:35.958754","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a class=\"anchor\" id=\"0.1\"></a>\n\n## Table of Contents\n\n1. [Import libraries](#1)\n1. [Download data](#2)\n1. [FE](#3)\n1. [Model training](#4)\n1. [Prediction](#5)\n    - [Training data](#5.1)\n    - [Test data](#5.2)","metadata":{"papermill":{"duration":0.010064,"end_time":"2022-01-18T11:30:35.989453","exception":false,"start_time":"2022-01-18T11:30:35.979389","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## 1. Import libraries <a class=\"anchor\" id=\"1\"></a>\n\n[Back to Table of Contents](#0.1)","metadata":{"papermill":{"duration":0.010195,"end_time":"2022-01-18T11:30:36.010143","exception":false,"start_time":"2022-01-18T11:30:35.999948","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Import libraries\nimport random\nimport os\nimport numpy as np \nimport pandas as pd \nimport requests\nimport pandas_datareader as web\n\n# Date\nimport datetime as dt\nfrom datetime import date, timedelta, datetime\n\n# EDA\nimport matplotlib.pyplot as plt\nfrom matplotlib.pylab import rcParams\n\n# Metrics\nfrom sklearn.metrics import r2_score\nfrom sklearn.metrics import mean_squared_error, mean_absolute_percentage_error\n\n# Modeling and preprocessing\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nimport xgboost as xgb\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"papermill":{"duration":2.129942,"end_time":"2022-01-18T11:30:38.150431","exception":false,"start_time":"2022-01-18T11:30:36.020489","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-16T14:16:15.839606Z","iopub.execute_input":"2022-04-16T14:16:15.840335Z","iopub.status.idle":"2022-04-16T14:16:17.101254Z","shell.execute_reply.started":"2022-04-16T14:16:15.840273Z","shell.execute_reply":"2022-04-16T14:16:17.100378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TASK:** It is proposed to experiment with forecasting_days","metadata":{}},{"cell_type":"code","source":"# Set main parameters\ncryptocurrency = 'BTC'\ntarget = 'Close'\nforecasting_days = 2    # requires > 1","metadata":{"execution":{"iopub.status.busy":"2022-04-16T14:16:17.103143Z","iopub.execute_input":"2022-04-16T14:16:17.103463Z","iopub.status.idle":"2022-04-16T14:16:17.108435Z","shell.execute_reply.started":"2022-04-16T14:16:17.103414Z","shell.execute_reply":"2022-04-16T14:16:17.107435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set random state\ndef fix_all_seeds(seed):\n    np.random.seed(seed)\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n\nrandom_state = 42\nfix_all_seeds(random_state)","metadata":{"papermill":{"duration":0.020075,"end_time":"2022-01-18T11:30:38.18269","exception":false,"start_time":"2022-01-18T11:30:38.162615","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-16T14:16:17.109824Z","iopub.execute_input":"2022-04-16T14:16:17.110755Z","iopub.status.idle":"2022-04-16T14:16:17.122095Z","shell.execute_reply.started":"2022-04-16T14:16:17.110709Z","shell.execute_reply":"2022-04-16T14:16:17.121298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TASK :** It is proposed to experiment with date_start and date_end","metadata":{}},{"cell_type":"code","source":"# Set time interval of data for given cryptocurrency - the period of coronavirus in 2020-2021\ndate_start = dt.datetime(2020, 4, 1)\n# date_end = dt.datetime.now()\ndate_end = dt.datetime(2021, 12, 31)\nprint(f\"Time interval: from {date_start} to {date_end}\")","metadata":{"execution":{"iopub.status.busy":"2022-04-16T14:16:17.124668Z","iopub.execute_input":"2022-04-16T14:16:17.125245Z","iopub.status.idle":"2022-04-16T14:16:17.137092Z","shell.execute_reply.started":"2022-04-16T14:16:17.125201Z","shell.execute_reply":"2022-04-16T14:16:17.136196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Download data <a class=\"anchor\" id=\"2\"></a>\n\n[Back to Table of Contents](#0.1)","metadata":{"papermill":{"duration":0.011134,"end_time":"2022-01-18T11:30:38.205393","exception":false,"start_time":"2022-01-18T11:30:38.194259","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Download information about cryptocurrencies\ndf_about = pd.read_csv(\"../input/forecasting-top-cryptocurrencies/about_top_cryptocurrencies_1B_information.csv\", sep=\";\")\ndf_about","metadata":{"execution":{"iopub.status.busy":"2022-04-16T14:16:17.138598Z","iopub.execute_input":"2022-04-16T14:16:17.139406Z","iopub.status.idle":"2022-04-16T14:16:17.206814Z","shell.execute_reply.started":"2022-04-16T14:16:17.139336Z","shell.execute_reply":"2022-04-16T14:16:17.206045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rank_cryptocurrency(df_about, cryptocurrency):\n    # Get rank by Market Cap for code of the cryptocurrency\n    # df_about from the dataset from https://www.kaggle.com/datasets/vbmokin/forecasting-top-cryptocurrencies\n    \n    place = df_about.index[df_about['code'] == cryptocurrency].tolist()[0]\n    if place==0:\n        place_end = 'st'\n    elif place < 3:\n        place_end = 'nd'\n    else: place_end = 'th'\n        \n    print(f\"{df_about.loc[place, 'name']} was {place+1}{place_end}\",\n          \"among the world's cryptocurrencies by market capitalization (2022-04-11)\")","metadata":{"execution":{"iopub.status.busy":"2022-04-16T14:16:17.208527Z","iopub.execute_input":"2022-04-16T14:16:17.209122Z","iopub.status.idle":"2022-04-16T14:16:17.216150Z","shell.execute_reply.started":"2022-04-16T14:16:17.209077Z","shell.execute_reply":"2022-04-16T14:16:17.215388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get rank by Market Cap of the cryptocurrency\nget_rank_cryptocurrency(df_about, cryptocurrency)","metadata":{"execution":{"iopub.status.busy":"2022-04-16T14:16:17.217630Z","iopub.execute_input":"2022-04-16T14:16:17.217841Z","iopub.status.idle":"2022-04-16T14:16:17.239126Z","shell.execute_reply.started":"2022-04-16T14:16:17.217816Z","shell.execute_reply":"2022-04-16T14:16:17.238204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_data(cryptocurrency, date_start, date_end=None):\n    # Get data for given cryptocurrency in USD from Yahoo.finance and https://coinmarketcap.com/\n    # date_end = None means that the date_end is the current day\n    \n    if date_end is None:\n        date_end = dt.datetime.now()\n    df = web.DataReader(f'{cryptocurrency}-USD', 'yahoo', date_start, date_end)\n    df = df[['High', 'Low', 'Open', 'Close', 'Volume']].reset_index(drop=False)\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-04-16T14:16:17.241868Z","iopub.execute_input":"2022-04-16T14:16:17.242520Z","iopub.status.idle":"2022-04-16T14:16:17.249314Z","shell.execute_reply.started":"2022-04-16T14:16:17.242479Z","shell.execute_reply":"2022-04-16T14:16:17.248487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Download data of the cryptocurrency via API\ndata = get_data(cryptocurrency, date_start, date_end)\ndata","metadata":{"execution":{"iopub.status.busy":"2022-04-16T14:16:17.250662Z","iopub.execute_input":"2022-04-16T14:16:17.251084Z","iopub.status.idle":"2022-04-16T14:16:19.174260Z","shell.execute_reply.started":"2022-04-16T14:16:17.251051Z","shell.execute_reply":"2022-04-16T14:16:19.173415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Draw logo-image for the dataset \"Forecasting Top Cryptocurrencies\"\n# data[['High', 'Low', 'Open', 'Close']][500:600].plot(figsize=(12,6)).legend(loc='upper center')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-13T18:52:12.494141Z","iopub.execute_input":"2022-04-13T18:52:12.494648Z","iopub.status.idle":"2022-04-13T18:52:12.498201Z","shell.execute_reply.started":"2022-04-13T18:52:12.494604Z","shell.execute_reply":"2022-04-13T18:52:12.497635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. FE <a class=\"anchor\" id=\"3\"></a>\n\n[Back to Table of Contents](#0.1)","metadata":{"papermill":{"duration":0.011602,"end_time":"2022-01-18T11:31:33.314401","exception":false,"start_time":"2022-01-18T11:31:33.302799","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"**TASK :** It is proposed to experiment with FE : add new features and modify existing ones","metadata":{}},{"cell_type":"code","source":"def get_features(df_feat):\n    # FE for data as row of DataFrame\n    \n    # Two new features from the competition tutorial\n    df_feat['Upper_Shadow'] = df_feat['High'] - np.maximum(df_feat['Close'], df_feat['Open'])\n    df_feat['Lower_Shadow'] = np.minimum(df_feat['Close'], df_feat['Open']) - df_feat['Low']\n    \n    # Thanks to https://www.kaggle.com/code1110/gresearch-simple-lgb-starter\n    df_feat['lower_shadow'] = np.minimum(df_feat['Close'], df_feat['Open']) - df_feat['Low']\n    df_feat['high2low'] = df_feat['High'] / df_feat['Low']\n    \n    return df_feat","metadata":{"papermill":{"duration":0.021609,"end_time":"2022-01-18T11:31:33.348237","exception":false,"start_time":"2022-01-18T11:31:33.326628","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-13T18:52:12.499178Z","iopub.execute_input":"2022-04-13T18:52:12.499504Z","iopub.status.idle":"2022-04-13T18:52:12.514597Z","shell.execute_reply.started":"2022-04-13T18:52:12.499459Z","shell.execute_reply":"2022-04-13T18:52:12.514004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FE\ndf = get_features(data)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:12.515593Z","iopub.execute_input":"2022-04-13T18:52:12.516393Z","iopub.status.idle":"2022-04-13T18:52:12.547095Z","shell.execute_reply.started":"2022-04-13T18:52:12.516352Z","shell.execute_reply":"2022-04-13T18:52:12.546294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['target'] = df[target].shift(-forecasting_days)\ndf = df.dropna().reset_index(drop=True)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:12.548264Z","iopub.execute_input":"2022-04-13T18:52:12.548951Z","iopub.status.idle":"2022-04-13T18:52:12.575992Z","shell.execute_reply.started":"2022-04-13T18:52:12.548899Z","shell.execute_reply":"2022-04-13T18:52:12.575042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_data_for_training(df, test_option, test_size):\n    # Get test and training datasets\n    # test_option = \"end\" for prediction problem or \"random\" for EDA\n    # test_size in days in the end of the time interval for prediction problem (test_option = \"random\")\n    # test_size in part of the days in all data (1 = 100%) for EDA problem (test_option = \"end\")\n    \n    df = df.drop(columns = ['Date'])\n    df = df.dropna(how=\"any\")\n    y = df.pop('target')\n    \n    # Standartization data\n    scaler = StandardScaler()\n    df = pd.DataFrame(scaler.fit_transform(df), columns = df.columns)\n    display(df)\n\n    if test_option == \"random\":\n        train, test, ytrain, ytest = train_test_split(df, y, test_size=test_size, random_state=random_state)\n    else:\n        # test_option == \"end\"\n        train_len = len(df)-test_size\n        test = df[train_len:]\n        train = df[:train_len]\n        ytest = y[train_len:]\n        ytrain = y[:train_len]\n        \n    print(f'Get training dataset with {len(train)} rows and test dataset with {len(test)} rows for test option - \"{test_option}\"')\n    \n    return train, test, ytrain, ytest","metadata":{"papermill":{"duration":0.021064,"end_time":"2022-01-18T11:31:33.38135","exception":false,"start_time":"2022-01-18T11:31:33.360286","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-13T18:52:12.577384Z","iopub.execute_input":"2022-04-13T18:52:12.577591Z","iopub.status.idle":"2022-04-13T18:52:12.585428Z","shell.execute_reply.started":"2022-04-13T18:52:12.577566Z","shell.execute_reply":"2022-04-13T18:52:12.584501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TASK :** It is proposed to experiment with option 'end' and 'random' in function get_data_for_training","metadata":{}},{"cell_type":"code","source":"# Get test and training datasets\ntrain, test, target_train, target_test = get_data_for_training(df, 'end', forecasting_days)\n#train, test, ytrain, ytest = get_data_for_training(df, 'random', 0.2)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:12.586849Z","iopub.execute_input":"2022-04-13T18:52:12.587619Z","iopub.status.idle":"2022-04-13T18:52:12.621221Z","shell.execute_reply.started":"2022-04-13T18:52:12.587579Z","shell.execute_reply":"2022-04-13T18:52:12.620384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Model training and prediction <a class=\"anchor\" id=\"4\"></a>\n\n[Back to Table of Contents](#0.1)","metadata":{"papermill":{"duration":0.011744,"end_time":"2022-01-18T11:31:33.405477","exception":false,"start_time":"2022-01-18T11:31:33.393733","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"**TASK :** It is proposed to experiment with model parameters","metadata":{}},{"cell_type":"code","source":"def xgb_training(train, target_train):\n    # XGB Classifier Training\n    \n    xgbr = xgb.XGBRegressor() \n    param_grid_xgb = {'max_depth': [3, 4, 5],\n                      'n_estimators': [50, 70, 90], \n                      'learning_rate': [0.01, 0.05, 0.1, 0.2],\n                      'random_state': [random_state]}\n    \n    # Training model\n    xgb_CV = GridSearchCV(xgbr, param_grid=param_grid_xgb, cv=3, verbose=False)\n    xgb_CV.fit(train, target_train)\n    xgbp = xgb_CV.best_params_\n    print(xgbp)\n    print(f\"Score is {xgb_CV.best_score_}\")\n\n    # Feature importance diagrame\n    xgb_model = xgb.XGBRegressor(max_depth=5,\n                                  random_state=random_state).fit(train, target_train)\n    fig =  plt.figure(figsize = (15,15))\n    axes = fig.add_subplot(111)\n    xgb.plot_importance(xgb_model,ax = axes,height = 0.5)\n    plt.show();plt.close()\n    \n    return xgb_CV, xgbp, xgb_model","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:12.622459Z","iopub.execute_input":"2022-04-13T18:52:12.622693Z","iopub.status.idle":"2022-04-13T18:52:12.632028Z","shell.execute_reply.started":"2022-04-13T18:52:12.622664Z","shell.execute_reply":"2022-04-13T18:52:12.631153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nxgb_CV, xgbp, xgb_model = xgb_training(train, target_train)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:12.633329Z","iopub.execute_input":"2022-04-13T18:52:12.633594Z","iopub.status.idle":"2022-04-13T18:52:35.99079Z","shell.execute_reply.started":"2022-04-13T18:52:12.633565Z","shell.execute_reply":"2022-04-13T18:52:35.989937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. Prediction<a class=\"anchor\" id=\"5\"></a>\n\n[Back to Table of Contents](#0.1)","metadata":{"papermill":{"duration":0.015682,"end_time":"2022-01-18T12:47:42.369031","exception":false,"start_time":"2022-01-18T12:47:42.353349","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def display_2float_lists_alongside(list1, name_list1, list2, name_list2, num=5, num_decimal=2):\n    # Display num first and last data from 2 lists (or array) with float numbers alongside\n    # It displays float number in format with \"%.{num_decimal}f\" )\n    \n    def list_float_formatting(list_float, num_decimal=2):\n    # Output float numbers from the list with formatting %.{num_decimal}f\n        return \", \".join(f\"%.{num_decimal}f\" % s for s in list_float)\n    \n    biggest_name_length = len(name_list1)+2 if len(name_list1) > len(name_list2) else len(name_list2)+2\n    name_list1 = '{0: >{width}}'.format(name_list1+\": \", width=biggest_name_length)\n    name_list2 = '{0: >{width}}'.format(name_list2+\": \", width=biggest_name_length)\n    \n    print(f\"Number of values is {len(list1)}\\n\")\n    num = len(list1) if len(list1) < num else num        \n    print(f\"The {num} first values:\")\n    print(name_list1, list_float_formatting(list(list1)[:num], num_decimal))\n    print(name_list2, list_float_formatting(list(list2)[:num], num_decimal), '\\n')\n    \n    if len(list1) > num:\n        print(f\"The {num} last values:\")\n        print(name_list1, list_float_formatting(list(list1)[-num:], num_decimal))\n        print(name_list2, list_float_formatting(list(list2)[-num:], num_decimal))    ","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:35.99207Z","iopub.execute_input":"2022-04-13T18:52:35.992278Z","iopub.status.idle":"2022-04-13T18:52:36.002238Z","shell.execute_reply.started":"2022-04-13T18:52:35.992253Z","shell.execute_reply":"2022-04-13T18:52:36.001434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.1. Training data<a class=\"anchor\" id=\"5.1\"></a>\n\n[Back to Table of Contents](#0.1)","metadata":{}},{"cell_type":"code","source":"# Prediction of training data\nypred_train = xgb_model.predict(train)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.00351Z","iopub.execute_input":"2022-04-13T18:52:36.00376Z","iopub.status.idle":"2022-04-13T18:52:36.029752Z","shell.execute_reply.started":"2022-04-13T18:52:36.003731Z","shell.execute_reply":"2022-04-13T18:52:36.029024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building plot for prediction for the training data \nx = np.arange(len(train))\nplt.figure(figsize=(12,8))\nplt.scatter(x, target_train, label = \"Target training data\", color = 'g', s=40)\nplt.scatter(x, ypred_train, label = \"XGBoost Regressor prediction\", color = 'y', s=10)\nplt.title('Prediction for the training data')\nplt.legend(loc='best')\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.031013Z","iopub.execute_input":"2022-04-13T18:52:36.031745Z","iopub.status.idle":"2022-04-13T18:52:36.325239Z","shell.execute_reply.started":"2022-04-13T18:52:36.031704Z","shell.execute_reply":"2022-04-13T18:52:36.324427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Comparison training data and prediction\ndisplay_2float_lists_alongside(target_train, \"Training data\", ypred_train, \"Prediction\")","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.326346Z","iopub.execute_input":"2022-04-13T18:52:36.326568Z","iopub.status.idle":"2022-04-13T18:52:36.333459Z","shell.execute_reply.started":"2022-04-13T18:52:36.32654Z","shell.execute_reply":"2022-04-13T18:52:36.332824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Score for the training data\nprint(f\"Score for training data is %0.7f\" % r2_score(target_train, ypred_train))","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.335077Z","iopub.execute_input":"2022-04-13T18:52:36.335633Z","iopub.status.idle":"2022-04-13T18:52:36.34664Z","shell.execute_reply.started":"2022-04-13T18:52:36.335591Z","shell.execute_reply":"2022-04-13T18:52:36.345999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.2. Test data<a class=\"anchor\" id=\"5.2\"></a>\n\n[Back to Table of Contents](#0.1)","metadata":{}},{"cell_type":"code","source":"# Prediction of test data\nypred_test = xgb_model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.348361Z","iopub.execute_input":"2022-04-13T18:52:36.348935Z","iopub.status.idle":"2022-04-13T18:52:36.362791Z","shell.execute_reply.started":"2022-04-13T18:52:36.348879Z","shell.execute_reply":"2022-04-13T18:52:36.362118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building plot for prediction for the test data \nx = np.arange(len(test))\nplt.figure(figsize=(12,8))\nplt.scatter(x, target_test, label = \"Target test data\", color = 'g')\nplt.scatter(x, ypred_test, label = \"XGBoost Regressor prediction\", color = 'b')\nplt.ylim(ymin=0)\nplt.title('Prediction for the test data')\nplt.legend(loc='best')\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.364396Z","iopub.execute_input":"2022-04-13T18:52:36.364851Z","iopub.status.idle":"2022-04-13T18:52:36.608217Z","shell.execute_reply.started":"2022-04-13T18:52:36.364814Z","shell.execute_reply":"2022-04-13T18:52:36.607415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building plot for prediction for the test data \nx = np.arange(len(test))\nplt.figure(figsize=(12,8))\nplt.scatter(x, target_test, label = \"Target test data\", color = 'g')\nplt.scatter(x, ypred_test, label = \"XGBoost Regressor prediction\", color = 'b')\nplt.title('Prediction for the test data')\nplt.legend(loc='best')\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.609362Z","iopub.execute_input":"2022-04-13T18:52:36.609593Z","iopub.status.idle":"2022-04-13T18:52:36.859395Z","shell.execute_reply.started":"2022-04-13T18:52:36.609565Z","shell.execute_reply":"2022-04-13T18:52:36.858472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Comparison test data and prediction\ndisplay_2float_lists_alongside(target_test, \"Test data\", ypred_test, \"Prediction\")","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.860596Z","iopub.execute_input":"2022-04-13T18:52:36.860848Z","iopub.status.idle":"2022-04-13T18:52:36.867129Z","shell.execute_reply.started":"2022-04-13T18:52:36.860817Z","shell.execute_reply":"2022-04-13T18:52:36.86631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**THE MAIN TASK : Optimize errors!**","metadata":{}},{"cell_type":"code","source":"# Score for the test data\nprint(f\"R2_score (optimal = 1) for test data is %0.2f\" % r2_score(target_test, list(ypred_test)))\nprint(f\"Root mean squared error (optimal = $0) for test data is %0.2f\" % mean_squared_error(target_test, ypred_test, squared=False))\nprint(f\"Mean absolute percentage error (optimal = 0 per cent) for test data is %0.2f\" % mean_absolute_percentage_error(target_test, ypred_test))","metadata":{"execution":{"iopub.status.busy":"2022-04-13T18:52:36.868537Z","iopub.execute_input":"2022-04-13T18:52:36.869119Z","iopub.status.idle":"2022-04-13T18:52:36.880448Z","shell.execute_reply.started":"2022-04-13T18:52:36.869076Z","shell.execute_reply":"2022-04-13T18:52:36.879626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I hope you find this kernel useful and enjoyable.\n\nYour comments and feedback are most welcome.","metadata":{"papermill":{"duration":0.018515,"end_time":"2022-01-18T12:47:43.747653","exception":false,"start_time":"2022-01-18T12:47:43.729138","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"[Go to Top](#0)","metadata":{"papermill":{"duration":0.017841,"end_time":"2022-01-18T12:47:43.783099","exception":false,"start_time":"2022-01-18T12:47:43.765258","status":"completed"},"tags":[]}}]}