{"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":"2022-07-30T11:11:50.218668Z","iopub.execute_input":"2022-07-30T11:11:50.219146Z","iopub.status.idle":"2022-07-30T11:11:50.252451Z","shell.execute_reply.started":"2022-07-30T11:11:50.219049Z","shell.execute_reply":"2022-07-30T11:11:50.250814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/store-sales-time-series-forecasting/train.csv\",parse_dates=['date'])\ntransaction_df = pd.read_csv('../input/store-sales-time-series-forecasting/transactions.csv',parse_dates=['date'])\nstore_df = pd.read_csv('../input/store-sales-time-series-forecasting/stores.csv')\noil_df = pd.read_csv('../input/store-sales-time-series-forecasting/oil.csv',parse_dates=['date'])\nholiday_df=pd.read_csv('../input/store-sales-time-series-forecasting/holidays_events.csv',parse_dates=['date'])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:12:39.311479Z","iopub.execute_input":"2022-07-30T11:12:39.311900Z","iopub.status.idle":"2022-07-30T11:12:42.808573Z","shell.execute_reply.started":"2022-07-30T11:12:39.311868Z","shell.execute_reply":"2022-07-30T11:12:42.807224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:12:47.951127Z","iopub.execute_input":"2022-07-30T11:12:47.951672Z","iopub.status.idle":"2022-07-30T11:12:48.633982Z","shell.execute_reply.started":"2022-07-30T11:12:47.951626Z","shell.execute_reply":"2022-07-30T11:12:48.632592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# first merge train data with store data than join other files on date column by setting index date wise.\n# transaction cannot be used in training model because train and test should have same data and transactions data is not available for test set \ntrain_df=train_df.merge(store_df,on='store_nbr')\ntrain_df=train_df.rename(columns={'type':'store_type'})\ntrain_df=train_df.set_index('date',drop=False).sort_index()\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:12:53.953189Z","iopub.execute_input":"2022-07-30T11:12:53.953808Z","iopub.status.idle":"2022-07-30T11:12:55.818470Z","shell.execute_reply.started":"2022-07-30T11:12:53.953757Z","shell.execute_reply":"2022-07-30T11:12:55.817601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oil_df=oil_df.set_index('date').sort_index()\nholiday_df=holiday_df.set_index('date').sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:01.128178Z","iopub.execute_input":"2022-07-30T11:13:01.128595Z","iopub.status.idle":"2022-07-30T11:13:01.137102Z","shell.execute_reply.started":"2022-07-30T11:13:01.128562Z","shell.execute_reply":"2022-07-30T11:13:01.135840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=train_df.join(oil_df)\ntrain_df=train_df.join(holiday_df)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:03.679873Z","iopub.execute_input":"2022-07-30T11:13:03.680602Z","iopub.status.idle":"2022-07-30T11:13:05.345190Z","shell.execute_reply.started":"2022-07-30T11:13:03.680566Z","shell.execute_reply":"2022-07-30T11:13:05.341519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=train_df.rename(columns={'dcoilwtico':'oil_price','type':'holiday_type'})","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:11.975669Z","iopub.execute_input":"2022-07-30T11:13:11.976096Z","iopub.status.idle":"2022-07-30T11:13:13.117378Z","shell.execute_reply.started":"2022-07-30T11:13:11.976063Z","shell.execute_reply":"2022-07-30T11:13:13.116117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['year']=train_df['date'].dt.year\ntrain_df['month']=train_df['date'].dt.month\ntrain_df['day_of_week']=train_df['date'].dt.day_name()\ntrain_df['quarter']=train_df['date'].dt.quarter\ntrain_df['date_of_month']=train_df['date'].dt.day\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:32.968941Z","iopub.execute_input":"2022-07-30T11:13:32.969382Z","iopub.status.idle":"2022-07-30T11:13:35.348345Z","shell.execute_reply.started":"2022-07-30T11:13:32.969348Z","shell.execute_reply":"2022-07-30T11:13:35.347031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fill missing values of oil price by mean values of particular year\ntrain_df.loc[train_df['year']==2013,'oil_price']=train_df.loc[train_df['year']==2013,'oil_price'].fillna(train_df.oil_price.mean())\ntrain_df.loc[train_df['year']==2014,'oil_price']=train_df.loc[train_df['year']==2014,'oil_price'].fillna(train_df.oil_price.mean())\ntrain_df.loc[train_df['year']==2015,'oil_price']=train_df.loc[train_df['year']==2015,'oil_price'].fillna(train_df.oil_price.mean())\ntrain_df.loc[train_df['year']==2016,'oil_price']=train_df.loc[train_df['year']==2016,'oil_price'].fillna(train_df.oil_price.mean())\ntrain_df.loc[train_df['year']==2017,'oil_price']=train_df.loc[train_df['year']==2017,'oil_price'].fillna(train_df.oil_price.mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:39.929944Z","iopub.execute_input":"2022-07-30T11:13:39.930354Z","iopub.status.idle":"2022-07-30T11:13:40.214883Z","shell.execute_reply.started":"2022-07-30T11:13:39.930322Z","shell.execute_reply":"2022-07-30T11:13:40.213574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ys=train_df.groupby('year').sales.mean()\nyp=train_df.groupby('year').onpromotion.mean()\nms=train_df.groupby('month').sales.mean()\nmp=train_df.groupby('month').onpromotion.mean()\nweeklys=train_df.groupby('day_of_week').sales.mean()\nweeklyp=train_df.groupby('day_of_week').onpromotion.mean()\nquarterlys=train_df.groupby('quarter').sales.mean()\nquarterlyp=train_df.groupby('quarter').onpromotion.mean()\ndailys=train_df.groupby('date_of_month').sales.mean()\ndailyp=train_df.groupby('date_of_month').onpromotion.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:46.485781Z","iopub.execute_input":"2022-07-30T11:13:46.486190Z","iopub.status.idle":"2022-07-30T11:13:47.456337Z","shell.execute_reply.started":"2022-07-30T11:13:46.486157Z","shell.execute_reply":"2022-07-30T11:13:47.455109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax=plt.subplots(5,2,figsize=(40,30))\nfig.suptitle('Average Sales and Promotion',fontsize=40)\nplt.subplots_adjust(hspace=0.5)\nsns.barplot(x=ys.index,y=ys,ax=ax[0,0],palette='mako')\nax[0,0].set_title('Average yearly sales',fontweight='bold',size=30)\nsns.barplot(x=yp.index,y=yp,ax=ax[0,1])\nax[0,1].set_title('Average yearly promotion of products',fontweight='bold',size=30)\nsns.barplot(x=ms.index,y=ms,ax=ax[1,0],palette='viridis')\nax[1,0].set_title('Avg monthly sales',fontweight='bold',size=30)\nsns.barplot(x=mp.index,y=mp,ax=ax[1,1])\nax[1,1].set_title('Avg monthly promotion',fontweight='bold',size=30)\nsns.barplot(x=weeklys.index,y=weeklys,ax=ax[2,0],palette='rocket')\nax[2,0].set_title('Avg weekly sales',fontweight='bold',size=30)\nsns.barplot(x=weeklyp.index,y=weeklyp,ax=ax[2,1])\nax[2,1].set_title('Avg weekly promotion',fontweight='bold',size=30)\nsns.barplot(x=quarterlys.index,y=quarterlys,ax=ax[3,0],palette='mako')\nax[3,0].set_title('Avg quarterly sales',fontweight='bold',size=30)\nsns.barplot(x=quarterlyp.index,y=quarterlyp,ax=ax[3,1])\nax[3,1].set_title('Avg quarterly promotion',fontweight='bold',size=30)\nsns.barplot(x=dailys.index,y=dailys,ax=ax[4,0],palette='crest')\nax[4,0].set_title('Avg daily sales',fontweight='bold',size=30)\nsns.barplot(x=dailyp.index,y=dailyp,ax=ax[4,1])\nax[4,1].set_title('Avg daily promotion',fontweight='bold',size=30)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:50.164400Z","iopub.execute_input":"2022-07-30T11:13:50.164842Z","iopub.status.idle":"2022-07-30T11:13:52.632757Z","shell.execute_reply.started":"2022-07-30T11:13:50.164809Z","shell.execute_reply":"2022-07-30T11:13:52.631810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***Insights***\n\n`Sales` is increasing with years and monthly sales is maximum for **December** maybe because of Christmas or New Year Celebration. People prefers to buy items on **Saturday** and **Sunday** and generaly sales are high for **Last Quarter** of year maybe because people save throught out the year and spend less in other quaters. People get paid twice a month (on 15th and 30th) and generally buy items at the end of month i.e.**(1,2 or 30,31)**\n\n`Promotion` of items increase rapidly for last two years.We don't see a clear relation between sales and promotion as sales were maximum on different days and products were promoted on different days.Promotion may influence consumers behaviour but **on the day of promotion sales were not increased**","metadata":{}},{"cell_type":"code","source":"products=train_df.groupby('family').sales.mean().sort_values(ascending=False)\nproductp=train_df.groupby('family').onpromotion.mean().sort_values(ascending=False)\nstypes=train_df.groupby('store_type').sales.mean()\nstypep=train_df.groupby('store_type').onpromotion.mean()\ncitys=train_df.groupby('city').sales.mean()\ncityp=train_df.groupby('city').onpromotion.mean()\nstatep=train_df.groupby('state').onpromotion.mean()\nstates=train_df.groupby('state').sales.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:13:56.928993Z","iopub.execute_input":"2022-07-30T11:13:56.929392Z","iopub.status.idle":"2022-07-30T11:13:58.846888Z","shell.execute_reply.started":"2022-07-30T11:13:56.929359Z","shell.execute_reply":"2022-07-30T11:13:58.845728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax=plt.subplots(3,2,figsize=(16,12))\nfig.suptitle('Average Sales(left) vs Average Promotion(right)',fontsize=30)\nplt.subplots_adjust(hspace=0.9)\nsns.barplot(x=stypes.index,y=stypes,ax=ax[0,0],palette='flare')\nax[0,0].set_title(\"Average sales of different stores\",size=20)\nsns.barplot(x=stypep.index,y=stypep,ax=ax[0,1])\nax[0,1].set_title(\"Average Promotion in different stores\",size=20)\nsns.barplot(x=citys.index,y=citys,ax=ax[1,0],palette='crest')\nax[1,0].set_title(\"Average sales city wise\",size=20)\nax[1,0].set_xticklabels(labels=citys.index,rotation=90)\nsns.barplot(x=cityp.index,y=cityp,ax=ax[1,1],color='YrGn',palette='flare')\nax[1,1].set_title(\"Avg promotion in cities\",size=20)\nax[1,1].set_xticklabels(labels=cityp.index,rotation=90)\nsns.barplot(x=states.index,y=states,ax=ax[2,0],palette='mako')\nax[2,0].set_title(\"Avg state sales\",size=20)\nax[2,0].set_xticklabels(labels=states.index,rotation=90)\nsns.barplot(x=statep.index,y=statep,ax=ax[2,1])\nax[2,1].set_title(\"Avg promotion state wise\",size=20)\nax[2,1].set_xticklabels(labels=statep.index,rotation=90);","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:00.301848Z","iopub.execute_input":"2022-07-30T11:14:00.302248Z","iopub.status.idle":"2022-07-30T11:14:01.939525Z","shell.execute_reply.started":"2022-07-30T11:14:00.302216Z","shell.execute_reply":"2022-07-30T11:14:01.938328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"products=train_df.groupby('family').sales.mean().sort_values(ascending=False)[:15]\nproductp=train_df.groupby('family').onpromotion.mean().sort_values(ascending=False)[:15]\nfig,ax=plt.subplots(2,1,figsize=(10,8))\nsns.barplot(x=products[:10],y=products.index[:10],ax=ax[0])\nax[0].set_title(\"Products on Sales (top 10)\")\nsns.barplot(x=productp[:10],y=productp.index[:10],ax=ax[1])\nax[1].set_title(\"Products on Promotion(top 10)\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:03.999653Z","iopub.execute_input":"2022-07-30T11:14:04.000056Z","iopub.status.idle":"2022-07-30T11:14:04.981583Z","shell.execute_reply.started":"2022-07-30T11:14:04.000023Z","shell.execute_reply":"2022-07-30T11:14:04.980419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Products which have maximim sales are similar to ones which were promoted maximum no. of times but promotion may not have more influence on sales as we see `DELI` products are promoted more than `Polutry,Meat and Bread` but sales graph show otherwise. People might prefer necessary items.","metadata":{}},{"cell_type":"code","source":"table1=train_df.pivot_table(index='month',columns='year',values='oil_price')\ntable1.plot(figsize=(12,8),title='Avg monthly oil prices');","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:11.260472Z","iopub.execute_input":"2022-07-30T11:14:11.260908Z","iopub.status.idle":"2022-07-30T11:14:12.874414Z","shell.execute_reply.started":"2022-07-30T11:14:11.260875Z","shell.execute_reply":"2022-07-30T11:14:12.873292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Oil prices were high in 2013 but after July 2015 prices starts decreasing. Oil prices were below 60 for next three years except for 3 months in 2015. In 2016 oil price increase after disater(in May) and stay high for other quarters than first quarter.\n\nFrom below graph we can see: `Sales` are increasing year after year and mostly high in last quarter of the year.","metadata":{}},{"cell_type":"code","source":"table2=train_df.pivot_table(index='month',columns='year',values='sales')\ntable2.plot(figsize=(12,10),title='Avg  monthly sales');","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:17.009102Z","iopub.execute_input":"2022-07-30T11:14:17.009563Z","iopub.status.idle":"2022-07-30T11:14:17.514097Z","shell.execute_reply.started":"2022-07-30T11:14:17.009517Z","shell.execute_reply":"2022-07-30T11:14:17.512565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.pivot_table(index='holiday_type',columns='locale',values='sales').plot(kind='bar',figsize=(10,6))","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:23.145129Z","iopub.execute_input":"2022-07-30T11:14:23.145561Z","iopub.status.idle":"2022-07-30T11:14:24.095750Z","shell.execute_reply.started":"2022-07-30T11:14:23.145516Z","shell.execute_reply":"2022-07-30T11:14:24.094776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sales are maximum on `National` holiday and among holiday type most people go shopping on `Additional Holidays` but on Local holidays(specific to some regions) people prefer `Transfer` (holiday transferred to other day) holiday to go shopping.\n\n\nFrom below graph we can conclude that people prefer store type `A` and places where there are `five similar stores` (cluster 5). Store type C with cluster 7 is lest preferred.","metadata":{}},{"cell_type":"code","source":"train_df.pivot_table(index='cluster',columns='store_type',values='sales').plot(kind='bar',title='Avg Sales of different stores based on clusters',figsize=(10,6));","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:29.698389Z","iopub.execute_input":"2022-07-30T11:14:29.699554Z","iopub.status.idle":"2022-07-30T11:14:30.623295Z","shell.execute_reply.started":"2022-07-30T11:14:29.699484Z","shell.execute_reply":"2022-07-30T11:14:30.621979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# analysis of year 2016\ndis_year=train_df[train_df.year==2016] ","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:36.354346Z","iopub.execute_input":"2022-07-30T11:14:36.354787Z","iopub.status.idle":"2022-07-30T11:14:36.448901Z","shell.execute_reply.started":"2022-07-30T11:14:36.354753Z","shell.execute_reply":"2022-07-30T11:14:36.447534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items=dis_year.groupby('family').sales.mean().sort_values(ascending=False)[:10]\nreg_items=train_df.groupby('family').sales.mean().sort_values(ascending=False)[:10]\nfig,ax=plt.subplots(2,1,figsize=(10,8),sharex=True)\nsns.barplot(x=items,y=items.index,ax=ax[0])\nax[0].set_title(\"Products sold during relief period\")\nsns.barplot(x=reg_items,y=reg_items.index,ax=ax[1])\nax[1].set_title(\"Products sold on regular days\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:39.727320Z","iopub.execute_input":"2022-07-30T11:14:39.728418Z","iopub.status.idle":"2022-07-30T11:14:40.511459Z","shell.execute_reply.started":"2022-07-30T11:14:39.728381Z","shell.execute_reply":"2022-07-30T11:14:40.510289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"People's preference are same(from above graph) even in year when earthquake hit the country but from below graph we can see that in the month of clamity `Home Care` products are in list of top 10 products with maximum sales (genarally DELI products are in list).","metadata":{}},{"cell_type":"code","source":"dis_year[dis_year.month==4].groupby('family').sales.mean().sort_values(ascending=False)[:10].plot(kind='bar');","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:47.265610Z","iopub.execute_input":"2022-07-30T11:14:47.266976Z","iopub.status.idle":"2022-07-30T11:14:47.537989Z","shell.execute_reply.started":"2022-07-30T11:14:47.266920Z","shell.execute_reply":"2022-07-30T11:14:47.536476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dis_year.loc[:,['cumsum_']]=dis_year.groupby('month').sales.apply(lambda x: x.cumsum())","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:53.043821Z","iopub.execute_input":"2022-07-30T11:14:53.044223Z","iopub.status.idle":"2022-07-30T11:14:53.145992Z","shell.execute_reply.started":"2022-07-30T11:14:53.044191Z","shell.execute_reply":"2022-07-30T11:14:53.144907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pointplot(x=dis_year.month,y=dis_year.cumsum_)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:14:55.707841Z","iopub.execute_input":"2022-07-30T11:14:55.708259Z","iopub.status.idle":"2022-07-30T11:15:03.515270Z","shell.execute_reply.started":"2022-07-30T11:14:55.708224Z","shell.execute_reply":"2022-07-30T11:15:03.513725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sales increase in month of April (when disaster hit) may be because government purchase necessary items for relief measures but decreased drascticaly for next month may be because people restrict their expenditure to save money in order to recover from clamity.","metadata":{}},{"cell_type":"markdown","source":"### Spliting Data into training and validation set\n\n> We use sample of data to train our model as data is too large and take longer time to analyse the whole data\n\nWe can drop the columns who are not that important and our target column(sales)","metadata":{}},{"cell_type":"code","source":"training_df=train_df['2013-01-01':'2016-10-31']\nX_train=training_df.drop(['sales','holiday_type','locale','locale_name','description','transferred','date'],axis=1)\ny_train=training_df['sales']\nvalidation_df=train_df['2016-11-01':]\nX_val=validation_df.drop(['sales','holiday_type','locale','locale_name','description','transferred','date'],axis=1)\ny_val=validation_df['sales']\nprint(training_df.shape,validation_df.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:08.814682Z","iopub.execute_input":"2022-07-30T11:15:08.815135Z","iopub.status.idle":"2022-07-30T11:15:09.119882Z","shell.execute_reply.started":"2022-07-30T11:15:08.815102Z","shell.execute_reply":"2022-07-30T11:15:09.118581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:11.579457Z","iopub.execute_input":"2022-07-30T11:15:11.580084Z","iopub.status.idle":"2022-07-30T11:15:11.599547Z","shell.execute_reply.started":"2022-07-30T11:15:11.580049Z","shell.execute_reply":"2022-07-30T11:15:11.598021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_=pd.get_dummies(X_train)\nX_train_","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:16.768788Z","iopub.execute_input":"2022-07-30T11:15:16.769182Z","iopub.status.idle":"2022-07-30T11:15:19.963481Z","shell.execute_reply.started":"2022-07-30T11:15:16.769147Z","shell.execute_reply":"2022-07-30T11:15:19.962169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_s=X_train_.sample(frac=0.008,random_state=22).sort_index()\ny_train_s=y_train.sample(frac=0.008,random_state=22).sort_index()\nprint(X_train_s.head())\nprint(y_train_s[:5])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:28.169511Z","iopub.execute_input":"2022-07-30T11:15:28.169924Z","iopub.status.idle":"2022-07-30T11:15:28.899773Z","shell.execute_reply.started":"2022-07-30T11:15:28.169891Z","shell.execute_reply":"2022-07-30T11:15:28.898412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val_=pd.get_dummies(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:33.494742Z","iopub.execute_input":"2022-07-30T11:15:33.495177Z","iopub.status.idle":"2022-07-30T11:15:34.001148Z","shell.execute_reply.started":"2022-07-30T11:15:33.495144Z","shell.execute_reply":"2022-07-30T11:15:33.999680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val_s=X_val_.sample(frac=0.008,random_state=22).sort_index()\ny_val_s=y_val.sample(frac=0.008,random_state=22).sort_index()\nprint(X_val_s.head(),y_val_s[:5])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:36.456288Z","iopub.execute_input":"2022-07-30T11:15:36.457033Z","iopub.status.idle":"2022-07-30T11:15:36.573630Z","shell.execute_reply.started":"2022-07-30T11:15:36.456991Z","shell.execute_reply":"2022-07-30T11:15:36.572357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Building, Evaluation and Parameter Tuning\n\n`Evaluation` will be on `Root Mean Squared Log Error`.\n\n`Tuning` first by random search and than by grid search. We take sample of data for evaluation and tuning as bigger dataset take more time.","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.ensemble import RandomForestRegressor,GradientBoostingRegressor\nfrom sklearn.model_selection import RandomizedSearchCV,GridSearchCV\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.preprocessing import MinMaxScaler","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:51.310255Z","iopub.execute_input":"2022-07-30T11:15:51.310672Z","iopub.status.idle":"2022-07-30T11:15:51.820422Z","shell.execute_reply.started":"2022-07-30T11:15:51.310637Z","shell.execute_reply":"2022-07-30T11:15:51.819043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models={'linear':LinearRegression(),\n       'decision':DecisionTreeRegressor(),\n       'random':RandomForestRegressor(),\n       'gradient':GradientBoostingRegressor()}","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:54.399144Z","iopub.execute_input":"2022-07-30T11:15:54.399866Z","iopub.status.idle":"2022-07-30T11:15:54.405931Z","shell.execute_reply.started":"2022-07-30T11:15:54.399832Z","shell.execute_reply":"2022-07-30T11:15:54.404843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_score(X,y,models):\n    scores={}\n    for name,model in models.items():\n        score=cross_val_score(model,X,y,cv=8)\n        scores[name]=np.mean(score)\n    return scores","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:15:56.743266Z","iopub.execute_input":"2022-07-30T11:15:56.743695Z","iopub.status.idle":"2022-07-30T11:15:56.750346Z","shell.execute_reply.started":"2022-07-30T11:15:56.743661Z","shell.execute_reply":"2022-07-30T11:15:56.749243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rmsle(train_sample,y_train_sample,val_sample,y_val_sample,models):\n    normalize=MinMaxScaler()\n    true_train=normalize.fit(np.array(y_train_sample).reshape(len(y_train_sample),1)).transform(np.array(y_train_sample).reshape(len(y_train_sample),1))\n    true_val=normalize.fit(np.array(y_val_sample).reshape(len(y_val_sample),1)).transform(np.array(y_val_sample).reshape(len(y_val_sample),1))\n    \n    error={}\n    for name,model in models.items():\n        model.fit(train_sample,y_train_sample)\n        y_train_pred=model.predict(train_sample)\n        y_val_pred=model.predict(val_sample)\n        pred_train=normalize.fit(np.array(y_train_pred).reshape(len(y_train_pred),1)).transform(np.array(y_train_pred).reshape(len(y_train_pred),1))\n        pred_val=normalize.fit(np.array(y_val_pred).reshape(len(y_val_pred),1)).transform(np.array(y_val_pred).reshape(len(y_val_pred),1))\n        error[name+'train_er']=np.sqrt(mean_squared_log_error(true_train,pred_train))\n        error[name+'val_er']=np.sqrt(mean_squared_log_error(true_val,pred_val))\n    return error","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:16:02.262975Z","iopub.execute_input":"2022-07-30T11:16:02.263540Z","iopub.status.idle":"2022-07-30T11:16:02.280488Z","shell.execute_reply.started":"2022-07-30T11:16:02.263470Z","shell.execute_reply":"2022-07-30T11:16:02.279015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result=model_score(X_train_s,y_train_s,models)\nresult","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:16:11.996419Z","iopub.execute_input":"2022-07-30T11:16:11.997009Z","iopub.status.idle":"2022-07-30T11:19:37.161352Z","shell.execute_reply.started":"2022-07-30T11:16:11.996967Z","shell.execute_reply":"2022-07-30T11:19:37.159975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_score=rmsle(X_train_s,y_train_s,X_val_s,y_val_s,models)\neval_score","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:20:08.953572Z","iopub.execute_input":"2022-07-30T11:20:08.954001Z","iopub.status.idle":"2022-07-30T11:20:37.657946Z","shell.execute_reply.started":"2022-07-30T11:20:08.953968Z","shell.execute_reply":"2022-07-30T11:20:37.656581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for better score of model let's do random search of parametrers\ndt_search={'max_depth':[None,4,13,19,26],\n          'min_samples_split':[2,6,12,18,23],\n          'min_samples_leaf':[1,6,12,19,15],\n          'max_leaf_nodes':[None,3,9,17,27]}\nrf_search={'n_estimators':[100,200,350,500,800,1000],\n          'min_samples_split':[2,4,6,8,12],\n          'min_samples_leaf':[1,4,7,3,9],\n          'max_depth':[None,6,8,14,20],\n          'max_leaf_nodes':[None,4,8,12,16,20]\n          }\ngb_search={'n_estimators':[100,500,800,1200],\n          'min_samples_split':[2,8,13,18,25],\n          'min_samples_leaf':[1,5,9,12,17],\n          'max_depth':[None,4,9,14,24],\n          'max_leaf_nodes':[None,3,7,11,19]}","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:22:05.177824Z","iopub.execute_input":"2022-07-30T11:22:05.178252Z","iopub.status.idle":"2022-07-30T11:22:05.189465Z","shell.execute_reply.started":"2022-07-30T11:22:05.178218Z","shell.execute_reply":"2022-07-30T11:22:05.188433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model11=RandomizedSearchCV(RandomForestRegressor(),param_distributions=rf_search,\n                          n_iter=10,cv=5)\nmodel11.fit(X_train_s,y_train_s)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:26:33.666800Z","iopub.execute_input":"2022-07-29T16:26:33.667882Z","iopub.status.idle":"2022-07-29T16:43:40.568874Z","shell.execute_reply.started":"2022-07-29T16:26:33.667840Z","shell.execute_reply":"2022-07-29T16:43:40.567940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model11.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-29T16:44:55.490365Z","iopub.execute_input":"2022-07-29T16:44:55.490831Z","iopub.status.idle":"2022-07-29T16:44:55.497635Z","shell.execute_reply.started":"2022-07-29T16:44:55.490784Z","shell.execute_reply":"2022-07-29T16:44:55.496729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# score improved but error also increased by random forest after tuning\nrandom_tune_model={'decision':DecisionTreeRegressor(min_samples_split=12,\n                     min_samples_leaf=15,\n                     max_leaf_nodes=None,\n                     max_depth=26),\n            'forest':RandomForestRegressor(n_estimators=200,\n                         min_samples_split=4,\n                         min_samples_leaf=3,\n                         max_leaf_nodes=None,\n                         max_depth=14),\n            'gradient':GradientBoostingRegressor(n_estimators=100,\n                                 min_samples_split=2,\n                                 min_samples_leaf=12,\n                                 max_leaf_nodes=11,\n                                 max_depth=24)}","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:22:16.044415Z","iopub.execute_input":"2022-07-30T11:22:16.044830Z","iopub.status.idle":"2022-07-30T11:22:16.052199Z","shell.execute_reply.started":"2022-07-30T11:22:16.044798Z","shell.execute_reply":"2022-07-30T11:22:16.050753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result2=model_score(X_train_s,y_train_s,random_tune_model)\nresult2","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:22:20.530304Z","iopub.execute_input":"2022-07-30T11:22:20.530713Z","iopub.status.idle":"2022-07-30T11:25:56.653741Z","shell.execute_reply.started":"2022-07-30T11:22:20.530681Z","shell.execute_reply":"2022-07-30T11:25:56.652373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_score2=rmsle(X_train_s,y_train_s,X_val_s,y_val_s,random_tune_model)\neval_score2","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:26:35.644841Z","iopub.execute_input":"2022-07-30T11:26:35.645239Z","iopub.status.idle":"2022-07-30T11:27:07.613916Z","shell.execute_reply.started":"2022-07-30T11:26:35.645207Z","shell.execute_reply":"2022-07-30T11:27:07.612736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for grid search\ndt_grid={'max_depth':[10,20,25],\n        'min_samples_split':[10,12,14],\n        'min_samples_leaf':[6,12,15],\n        'max_leaf_nodes':[None,3]}\nrf_grid={'n_estimators':[100,350,500],\n        'min_samples_split':[2,4,6],\n        'min_samples_leaf':[2,4,8],\n        'max_depth':[None,4],\n        'max_leaf_nodes':[5,10,20]}\ngb_grid={'n_estimators':[100,300],\n          'min_samples_split':[2,4,6],\n          'min_samples_leaf':[4,8,12],\n          'max_depth':[None,24],\n          'max_leaf_nodes':[None,3,11]}","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:27:48.024416Z","iopub.execute_input":"2022-07-30T11:27:48.024849Z","iopub.status.idle":"2022-07-30T11:27:48.033621Z","shell.execute_reply.started":"2022-07-30T11:27:48.024816Z","shell.execute_reply":"2022-07-30T11:27:48.032357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_gb_grid=GridSearchCV(GradientBoostingRegressor(),param_grid=gb_grid,cv=8)\nmodel_gb_grid.fit(X_train_s,y_train_s)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:16:53.133275Z","iopub.execute_input":"2022-07-22T08:16:53.134155Z","iopub.status.idle":"2022-07-22T08:52:44.443046Z","shell.execute_reply.started":"2022-07-22T08:16:53.134111Z","shell.execute_reply":"2022-07-22T08:52:44.442037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_gb_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:53:21.347397Z","iopub.execute_input":"2022-07-22T08:53:21.348329Z","iopub.status.idle":"2022-07-22T08:53:21.356244Z","shell.execute_reply.started":"2022-07-22T08:53:21.348293Z","shell.execute_reply":"2022-07-22T08:53:21.354922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# error by gradient boosting decreased from 0.028 to 0.026\ngrid_tune_model={'decision':DecisionTreeRegressor(min_samples_split=10,\n                     min_samples_leaf=12,\n                     max_leaf_nodes=None,\n                     max_depth=25),\n            'forest':RandomForestRegressor(n_estimators=500),\n            'gradient':GradientBoostingRegressor(n_estimators=100,\n                                 min_samples_split=6,\n                                 min_samples_leaf=8,\n                                 max_leaf_nodes=11,\n                                 max_depth=None)}","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:28:04.677640Z","iopub.execute_input":"2022-07-30T11:28:04.678061Z","iopub.status.idle":"2022-07-30T11:28:04.684968Z","shell.execute_reply.started":"2022-07-30T11:28:04.678028Z","shell.execute_reply":"2022-07-30T11:28:04.683837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result3=model_score(X_train_s,y_train_s,grid_tune_model)\nresult3","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:28:10.027563Z","iopub.execute_input":"2022-07-30T11:28:10.027940Z","iopub.status.idle":"2022-07-30T11:42:53.027998Z","shell.execute_reply.started":"2022-07-30T11:28:10.027909Z","shell.execute_reply":"2022-07-30T11:42:53.026796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_score3=rmsle(X_train_s,y_train_s,X_val_s,y_val_s,grid_tune_model)\neval_score3","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:43:26.014465Z","iopub.execute_input":"2022-07-30T11:43:26.014944Z","iopub.status.idle":"2022-07-30T11:45:39.527083Z","shell.execute_reply.started":"2022-07-30T11:43:26.014911Z","shell.execute_reply":"2022-07-30T11:45:39.525903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Prepare test data","metadata":{}},{"cell_type":"code","source":"test_data=pd.read_csv('../input/store-sales-time-series-forecasting/test.csv',parse_dates=['date'])","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:03.145570Z","iopub.execute_input":"2022-07-30T11:46:03.146002Z","iopub.status.idle":"2022-07-30T11:46:03.196322Z","shell.execute_reply.started":"2022-07-30T11:46:03.145968Z","shell.execute_reply":"2022-07-30T11:46:03.194735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:05.569032Z","iopub.execute_input":"2022-07-30T11:46:05.569411Z","iopub.status.idle":"2022-07-30T11:46:05.583906Z","shell.execute_reply.started":"2022-07-30T11:46:05.569379Z","shell.execute_reply":"2022-07-30T11:46:05.582552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=test_data.merge(store_df,on='store_nbr')\ntest_data=test_data.rename(columns={'type':'store_type'})\ntest_data=test_data.set_index('date',drop=False).sort_index()\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:10.124938Z","iopub.execute_input":"2022-07-30T11:46:10.125363Z","iopub.status.idle":"2022-07-30T11:46:10.159261Z","shell.execute_reply.started":"2022-07-30T11:46:10.125327Z","shell.execute_reply":"2022-07-30T11:46:10.158103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=test_data.join(oil_df)\ntest_data=test_data.join(holiday_df)\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:14.666471Z","iopub.execute_input":"2022-07-30T11:46:14.666900Z","iopub.status.idle":"2022-07-30T11:46:14.706211Z","shell.execute_reply.started":"2022-07-30T11:46:14.666868Z","shell.execute_reply":"2022-07-30T11:46:14.704876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=test_data.rename(columns={'dcoilwtico':'oil_price','type':'holiday_type'})","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:20.093016Z","iopub.execute_input":"2022-07-30T11:46:20.093415Z","iopub.status.idle":"2022-07-30T11:46:20.105765Z","shell.execute_reply.started":"2022-07-30T11:46:20.093383Z","shell.execute_reply":"2022-07-30T11:46:20.104543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['year']=test_data['date'].dt.year\ntest_data['month']=test_data['date'].dt.month\ntest_data['day_of_week']=test_data['date'].dt.day_name()\ntest_data['quarter']=test_data['date'].dt.quarter\ntest_data['date_of_month']=test_data['date'].dt.day\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:22.348487Z","iopub.execute_input":"2022-07-30T11:46:22.348923Z","iopub.status.idle":"2022-07-30T11:46:22.404469Z","shell.execute_reply.started":"2022-07-30T11:46:22.348891Z","shell.execute_reply":"2022-07-30T11:46:22.403576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:27.386835Z","iopub.execute_input":"2022-07-30T11:46:27.387230Z","iopub.status.idle":"2022-07-30T11:46:27.413794Z","shell.execute_reply.started":"2022-07-30T11:46:27.387198Z","shell.execute_reply":"2022-07-30T11:46:27.412387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:33.612660Z","iopub.execute_input":"2022-07-30T11:46:33.613424Z","iopub.status.idle":"2022-07-30T11:46:33.645075Z","shell.execute_reply.started":"2022-07-30T11:46:33.613376Z","shell.execute_reply":"2022-07-30T11:46:33.643799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to fill missing oil prices with mean value\ntest_data['oil_price']=test_data.oil_price.fillna(test_data.oil_price.mean())","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:39.757509Z","iopub.execute_input":"2022-07-30T11:46:39.757907Z","iopub.status.idle":"2022-07-30T11:46:39.764975Z","shell.execute_reply.started":"2022-07-30T11:46:39.757875Z","shell.execute_reply":"2022-07-30T11:46:39.763785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's drop columns with missing value as it is difficult to fill values (but we can fill most frequent value)\nX_test=test_data.drop(['holiday_type','locale','locale_name','description','transferred','date'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:45.334034Z","iopub.execute_input":"2022-07-30T11:46:45.334476Z","iopub.status.idle":"2022-07-30T11:46:45.348900Z","shell.execute_reply.started":"2022-07-30T11:46:45.334443Z","shell.execute_reply":"2022-07-30T11:46:45.347705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_=pd.get_dummies(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:46:52.963699Z","iopub.execute_input":"2022-07-30T11:46:52.964074Z","iopub.status.idle":"2022-07-30T11:46:53.005739Z","shell.execute_reply.started":"2022-07-30T11:46:52.964043Z","shell.execute_reply":"2022-07-30T11:46:53.004428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_s=X_test_[:2000]","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:47:05.793233Z","iopub.execute_input":"2022-07-30T11:47:05.793660Z","iopub.status.idle":"2022-07-30T11:47:05.799431Z","shell.execute_reply.started":"2022-07-30T11:47:05.793626Z","shell.execute_reply":"2022-07-30T11:47:05.798180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# minimum error is given by gradient boosting so let's select that for predicting the values\ntuned_model=GradientBoostingRegressor(n_estimators=100,\n                                 min_samples_split=6,\n                                 min_samples_leaf=8,\n                                 max_leaf_nodes=11,\n                                 max_depth=None)\ntuned_model.fit(X_train_s,y_train_s)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:47:09.757686Z","iopub.execute_input":"2022-07-30T11:47:09.758095Z","iopub.status.idle":"2022-07-30T11:47:15.226803Z","shell.execute_reply.started":"2022-07-30T11:47:09.758062Z","shell.execute_reply":"2022-07-30T11:47:15.225688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tuned_model.score(X_train_s,y_train_s),tuned_model.score(X_val_s,y_val_s)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:47:37.816886Z","iopub.execute_input":"2022-07-30T11:47:37.817276Z","iopub.status.idle":"2022-07-30T11:47:37.876942Z","shell.execute_reply.started":"2022-07-30T11:47:37.817245Z","shell.execute_reply":"2022-07-30T11:47:37.875801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rmsle_score(y_pred,y_true):\n    y_pred=MinMaxScaler().fit(np.array(y_pred).reshape(len(y_pred),1)).transform(np.array(y_pred).reshape(len(y_pred),1))\n    y_true=MinMaxScaler().fit(np.array(y_true).reshape(len(y_true),1)).transform(np.array(y_true).reshape(len(y_true),1))\n    return np.sqrt(mean_squared_log_error(y_true,y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:47:43.118824Z","iopub.execute_input":"2022-07-30T11:47:43.119225Z","iopub.status.idle":"2022-07-30T11:47:43.127411Z","shell.execute_reply.started":"2022-07-30T11:47:43.119192Z","shell.execute_reply":"2022-07-30T11:47:43.126208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred=tuned_model.predict(X_train_s)\ny_val_pred=tuned_model.predict(X_val_s)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:47:45.725236Z","iopub.execute_input":"2022-07-30T11:47:45.725657Z","iopub.status.idle":"2022-07-30T11:47:45.780680Z","shell.execute_reply.started":"2022-07-30T11:47:45.725625Z","shell.execute_reply":"2022-07-30T11:47:45.779408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(rmsle_score(y_val_pred,y_val_s))\nprint(rmsle_score(y_train_pred,y_train_s))","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:47:49.184360Z","iopub.execute_input":"2022-07-30T11:47:49.184803Z","iopub.status.idle":"2022-07-30T11:47:49.195433Z","shell.execute_reply.started":"2022-07-30T11:47:49.184768Z","shell.execute_reply":"2022-07-30T11:47:49.194519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_pred=tuned_model.predict(X_test_s)\ny_test_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:47:51.742049Z","iopub.execute_input":"2022-07-30T11:47:51.742844Z","iopub.status.idle":"2022-07-30T11:47:51.757304Z","shell.execute_reply.started":"2022-07-30T11:47:51.742803Z","shell.execute_reply":"2022-07-30T11:47:51.756194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}