{"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":"****Introduction****","metadata":{}},{"cell_type":"markdown","source":"Brick-and-mortar grocery stores are always in a delicate dance with purchasing and sales forecasting. Predict a little over, and grocers are stuck with overstocked, perishable goods. Guess a little under, and popular items quickly sell out, leaving money on the table and customers fuming.\n\nThe problem becomes more complex as retailers add new locations with unique needs, new products, ever transitioning seasonal tastes, and unpredictable product marketing. Corporación Favorita, a large Ecuadorian-based grocery retailer, knows this all too well. They operate hundreds of supermarkets, with over 200,000 different products on their shelves.\n\nCorporación Favorita has challenged the Kaggle community to build a model that more accurately forecasts product sales. They currently rely on subjective forecasting methods with very little data to back them up and very little automation to execute plans. They’re excited to see how machine learning could better ensure they please customers by having just enough of the right products at the right time.","metadata":{}},{"cell_type":"markdown","source":"**Unzipping the zipped files**","metadata":{}},{"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-11-11T06:24:29.237692Z","iopub.execute_input":"2021-11-11T06:24:29.239112Z","iopub.status.idle":"2021-11-11T06:24:29.322429Z","shell.execute_reply.started":"2021-11-11T06:24:29.238086Z","shell.execute_reply":"2021-11-11T06:24:29.320641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr\nimport py7zr","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:24:30.273744Z","iopub.execute_input":"2021-11-11T06:24:30.274354Z","iopub.status.idle":"2021-11-11T06:24:41.251172Z","shell.execute_reply.started":"2021-11-11T06:24:30.274302Z","shell.execute_reply":"2021-11-11T06:24:41.250165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\nfrom subprocess import check_output\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        archive = py7zr.SevenZipFile(os.path.join(dirname, filename), mode='r')\n        archive.extractall(path=\"/kaggle/working\")\n        archive.close()\n\nprint(check_output([\"ls\", \"../working\"]).decode(\"utf8\"))","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:24:41.253097Z","iopub.execute_input":"2021-11-11T06:24:41.253421Z","iopub.status.idle":"2021-11-11T06:26:17.233212Z","shell.execute_reply.started":"2021-11-11T06:24:41.253385Z","shell.execute_reply":"2021-11-11T06:26:17.232073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Importing Libraries**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom scipy.special import boxcox1p\nimport seaborn as sns\nimport sklearn\nfrom sklearn import linear_model\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:27:15.950724Z","iopub.execute_input":"2021-11-11T06:27:15.951121Z","iopub.status.idle":"2021-11-11T06:27:17.366112Z","shell.execute_reply.started":"2021-11-11T06:27:15.951081Z","shell.execute_reply":"2021-11-11T06:27:17.365033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Inputting the train data set into Python environment**","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('./train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:27:21.447661Z","iopub.execute_input":"2021-11-11T06:27:21.44849Z","iopub.status.idle":"2021-11-11T06:28:46.226313Z","shell.execute_reply.started":"2021-11-11T06:27:21.448407Z","shell.execute_reply":"2021-11-11T06:28:46.225476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['date']=pd.to_datetime(df['date']) #Converting date to datetime format","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:29:53.278004Z","iopub.execute_input":"2021-11-11T06:29:53.278231Z","iopub.status.idle":"2021-11-11T06:29:58.406188Z","shell.execute_reply.started":"2021-11-11T06:29:53.278205Z","shell.execute_reply":"2021-11-11T06:29:58.404793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Considering data of July 2013\ntr=df[(df['date'].dt.year==2013)&(df['date'].dt.month==7)] \ntr.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:34:31.048493Z","iopub.execute_input":"2021-11-11T06:34:31.049332Z","iopub.status.idle":"2021-11-11T06:34:54.301333Z","shell.execute_reply.started":"2021-11-11T06:34:31.049287Z","shell.execute_reply":"2021-11-11T06:34:54.300486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:34:58.270879Z","iopub.execute_input":"2021-11-11T06:34:58.27121Z","iopub.status.idle":"2021-11-11T06:34:58.278235Z","shell.execute_reply.started":"2021-11-11T06:34:58.271176Z","shell.execute_reply":"2021-11-11T06:34:58.277432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr['onpromotion']=tr['onpromotion'].fillna('Not Available') #Replace NaN with Not Available\ntr.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:35:00.489186Z","iopub.execute_input":"2021-11-11T06:35:00.490053Z","iopub.status.idle":"2021-11-11T06:35:00.612377Z","shell.execute_reply.started":"2021-11-11T06:35:00.490013Z","shell.execute_reply":"2021-11-11T06:35:00.611526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr1=tr.sample(frac=.1,random_state=1) #Considering a part of data\ntr1","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:35:17.413288Z","iopub.execute_input":"2021-11-11T06:35:17.414283Z","iopub.status.idle":"2021-11-11T06:35:17.537913Z","shell.execute_reply.started":"2021-11-11T06:35:17.414226Z","shell.execute_reply":"2021-11-11T06:35:17.533514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_1= tr1.groupby('date')['unit_sales'].sum().reset_index()\ntr_1","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:35:21.72179Z","iopub.execute_input":"2021-11-11T06:35:21.722094Z","iopub.status.idle":"2021-11-11T06:35:21.746767Z","shell.execute_reply.started":"2021-11-11T06:35:21.722064Z","shell.execute_reply":"2021-11-11T06:35:21.74581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=tr_1[['date']]\ny=tr_1['unit_sales']\nmodel=LinearRegression()\nmodel.fit(X,y)\nprint(\"Intercept:\",model.intercept_)\nprint(\"Coefficient:\",model.coef_)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:43:07.549968Z","iopub.execute_input":"2021-11-11T06:43:07.5503Z","iopub.status.idle":"2021-11-11T06:43:07.583372Z","shell.execute_reply.started":"2021-11-11T06:43:07.550268Z","shell.execute_reply":"2021-11-11T06:43:07.582308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min_best_fit_model=model.intercept_+model.coef_*tr_1.unit_sales\ntr_1['min_best_fit_model']=min_best_fit_model\ntr_1[[\"date\",\"unit_sales\",\"min_best_fit_model\"]]\n","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:55:28.951161Z","iopub.execute_input":"2021-11-11T06:55:28.951592Z","iopub.status.idle":"2021-11-11T06:55:28.973338Z","shell.execute_reply.started":"2021-11-11T06:55:28.951549Z","shell.execute_reply":"2021-11-11T06:55:28.972621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nfig,ax=plt.subplots()\nax.scatter(x='date',y='unit_sales',data=tr_1)\nax.plot(tr_1.date,tr_1.unit_sales,color='red')\nax.set_ylabel(\"Unit Sales\")\nax.set_xlabel(\"Date\")\nax.set_title(\"model line\")","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:43:14.168566Z","iopub.execute_input":"2021-11-11T06:43:14.169087Z","iopub.status.idle":"2021-11-11T06:43:14.54212Z","shell.execute_reply.started":"2021-11-11T06:43:14.169051Z","shell.execute_reply":"2021-11-11T06:43:14.541445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.distplot(tr_1['unit_sales'])","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:55:42.539607Z","iopub.execute_input":"2021-11-11T06:55:42.54012Z","iopub.status.idle":"2021-11-11T06:55:42.805103Z","shell.execute_reply.started":"2021-11-11T06:55:42.540074Z","shell.execute_reply":"2021-11-11T06:55:42.804385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(tr_1[['date']]) # Storing into X \ny = np.array(tr_1[['unit_sales']]) # Storing into y \nprint(X.shape) # Vewing the shape of X\nprint(y.shape) # Vewing the shape of y","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:55:47.780574Z","iopub.execute_input":"2021-11-11T06:55:47.78144Z","iopub.status.idle":"2021-11-11T06:55:47.790211Z","shell.execute_reply.started":"2021-11-11T06:55:47.781393Z","shell.execute_reply":"2021-11-11T06:55:47.789023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(X,y,color=\"red\") # Plot a graph X vs y\nplt.title('Date vs Sales')\nplt.xlabel('Date')\nplt.ylabel('Unit Sales')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:43:38.671216Z","iopub.execute_input":"2021-11-11T06:43:38.671571Z","iopub.status.idle":"2021-11-11T06:43:38.928366Z","shell.execute_reply.started":"2021-11-11T06:43:38.671505Z","shell.execute_reply":"2021-11-11T06:43:38.927551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.25,random_state=15) # Spliting into train & test dataset\nregressor = LinearRegression() # Creating a regressior\nregressor.fit(X_train,y_train) # Fiting the dataset into the model","metadata":{"execution":{"iopub.status.busy":"2021-11-11T05:47:07.19176Z","iopub.execute_input":"2021-11-11T05:47:07.192306Z","iopub.status.idle":"2021-11-11T05:47:07.204977Z","shell.execute_reply.started":"2021-11-11T05:47:07.192179Z","shell.execute_reply":"2021-11-11T05:47:07.203611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_2=pd.read_csv('./stores.csv')\ndf_2.sample(frac=.1)\ndf_2.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:01.216855Z","iopub.execute_input":"2021-11-11T06:56:01.217602Z","iopub.status.idle":"2021-11-11T06:56:01.23336Z","shell.execute_reply.started":"2021-11-11T06:56:01.217554Z","shell.execute_reply":"2021-11-11T06:56:01.232534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nnumbers = df_2[\"city\"].value_counts()\npaths = df_2[\"city\"].value_counts().keys()\nplt.title(\"City \")\nplt.ylabel(\"Different types of cities\")\nplt.xlabel(\"Count\")\nplt.barh(paths, numbers)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:02.062974Z","iopub.execute_input":"2021-11-11T06:56:02.063439Z","iopub.status.idle":"2021-11-11T06:56:02.387352Z","shell.execute_reply.started":"2021-11-11T06:56:02.063406Z","shell.execute_reply":"2021-11-11T06:56:02.386753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nnumbers = df_2[\"state\"].value_counts()\npaths = df_2[\"state\"].value_counts().keys()\nplt.title(\"State \")\nplt.ylabel(\"Different types of states\")\nplt.xlabel(\"Count\")\nplt.barh(paths, numbers)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:03.342998Z","iopub.execute_input":"2021-11-11T06:56:03.343443Z","iopub.status.idle":"2021-11-11T06:56:03.628767Z","shell.execute_reply.started":"2021-11-11T06:56:03.34341Z","shell.execute_reply":"2021-11-11T06:56:03.627678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nnumbers = df_2[\"type\"].value_counts()\npaths = df_2[\"type\"].value_counts().keys()\nplt.title(\"Type \")\nplt.ylabel(\"Different types\")\nplt.xlabel(\"Count\")\nplt.bar(paths, numbers)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:04.562311Z","iopub.execute_input":"2021-11-11T06:56:04.56263Z","iopub.status.idle":"2021-11-11T06:56:04.766143Z","shell.execute_reply.started":"2021-11-11T06:56:04.562594Z","shell.execute_reply":"2021-11-11T06:56:04.765246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_3=pd.read_csv('./items.csv')\ndf_3.sample(frac=.1)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:05.506898Z","iopub.execute_input":"2021-11-11T06:56:05.507228Z","iopub.status.idle":"2021-11-11T06:56:05.53159Z","shell.execute_reply.started":"2021-11-11T06:56:05.507193Z","shell.execute_reply":"2021-11-11T06:56:05.530873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_4=pd.read_csv('./sample_submission.csv')\ndf_4.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:06.591851Z","iopub.execute_input":"2021-11-11T06:56:06.592393Z","iopub.status.idle":"2021-11-11T06:56:07.210477Z","shell.execute_reply.started":"2021-11-11T06:56:06.592356Z","shell.execute_reply":"2021-11-11T06:56:07.209598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_5=pd.read_csv('./holidays_events.csv')\ndf_5.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:08.183328Z","iopub.execute_input":"2021-11-11T06:56:08.184063Z","iopub.status.idle":"2021-11-11T06:56:08.210265Z","shell.execute_reply.started":"2021-11-11T06:56:08.183988Z","shell.execute_reply":"2021-11-11T06:56:08.209135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_6=pd.read_csv('./oil.csv')\ndf_6.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T06:56:09.157825Z","iopub.execute_input":"2021-11-11T06:56:09.158161Z","iopub.status.idle":"2021-11-11T06:56:09.172999Z","shell.execute_reply.started":"2021-11-11T06:56:09.158124Z","shell.execute_reply":"2021-11-11T06:56:09.17228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}