{"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-21T20:45:17.104747Z","iopub.execute_input":"2022-07-21T20:45:17.105269Z","iopub.status.idle":"2022-07-21T20:45:17.136458Z","shell.execute_reply.started":"2022-07-21T20:45:17.105155Z","shell.execute_reply":"2022-07-21T20:45:17.135253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--read all files---------------------------------------------","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/store-sales-time-series-forecasting/train.csv\")\ntest = pd.read_csv(\"../input/store-sales-time-series-forecasting/test.csv\")\nstores = pd.read_csv(\"../input/store-sales-time-series-forecasting/stores.csv\")\ntransactions = pd.read_csv(\"../input/store-sales-time-series-forecasting/transactions.csv\")\noil = pd.read_csv(\"../input/store-sales-time-series-forecasting/oil.csv\")\nholidays = pd.read_csv(\"../input/store-sales-time-series-forecasting/holidays_events.csv\")\nsubmission = pd.read_csv(\"../input/store-sales-time-series-forecasting/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:21.442922Z","iopub.execute_input":"2022-07-21T20:45:21.443342Z","iopub.status.idle":"2022-07-21T20:45:24.177754Z","shell.execute_reply.started":"2022-07-21T20:45:21.443309Z","shell.execute_reply":"2022-07-21T20:45:24.176765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--format dates-----------------------------------------------","metadata":{}},{"cell_type":"code","source":"train['date'] = pd.to_datetime(train['date'])\ntest['date'] = pd.to_datetime(test['date'])\ntransactions['date'] = pd.to_datetime(transactions['date'])\noil['date'] = pd.to_datetime(oil['date'])\nholidays['date'] = pd.to_datetime(holidays['date'])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:26.896531Z","iopub.execute_input":"2022-07-21T20:45:26.896926Z","iopub.status.idle":"2022-07-21T20:45:27.312784Z","shell.execute_reply.started":"2022-07-21T20:45:26.896894Z","shell.execute_reply":"2022-07-21T20:45:27.311499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--show train and test data-----------------------------------","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:30.201251Z","iopub.execute_input":"2022-07-21T20:45:30.202084Z","iopub.status.idle":"2022-07-21T20:45:30.231492Z","shell.execute_reply.started":"2022-07-21T20:45:30.202012Z","shell.execute_reply":"2022-07-21T20:45:30.230257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:33.328432Z","iopub.execute_input":"2022-07-21T20:45:33.328838Z","iopub.status.idle":"2022-07-21T20:45:33.348841Z","shell.execute_reply.started":"2022-07-21T20:45:33.328803Z","shell.execute_reply":"2022-07-21T20:45:33.348083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:36.208311Z","iopub.execute_input":"2022-07-21T20:45:36.209067Z","iopub.status.idle":"2022-07-21T20:45:36.223557Z","shell.execute_reply.started":"2022-07-21T20:45:36.209017Z","shell.execute_reply":"2022-07-21T20:45:36.222342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--prepare train and test data--------------------------------","metadata":{}},{"cell_type":"code","source":"train_sales = train['sales']\ntrain.drop(columns = ['sales'] , inplace = True , axis = 1)\ntrain_category = pd.get_dummies(train, columns=['store_nbr','family'], drop_first=True)\n\ntest_category = pd.get_dummies(test, columns=['store_nbr','family'], drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:39.267913Z","iopub.execute_input":"2022-07-21T20:45:39.268365Z","iopub.status.idle":"2022-07-21T20:45:41.200280Z","shell.execute_reply.started":"2022-07-21T20:45:39.268329Z","shell.execute_reply":"2022-07-21T20:45:41.198903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--preprocessing train and test data--------------------------","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\ntrain_standard = StandardScaler()\ntest_standard = StandardScaler()\n\ntrain_temp = train_category.copy()\ntrain_standard.fit(train_temp[['onpromotion']])\ntrain_standard_temp = pd.DataFrame(train_standard.transform(train_temp[['onpromotion']]))\ntrain_category [['onpromotion']] = train_standard_temp\n\ntest_temp = test_category.copy()\ntest_standard.fit(test_temp[['onpromotion']])\ntest_standard_temp = pd.DataFrame(test_standard.transform(test_temp[['onpromotion']]))\ntest_category [['onpromotion']] = test_standard_temp","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:44.081132Z","iopub.execute_input":"2022-07-21T20:45:44.081722Z","iopub.status.idle":"2022-07-21T20:45:44.984600Z","shell.execute_reply.started":"2022-07-21T20:45:44.081689Z","shell.execute_reply":"2022-07-21T20:45:44.983314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--remove id and dates on category data-----------------------","metadata":{}},{"cell_type":"code","source":"train_category.drop(columns = ['id','date'], inplace = True , axis = 1)\ntest_category.drop(columns = ['id','date'], inplace = True , axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:48.161877Z","iopub.execute_input":"2022-07-21T20:45:48.162306Z","iopub.status.idle":"2022-07-21T20:45:48.795882Z","shell.execute_reply.started":"2022-07-21T20:45:48.162270Z","shell.execute_reply":"2022-07-21T20:45:48.794669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--view data----","metadata":{}},{"cell_type":"code","source":"train_sales","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:52.109743Z","iopub.execute_input":"2022-07-21T20:45:52.110180Z","iopub.status.idle":"2022-07-21T20:45:52.119422Z","shell.execute_reply.started":"2022-07-21T20:45:52.110142Z","shell.execute_reply":"2022-07-21T20:45:52.118291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_category","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:54.568787Z","iopub.execute_input":"2022-07-21T20:45:54.569342Z","iopub.status.idle":"2022-07-21T20:45:54.803639Z","shell.execute_reply.started":"2022-07-21T20:45:54.569296Z","shell.execute_reply":"2022-07-21T20:45:54.802555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_category","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:45:57.939879Z","iopub.execute_input":"2022-07-21T20:45:57.940737Z","iopub.status.idle":"2022-07-21T20:45:57.965394Z","shell.execute_reply.started":"2022-07-21T20:45:57.940688Z","shell.execute_reply":"2022-07-21T20:45:57.964356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--train model using sklearn LinearRegression-----------------","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\ntrain_model = LinearRegression()\ntrain_model.fit(train_category, train_sales)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:46:03.838197Z","iopub.execute_input":"2022-07-21T20:46:03.838605Z","iopub.status.idle":"2022-07-21T20:46:21.493833Z","shell.execute_reply.started":"2022-07-21T20:46:03.838571Z","shell.execute_reply":"2022-07-21T20:46:21.492641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--predict sales for test-------------------------------------","metadata":{}},{"cell_type":"code","source":"test_predict = train_model.predict(test_category)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:46:24.259322Z","iopub.execute_input":"2022-07-21T20:46:24.260140Z","iopub.status.idle":"2022-07-21T20:46:24.276241Z","shell.execute_reply.started":"2022-07-21T20:46:24.260095Z","shell.execute_reply":"2022-07-21T20:46:24.274363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--change negative prediction value to zero----------","metadata":{}},{"cell_type":"code","source":"i = 0\nfor predict_sales in test_predict:\n if predict_sales<0 :\n    test_predict[i] = 0\n i = i+1","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:46:27.431487Z","iopub.execute_input":"2022-07-21T20:46:27.432056Z","iopub.status.idle":"2022-07-21T20:46:27.452844Z","shell.execute_reply.started":"2022-07-21T20:46:27.431987Z","shell.execute_reply":"2022-07-21T20:46:27.451833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--override prediction in submission--------------------------","metadata":{}},{"cell_type":"code","source":"submission['sales']= test_predict\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:46:30.919710Z","iopub.execute_input":"2022-07-21T20:46:30.920270Z","iopub.status.idle":"2022-07-21T20:46:30.994816Z","shell.execute_reply.started":"2022-07-21T20:46:30.920221Z","shell.execute_reply":"2022-07-21T20:46:30.993585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--show submission data---------------------------------------","metadata":{}},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-21T20:46:33.960228Z","iopub.execute_input":"2022-07-21T20:46:33.960635Z","iopub.status.idle":"2022-07-21T20:46:33.973648Z","shell.execute_reply.started":"2022-07-21T20:46:33.960600Z","shell.execute_reply":"2022-07-21T20:46:33.972769Z"},"trusted":true},"execution_count":null,"outputs":[]}]}