{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor as RFR\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-28T11:24:28.812554Z","iopub.execute_input":"2022-07-28T11:24:28.813103Z","iopub.status.idle":"2022-07-28T11:24:28.827766Z","shell.execute_reply.started":"2022-07-28T11:24:28.813064Z","shell.execute_reply":"2022-07-28T11:24:28.826244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 訓練データ\nStoreSalesデータの訓練用サブセットを読み込む","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/store-sales-time-series-forecasting/test.csv')\ntrain = pd.read_csv('../input/store-sales-time-series-forecasting/train.csv')\nholiday = pd.read_csv('../input/store-sales-time-series-forecasting/holidays_events.csv')\noil = pd.read_csv('../input/store-sales-time-series-forecasting/oil.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')\nsubmission = pd.read_csv('../input/store-sales-time-series-forecasting/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:28.830368Z","iopub.execute_input":"2022-07-28T11:24:28.830884Z","iopub.status.idle":"2022-07-28T11:24:31.148801Z","shell.execute_reply.started":"2022-07-28T11:24:28.830840Z","shell.execute_reply":"2022-07-28T11:24:31.147432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:31.151068Z","iopub.execute_input":"2022-07-28T11:24:31.151461Z","iopub.status.idle":"2022-07-28T11:24:31.417683Z","shell.execute_reply.started":"2022-07-28T11:24:31.151428Z","shell.execute_reply":"2022-07-28T11:24:31.416320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train.groupby(['date']).agg({\"sales\" : \"sum\"}).reset_index()\ndf.sales = (df.sales.round())\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:31.419207Z","iopub.execute_input":"2022-07-28T11:24:31.419582Z","iopub.status.idle":"2022-07-28T11:24:31.721086Z","shell.execute_reply.started":"2022-07-28T11:24:31.419549Z","shell.execute_reply":"2022-07-28T11:24:31.719584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['date'] = pd.to_datetime(df['date'])\nplt.plot(df.date,df.sales)\nplt.xlabel(\"date\")\nplt.ylabel(\"sales\")\n\nplt.savefig(\"date.png\")\nplt.show()\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:31.723809Z","iopub.execute_input":"2022-07-28T11:24:31.724215Z","iopub.status.idle":"2022-07-28T11:24:32.080765Z","shell.execute_reply.started":"2022-07-28T11:24:31.724180Z","shell.execute_reply":"2022-07-28T11:24:32.079435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train.groupby(['onpromotion'], as_index= False).agg({\"sales\" : \"mean\"})\nplt.scatter(df.onpromotion,df.sales)\nplt.xlabel(\"onpromotion\")\nplt.ylabel(\"sales\")\nplt.savefig(\"onpromotion.png\")\nplt.show()\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:32.082520Z","iopub.execute_input":"2022-07-28T11:24:32.083846Z","iopub.status.idle":"2022-07-28T11:24:32.442567Z","shell.execute_reply.started":"2022-07-28T11:24:32.083788Z","shell.execute_reply":"2022-07-28T11:24:32.441270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['year']  = pd.to_datetime(train['date']).dt.year\ntrain['month']  = pd.to_datetime(train['date']).dt.month\ntrain['day']  = pd.to_datetime(train['date']).dt.day\n\ntest['year']  = pd.to_datetime(test['date']).dt.year\ntest['month']  = pd.to_datetime(test['date']).dt.month\ntest['day']  = pd.to_datetime(test['date']).dt.day","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:32.444476Z","iopub.execute_input":"2022-07-28T11:24:32.445035Z","iopub.status.idle":"2022-07-28T11:24:34.803832Z","shell.execute_reply.started":"2022-07-28T11:24:32.444995Z","shell.execute_reply":"2022-07-28T11:24:34.801962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(columns = ['store_nbr'], inplace = True , axis = 1)\ntest.drop(columns = ['store_nbr'], inplace = True , axis = 1)\ntrain = pd.get_dummies(train,columns = ['family','year','month','day'],drop_first=True)\ntest = pd.get_dummies(test,columns = ['family','year','month','day'],drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:34.805913Z","iopub.execute_input":"2022-07-28T11:24:34.807483Z","iopub.status.idle":"2022-07-28T11:24:37.399205Z","shell.execute_reply.started":"2022-07-28T11:24:34.807418Z","shell.execute_reply":"2022-07-28T11:24:37.397817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.sort_index(axis=1)\n\ntest = test.sort_index(axis=1)\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:37.401378Z","iopub.execute_input":"2022-07-28T11:24:37.402025Z","iopub.status.idle":"2022-07-28T11:24:38.488412Z","shell.execute_reply.started":"2022-07-28T11:24:37.401985Z","shell.execute_reply":"2022-07-28T11:24:38.486932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_standard =  StandardScaler()\ntrain_copied = train.copy()\ntrain_standard.fit(train_copied[['onpromotion']])\n\ntrain_std = pd.DataFrame(train_standard.transform(train_copied[['onpromotion']]))\n\ntrain['onpromotion'] = train_std","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:38.490553Z","iopub.execute_input":"2022-07-28T11:24:38.491504Z","iopub.status.idle":"2022-07-28T11:24:39.116312Z","shell.execute_reply.started":"2022-07-28T11:24:38.491455Z","shell.execute_reply":"2022-07-28T11:24:39.114874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_standard =  StandardScaler()\ntest_copied = test.copy()\ntest_standard.fit(test_copied[['onpromotion']])\ntest_std = pd.DataFrame(test_standard.transform(test_copied[['onpromotion']]))\n\ntest['onpromotion'] = test_std","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:39.119979Z","iopub.execute_input":"2022-07-28T11:24:39.120763Z","iopub.status.idle":"2022-07-28T11:24:39.139447Z","shell.execute_reply.started":"2022-07-28T11:24:39.120711Z","shell.execute_reply":"2022-07-28T11:24:39.137480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_dif = train[train['sales'] == 0].index.tolist()\ntrain.drop(train.index[index_dif],inplace=True)\ntrain = train.sample(frac = 1)\n\ntrain_x = train.copy()\ntrain_x.drop(columns = ['date', 'sales'], inplace = True , axis = 1)\ntrain_y = train.sales\n\ntest_x = test.copy()\ntest_x.drop(columns = ['date'], inplace = True , axis = 1)\nprint(list(filter(lambda x:x not in test_x.columns, train_x.columns)))\nprint(list(filter(lambda x:x not in train_x.columns, test_x.columns)))\ncolumnList = ['day_10', 'day_11', 'day_12', 'day_13', 'day_14', 'day_15', 'day_16', 'day_2', 'day_3', 'day_4', 'day_5', 'day_6', 'day_7', 'day_8', 'day_9', 'month_10', 'month_11', 'month_12', 'month_2', 'month_3', 'month_4', 'month_5', 'month_6', 'month_7', 'month_8', 'month_9', 'year_2014', 'year_2015', 'year_2016', 'year_2017']\ntrain_x = train_x.drop(columns = columnList)\ntrain_x","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:39.141838Z","iopub.execute_input":"2022-07-28T11:24:39.142796Z","iopub.status.idle":"2022-07-28T11:24:45.526073Z","shell.execute_reply.started":"2022-07-28T11:24:39.142616Z","shell.execute_reply":"2022-07-28T11:24:45.524756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(index_dif))\nprint(len(train_y))\nprint(len(train_y) - len(index_dif))","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:45.527987Z","iopub.execute_input":"2022-07-28T11:24:45.528884Z","iopub.status.idle":"2022-07-28T11:24:45.537619Z","shell.execute_reply.started":"2022-07-28T11:24:45.528823Z","shell.execute_reply":"2022-07-28T11:24:45.536035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = RFR(n_jobs=-1, random_state=2525)\n#model.fit(train_x,train_y)\n\nmodel = LinearRegression()\nmodel.fit(train_x,train_y)\nmodel.coef_\nmodel.intercept_\ntest_x","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:45.539622Z","iopub.execute_input":"2022-07-28T11:24:45.540099Z","iopub.status.idle":"2022-07-28T11:24:53.001969Z","shell.execute_reply.started":"2022-07-28T11:24:45.540060Z","shell.execute_reply":"2022-07-28T11:24:53.000745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_x=test_x.index \npred = model.predict(test_x)\npred","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:53.003926Z","iopub.execute_input":"2022-07-28T11:24:53.004421Z","iopub.status.idle":"2022-07-28T11:24:53.022745Z","shell.execute_reply.started":"2022-07-28T11:24:53.004381Z","shell.execute_reply":"2022-07-28T11:24:53.020601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['sales'] = pred\n#submission = submission.abs()\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:53.025201Z","iopub.execute_input":"2022-07-28T11:24:53.026194Z","iopub.status.idle":"2022-07-28T11:24:53.184279Z","shell.execute_reply.started":"2022-07-28T11:24:53.026129Z","shell.execute_reply":"2022-07-28T11:24:53.183196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-28T11:24:53.185531Z","iopub.execute_input":"2022-07-28T11:24:53.186542Z","iopub.status.idle":"2022-07-28T11:24:53.202171Z","shell.execute_reply.started":"2022-07-28T11:24:53.186503Z","shell.execute_reply":"2022-07-28T11:24:53.200787Z"},"trusted":true},"execution_count":null,"outputs":[]}]}