{"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-21T14:38:00.957826Z","iopub.execute_input":"2022-07-21T14:38:00.958351Z","iopub.status.idle":"2022-07-21T14:38:00.971089Z","shell.execute_reply.started":"2022-07-21T14:38:00.958303Z","shell.execute_reply":"2022-07-21T14:38:00.969343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib as mpl\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:01.053865Z","iopub.execute_input":"2022-07-21T14:38:01.054846Z","iopub.status.idle":"2022-07-21T14:38:01.059885Z","shell.execute_reply.started":"2022-07-21T14:38:01.054796Z","shell.execute_reply":"2022-07-21T14:38:01.058658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## データセット読み込み","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/store-sales-time-series-forecasting/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/test.csv\")\noil = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/oil.csv\")\nholi = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/holidays_events.csv\")\nstores = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/stores.csv\")\ntrans = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/transactions.csv\")\nsub = pd.read_csv(\"/kaggle/input/store-sales-time-series-forecasting/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:01.147765Z","iopub.execute_input":"2022-07-21T14:38:01.148624Z","iopub.status.idle":"2022-07-21T14:38:05.048279Z","shell.execute_reply.started":"2022-07-21T14:38:01.148576Z","shell.execute_reply":"2022-07-21T14:38:05.046996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 欠損値の確認","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:05.050699Z","iopub.execute_input":"2022-07-21T14:38:05.051066Z","iopub.status.idle":"2022-07-21T14:38:05.724434Z","shell.execute_reply.started":"2022-07-21T14:38:05.051029Z","shell.execute_reply":"2022-07-21T14:38:05.723064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:05.726179Z","iopub.execute_input":"2022-07-21T14:38:05.726591Z","iopub.status.idle":"2022-07-21T14:38:05.746929Z","shell.execute_reply.started":"2022-07-21T14:38:05.726553Z","shell.execute_reply":"2022-07-21T14:38:05.745374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.merge(train,stores, on = \"store_nbr\")\ntest = pd.merge(test,stores, on = \"store_nbr\")\ntrain = pd.merge(train,holi,how='left', on='date')\ntest = pd.merge(test,holi,how='left', on='date')\ntrain = pd.merge(train,oil,how='left', on='date')\ntest = pd.merge(test,oil,how='left', on='date')\ntrain = pd.merge(train,trans,how='left', on=['date','store_nbr'])\ntest = pd.merge(test,trans,how='left', on=['date','store_nbr'])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:05.751367Z","iopub.execute_input":"2022-07-21T14:38:05.752063Z","iopub.status.idle":"2022-07-21T14:38:11.421319Z","shell.execute_reply.started":"2022-07-21T14:38:05.752015Z","shell.execute_reply":"2022-07-21T14:38:11.419657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:11.423503Z","iopub.execute_input":"2022-07-21T14:38:11.424090Z","iopub.status.idle":"2022-07-21T14:38:11.465072Z","shell.execute_reply.started":"2022-07-21T14:38:11.424049Z","shell.execute_reply":"2022-07-21T14:38:11.464061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:11.466675Z","iopub.execute_input":"2022-07-21T14:38:11.467057Z","iopub.status.idle":"2022-07-21T14:38:11.500570Z","shell.execute_reply.started":"2022-07-21T14:38:11.467019Z","shell.execute_reply":"2022-07-21T14:38:11.499246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.rename(columns={'type_y':'day_type'},inplace=True)\ntrain['day_type'] = train['day_type'].fillna('Normal')\ntest.rename(columns={'type_y':'day_type'},inplace=True)\ntest['day_type'] = test['day_type'].fillna('Normal')\ntrain['transferred'] = train['transferred'].fillna(\"NAN\")\ntest['transferred'] = test['transferred'].fillna(\"NAN\")\ntrain.rename(columns={'dcoilwtico':'oil_price'},inplace=True)\ntrain['oil_price'].interpolate(limit_direction='both', inplace = True)\ntest.rename(columns={'dcoilwtico':'oil_price'},inplace=True)\ntest['oil_price'].interpolate(limit_direction='both', inplace = True)\ntrain['transactions'] = train['transactions'].fillna(0)\ntest['transactions'] = test['transactions'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:11.503560Z","iopub.execute_input":"2022-07-21T14:38:11.504107Z","iopub.status.idle":"2022-07-21T14:38:12.512809Z","shell.execute_reply.started":"2022-07-21T14:38:11.504053Z","shell.execute_reply":"2022-07-21T14:38:12.511309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:12.514691Z","iopub.execute_input":"2022-07-21T14:38:12.515886Z","iopub.status.idle":"2022-07-21T14:38:12.550495Z","shell.execute_reply.started":"2022-07-21T14:38:12.515842Z","shell.execute_reply":"2022-07-21T14:38:12.549302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:12.552197Z","iopub.execute_input":"2022-07-21T14:38:12.552633Z","iopub.status.idle":"2022-07-21T14:38:12.586211Z","shell.execute_reply.started":"2022-07-21T14:38:12.552595Z","shell.execute_reply":"2022-07-21T14:38:12.584605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"national_event\"] = np.where(train[\"locale\"] == \"National\", 1, 0)\ntest[\"national_event\"] = np.where(test[\"locale\"] == \"National\", 1, 0)\n\ntrain[\"regional_event\"] = np.where(train[\"state\"] == train[\"locale_name\"], 1, 0)\ntest[\"regional_event\"] = np.where(test[\"state\"] == test[\"locale_name\"], 1, 0)\n\ntrain[\"local_event\"] = np.where(train[\"city\"] == train[\"locale_name\"], 1, 0)\ntest[\"local_event\"] = np.where(test[\"city\"] == test[\"locale_name\"], 1, 0)\n\ntrain.drop([\"locale\",\"locale_name\",\"description\"], axis = 1, inplace = True)\ntest.drop([\"locale\",\"locale_name\",\"description\"], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:12.590800Z","iopub.execute_input":"2022-07-21T14:38:12.591595Z","iopub.status.idle":"2022-07-21T14:38:15.720048Z","shell.execute_reply.started":"2022-07-21T14:38:12.591546Z","shell.execute_reply":"2022-07-21T14:38:15.718458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:15.722063Z","iopub.execute_input":"2022-07-21T14:38:15.723795Z","iopub.status.idle":"2022-07-21T14:38:15.756313Z","shell.execute_reply.started":"2022-07-21T14:38:15.723711Z","shell.execute_reply":"2022-07-21T14:38:15.754702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:15.758439Z","iopub.execute_input":"2022-07-21T14:38:15.758927Z","iopub.status.idle":"2022-07-21T14:38:15.792745Z","shell.execute_reply.started":"2022-07-21T14:38:15.758876Z","shell.execute_reply":"2022-07-21T14:38:15.791388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## \"date\"を\"day(曜日)\"、\"month\"、\"year\"に分解","metadata":{}},{"cell_type":"code","source":"from datetime import datetime\ntrain['day']  = pd.to_datetime(train['date']).dt.day_name()\ntrain['month']  = pd.to_datetime(train['date']).dt.month\ntrain['year']  = pd.to_datetime(train['date']).dt.year\ntest['day']  = pd.to_datetime(test['date']).dt.day_name()\ntest['month']  = pd.to_datetime(test['date']).dt.month\ntest['year']  = pd.to_datetime(test['date']).dt.year\ntrain.drop('date',axis=1,inplace=True)\ntest.drop('date',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:15.794423Z","iopub.execute_input":"2022-07-21T14:38:15.795446Z","iopub.status.idle":"2022-07-21T14:38:20.679369Z","shell.execute_reply.started":"2022-07-21T14:38:15.795406Z","shell.execute_reply":"2022-07-21T14:38:20.677950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"day_Monday\"] = np.where(train[\"day\"] == \"Monday\", 1, 0)\ntrain[\"day_Tuesday\"] = np.where(train[\"day\"] == \"Tuesday\", 1, 0)\ntrain[\"day_Wednesday\"] = np.where(train[\"day\"] == \"Wednesday\", 1, 0)\ntrain[\"day_Thursday\"] = np.where(train[\"day\"] == \"Thursday\", 1, 0)\ntrain[\"day_Friday\"] = np.where(train[\"day\"] == \"Friday\", 1, 0)\ntrain[\"day_Saturday\"] = np.where(train[\"day\"] == \"Saturday\", 1, 0)\nfor i in range(2, 13):\n    train[\"month_%d\" %i] = np.where(train[\"month\"] == i, 1, 0)\nfor j in range(2014, 2018):\n    train[\"year_%d\" %j] = np.where(train[\"year\"] == j, 1, 0)\ntrain[\"day_type_Bridge\"] = np.where(train[\"day_type\"] == \"Bridge\", 1, 0)\ntrain[\"day_type_Event\"] = np.where(train[\"day_type\"] == \"Event\", 1, 0)\ntrain[\"day_type_Holiday\"] = np.where(train[\"day_type\"] == \"Holiday\", 1, 0)\ntrain[\"day_type_Normal\"] = np.where(train[\"day_type\"] == \"Normal\", 1, 0)\ntrain[\"day_type_Transfer\"] = np.where(train[\"day_type\"] == \"Transfer\", 1, 0)\ntrain[\"day_type_WorkDay\"] = np.where(train[\"day_type\"] == \"WorkDay\", 1, 0)\ntrain[\"transferred_True\"] = np.where(train[\"transferred\"] == \"True\", 1, 0)\ntrain[\"transferred_NAN\"] = np.where(train[\"transferred\"] == \"NAN\", 1, 0)\ntrain.drop('day', axis=1, inplace=True)\ntrain.drop('month', axis=1, inplace=True)\ntrain.drop('year', axis=1, inplace=True)\ntrain.drop('day_type',axis=1,inplace=True)\ntrain.drop('transferred',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:20.681076Z","iopub.execute_input":"2022-07-21T14:38:20.681797Z","iopub.status.idle":"2022-07-21T14:38:34.500010Z","shell.execute_reply.started":"2022-07-21T14:38:20.681754Z","shell.execute_reply":"2022-07-21T14:38:34.498670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"day_Monday\"] = np.where(test[\"day\"] == \"Monday\", 1, 0)\ntest[\"day_Tuesday\"] = np.where(test[\"day\"] == \"Tuesday\", 1, 0)\ntest[\"day_Wednesday\"] = np.where(test[\"day\"] == \"Wednesday\", 1, 0)\ntest[\"day_Thursday\"] = np.where(test[\"day\"] == \"Thursday\", 1, 0)\ntest[\"day_Friday\"] = np.where(test[\"day\"] == \"Friday\", 1, 0)\ntest[\"day_Saturday\"] = np.where(test[\"day\"] == \"Saturday\", 1, 0)\nfor i in range(2, 13):\n    test[\"month_%d\" %i] = np.where(test[\"month\"] == i, 1, 0)\nfor j in range(2014, 2018):\n    test[\"year_%d\" %j] = np.where(test[\"year\"] == j, 1, 0)\ntest[\"day_type_Bridge\"] = np.where(test[\"day_type\"] == \"Bridge\", 1, 0)\ntest[\"day_type_Event\"] = np.where(test[\"day_type\"] == \"Event\", 1, 0)\ntest[\"day_type_Holiday\"] = np.where(test[\"day_type\"] == \"Holiday\", 1, 0)\ntest[\"day_type_Normal\"] = np.where(test[\"day_type\"] == \"Normal\", 1, 0)\ntest[\"day_type_Transfer\"] = np.where(test[\"day_type\"] == \"Transfer\", 1, 0)\ntest[\"day_type_WorkDay\"] = np.where(test[\"day_type\"] == \"WorkDay\", 1, 0)\ntest[\"transferred_True\"] = np.where(test[\"transferred\"] == \"True\", 1, 0)\ntest[\"transferred_NAN\"] = np.where(test[\"transferred\"] == \"NAN\", 1, 0)\ntest.drop('day', axis=1, inplace=True)\ntest.drop('month', axis=1, inplace=True)\ntest.drop('year', axis=1, inplace=True)\ntest.drop('day_type',axis=1,inplace=True)\ntest.drop('transferred',axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:34.502152Z","iopub.execute_input":"2022-07-21T14:38:34.502537Z","iopub.status.idle":"2022-07-21T14:38:34.634059Z","shell.execute_reply.started":"2022-07-21T14:38:34.502505Z","shell.execute_reply":"2022-07-21T14:38:34.632664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  6個の変数をダミー化","metadata":{}},{"cell_type":"code","source":"train = pd.get_dummies(train, columns=[\"store_nbr\",\"family\",\"city\",\"state\",\"type_x\",\"cluster\"], drop_first = True)\ntest = pd.get_dummies(test, columns=[\"store_nbr\",\"family\",\"city\",\"state\",\"type_x\",\"cluster\"], drop_first = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:34.635898Z","iopub.execute_input":"2022-07-21T14:38:34.636318Z","iopub.status.idle":"2022-07-21T14:38:40.044365Z","shell.execute_reply.started":"2022-07-21T14:38:34.636277Z","shell.execute_reply":"2022-07-21T14:38:40.042751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:40.047611Z","iopub.execute_input":"2022-07-21T14:38:40.049921Z","iopub.status.idle":"2022-07-21T14:38:40.890615Z","shell.execute_reply.started":"2022-07-21T14:38:40.049853Z","shell.execute_reply":"2022-07-21T14:38:40.889402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:40.892346Z","iopub.execute_input":"2022-07-21T14:38:40.893908Z","iopub.status.idle":"2022-07-21T14:38:40.926013Z","shell.execute_reply.started":"2022-07-21T14:38:40.893837Z","shell.execute_reply":"2022-07-21T14:38:40.924812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## \"onpromotion\",\"oil_price\",\"transactions\"の標準化","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\ntrain_standard = StandardScaler()\ntrain_copied = train.copy()\ntrain_standard.fit(train_copied[['onpromotion','oil_price','transactions']])\ntrain_std = pd.DataFrame(train_standard.transform(train_copied[['onpromotion','oil_price','transactions']]))\ntrain[['onpromotion','oil_price','transactions']] = train_std\n\ntest_copied = test.copy()\ntest_std = train_standard.transform(test_copied[['onpromotion','oil_price','transactions']])\ntest[['onpromotion','oil_price','transactions']] = test_std","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:40.927599Z","iopub.execute_input":"2022-07-21T14:38:40.927932Z","iopub.status.idle":"2022-07-21T14:38:44.006318Z","shell.execute_reply.started":"2022-07-21T14:38:40.927901Z","shell.execute_reply":"2022-07-21T14:38:44.004309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 目的変数を用意し、\"id\"とトレーニングデータの\"sales\"を削除","metadata":{}},{"cell_type":"code","source":"y_train = train[\"sales\"]\ntrain.drop([\"id\",\"sales\"], axis = 1, inplace = True)\ntest.drop([\"id\"], axis = 1, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:44.008144Z","iopub.execute_input":"2022-07-21T14:38:44.008636Z","iopub.status.idle":"2022-07-21T14:38:47.464745Z","shell.execute_reply.started":"2022-07-21T14:38:44.008596Z","shell.execute_reply":"2022-07-21T14:38:47.463744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:47.466327Z","iopub.execute_input":"2022-07-21T14:38:47.467571Z","iopub.status.idle":"2022-07-21T14:38:48.191466Z","shell.execute_reply.started":"2022-07-21T14:38:47.467527Z","shell.execute_reply":"2022-07-21T14:38:48.190060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:48.193423Z","iopub.execute_input":"2022-07-21T14:38:48.193797Z","iopub.status.idle":"2022-07-21T14:38:48.227631Z","shell.execute_reply.started":"2022-07-21T14:38:48.193763Z","shell.execute_reply":"2022-07-21T14:38:48.226032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## モデル作成","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression as LR\nmodel =  LR()\nmodel.fit(train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:48.262779Z","iopub.execute_input":"2022-07-21T14:38:48.263291Z","iopub.status.idle":"2022-07-21T14:38:55.294972Z","shell.execute_reply.started":"2022-07-21T14:38:48.263219Z","shell.execute_reply":"2022-07-21T14:38:55.293100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predicted = model.predict(test)\ntest_predicted","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:55.296450Z","iopub.status.idle":"2022-07-21T14:38:55.296982Z","shell.execute_reply.started":"2022-07-21T14:38:55.296745Z","shell.execute_reply":"2022-07-21T14:38:55.296767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predicted[test_predicted < 0] = 0","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:55.299442Z","iopub.status.idle":"2022-07-21T14:38:55.300005Z","shell.execute_reply.started":"2022-07-21T14:38:55.299773Z","shell.execute_reply":"2022-07-21T14:38:55.299796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub[\"sales\"] = list(map(int, test_predicted))\nsub.to_csv('submission.csv', index=False)\nsub","metadata":{"execution":{"iopub.status.busy":"2022-07-21T14:38:55.301982Z","iopub.status.idle":"2022-07-21T14:38:55.303143Z","shell.execute_reply.started":"2022-07-21T14:38:55.302905Z","shell.execute_reply":"2022-07-21T14:38:55.302936Z"},"trusted":true},"execution_count":null,"outputs":[]}]}