{"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-22T09:00:44.910360Z","iopub.execute_input":"2022-07-22T09:00:44.910807Z","iopub.status.idle":"2022-07-22T09:00:44.945499Z","shell.execute_reply.started":"2022-07-22T09:00:44.910704Z","shell.execute_reply":"2022-07-22T09:00:44.944541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**パッケージのインポート**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport random\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:44.950552Z","iopub.execute_input":"2022-07-22T09:00:44.950904Z","iopub.status.idle":"2022-07-22T09:00:46.499880Z","shell.execute_reply.started":"2022-07-22T09:00:44.950872Z","shell.execute_reply":"2022-07-22T09:00:46.498593Z"},"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')\noile = pd.read_csv('../input/store-sales-time-series-forecasting/oil.csv')\nstores = pd.read_csv('../input/store-sales-time-series-forecasting/stores.csv')\nevents = pd.read_csv('../input/store-sales-time-series-forecasting/holidays_events.csv')\ntrans = pd.read_csv('../input/store-sales-time-series-forecasting/transactions.csv')\n\ntest = pd.read_csv('../input/store-sales-time-series-forecasting/test.csv')\n\nsubmission = pd.read_csv('../input/store-sales-time-series-forecasting/sample_submission.csv')\n\n\"\"\"\nid:\ndate:日付\nstore_nbr:商品が販売されている店舗\nfamily:販売された製品のタイプ\nsales:特定の日付における特定の店舗での製品ファミリーの総売上高\nonpromotion:特定の日に店舗で宣伝されていた商品ファミリーのアイテムの総数\ndcoilwtico:毎日の石油価格\ncity:都市\nstate:州\nstore_type:タイプ\ncluster:クラスター（類似したストアのグループ）\nevent_type:休日\nlocale:\nlocale_ame:\ndescription:\ntransferred:\ntransactions:取引量\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:46.501894Z","iopub.execute_input":"2022-07-22T09:00:46.502250Z","iopub.status.idle":"2022-07-22T09:00:50.059373Z","shell.execute_reply.started":"2022-07-22T09:00:46.502215Z","shell.execute_reply":"2022-07-22T09:00:50.058164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oile_train = oile[oile['date'] <= train['date'].max()]\noile_test = oile[oile['date'] > train['date'].max()]","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:50.061129Z","iopub.execute_input":"2022-07-22T09:00:50.061631Z","iopub.status.idle":"2022-07-22T09:00:50.909301Z","shell.execute_reply.started":"2022-07-22T09:00:50.061574Z","shell.execute_reply":"2022-07-22T09:00:50.906984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:50.913628Z","iopub.execute_input":"2022-07-22T09:00:50.914039Z","iopub.status.idle":"2022-07-22T09:00:50.932403Z","shell.execute_reply.started":"2022-07-22T09:00:50.914003Z","shell.execute_reply":"2022-07-22T09:00:50.931420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train.merge(oile_train, on = 'date',how = 'left')\ntrain_data = train_data.merge(stores, on=\"store_nbr\", how=\"left\")\ntrain_data = train_data.merge(events, on=\"date\", how=\"left\")\ntrain_data = train_data.merge(trans, on=[\"date\", \"store_nbr\"], how=\"left\")\n\ntrain_data = train_data.rename(columns={\"type_x\":\"store_type\"})\ntrain_data = train_data.rename(columns={\"type_y\":\"event_type\"})\n\ntrain = train_data","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:50.936116Z","iopub.execute_input":"2022-07-22T09:00:50.936598Z","iopub.status.idle":"2022-07-22T09:00:56.327988Z","shell.execute_reply.started":"2022-07-22T09:00:50.936550Z","shell.execute_reply":"2022-07-22T09:00:56.326600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test.merge(oile_test, on = 'date',how = 'left')\ntest_data = test_data.merge(stores, on=\"store_nbr\", how=\"left\")\ntest_data = test_data.merge(events, on=\"date\", how=\"left\")\ntest_data = test_data.merge(trans, on=[\"date\", \"store_nbr\"], how=\"left\")\n\ntest_data = test_data.rename(columns={\"type_x\":\"store_type\"})\ntest_data = test_data.rename(columns={\"type_y\":\"event_type\"})\n\ntest_col = ['id', 'date', 'store_nbr', 'family', 'onpromotion',\n            'city', 'state', 'store_type', 'cluster', 'event_type',\n       'locale', 'locale_name', 'description', 'transferred', 'transactions']\ntest = test_data","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.329344Z","iopub.execute_input":"2022-07-22T09:00:56.329687Z","iopub.status.idle":"2022-07-22T09:00:56.406825Z","shell.execute_reply.started":"2022-07-22T09:00:56.329657Z","shell.execute_reply":"2022-07-22T09:00:56.405532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.408522Z","iopub.execute_input":"2022-07-22T09:00:56.408888Z","iopub.status.idle":"2022-07-22T09:00:56.432999Z","shell.execute_reply.started":"2022-07-22T09:00:56.408854Z","shell.execute_reply":"2022-07-22T09:00:56.431811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.435026Z","iopub.execute_input":"2022-07-22T09:00:56.435830Z","iopub.status.idle":"2022-07-22T09:00:56.469321Z","shell.execute_reply.started":"2022-07-22T09:00:56.435782Z","shell.execute_reply":"2022-07-22T09:00:56.468108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.470629Z","iopub.execute_input":"2022-07-22T09:00:56.470947Z","iopub.status.idle":"2022-07-22T09:00:56.477496Z","shell.execute_reply.started":"2022-07-22T09:00:56.470919Z","shell.execute_reply":"2022-07-22T09:00:56.476643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.478866Z","iopub.execute_input":"2022-07-22T09:00:56.479489Z","iopub.status.idle":"2022-07-22T09:00:56.495177Z","shell.execute_reply.started":"2022-07-22T09:00:56.479457Z","shell.execute_reply":"2022-07-22T09:00:56.494035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.500406Z","iopub.execute_input":"2022-07-22T09:00:56.501167Z","iopub.status.idle":"2022-07-22T09:00:56.508653Z","shell.execute_reply.started":"2022-07-22T09:00:56.501129Z","shell.execute_reply":"2022-07-22T09:00:56.507916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.509691Z","iopub.execute_input":"2022-07-22T09:00:56.510654Z","iopub.status.idle":"2022-07-22T09:00:56.536030Z","shell.execute_reply.started":"2022-07-22T09:00:56.510619Z","shell.execute_reply":"2022-07-22T09:00:56.534938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:56.537368Z","iopub.execute_input":"2022-07-22T09:00:56.537901Z","iopub.status.idle":"2022-07-22T09:00:59.053329Z","shell.execute_reply.started":"2022-07-22T09:00:56.537867Z","shell.execute_reply":"2022-07-22T09:00:59.052291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.nunique()#種類の個数","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:00:59.055114Z","iopub.execute_input":"2022-07-22T09:00:59.055446Z","iopub.status.idle":"2022-07-22T09:01:02.365669Z","shell.execute_reply.started":"2022-07-22T09:00:59.055417Z","shell.execute_reply":"2022-07-22T09:01:02.364421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.store_type.unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:02.367323Z","iopub.execute_input":"2022-07-22T09:01:02.367810Z","iopub.status.idle":"2022-07-22T09:01:02.592247Z","shell.execute_reply.started":"2022-07-22T09:01:02.367766Z","shell.execute_reply":"2022-07-22T09:01:02.590944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"グラフ","metadata":{}},{"cell_type":"code","source":"df = train.groupby('date').agg({\"sales\" : \"sum\"}).reset_index()\ndf.sales = (df.sales.round())//100","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:02.593659Z","iopub.execute_input":"2022-07-22T09:01:02.594044Z","iopub.status.idle":"2022-07-22T09:01:03.003654Z","shell.execute_reply.started":"2022-07-22T09:01:02.593990Z","shell.execute_reply":"2022-07-22T09:01:03.002460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:03.005272Z","iopub.execute_input":"2022-07-22T09:01:03.005831Z","iopub.status.idle":"2022-07-22T09:01:03.016772Z","shell.execute_reply.started":"2022-07-22T09:01:03.005798Z","shell.execute_reply":"2022-07-22T09:01:03.015455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(df.date,df.sales)\nplt.xlabel(\"date\")\nplt.ylabel(\"sales\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:03.018348Z","iopub.execute_input":"2022-07-22T09:01:03.018707Z","iopub.status.idle":"2022-07-22T09:01:20.016213Z","shell.execute_reply.started":"2022-07-22T09:01:03.018675Z","shell.execute_reply":"2022-07-22T09:01:20.014954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train.groupby('store_nbr').agg({\"sales\" : \"sum\"}).reset_index()\ndf.sales = (df.sales.round())//100000","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:20.017808Z","iopub.execute_input":"2022-07-22T09:01:20.018230Z","iopub.status.idle":"2022-07-22T09:01:20.084591Z","shell.execute_reply.started":"2022-07-22T09:01:20.018192Z","shell.execute_reply":"2022-07-22T09:01:20.083447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(df.store_nbr,df.sales)\nplt.xlabel(\"store_nbs\")\nplt.ylabel(\"sales\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:20.085959Z","iopub.execute_input":"2022-07-22T09:01:20.086303Z","iopub.status.idle":"2022-07-22T09:01:20.365508Z","shell.execute_reply.started":"2022-07-22T09:01:20.086271Z","shell.execute_reply":"2022-07-22T09:01:20.364399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train.groupby('family').agg({\"sales\" : \"sum\"}).reset_index()\ndf.sales = (df.sales.round())//1000\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:20.367307Z","iopub.execute_input":"2022-07-22T09:01:20.367623Z","iopub.status.idle":"2022-07-22T09:01:20.641241Z","shell.execute_reply.started":"2022-07-22T09:01:20.367595Z","shell.execute_reply":"2022-07-22T09:01:20.639997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(df.family,df.sales)\nplt.xlabel(\"store_nbs\")\nplt.ylabel(\"sales\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:20.642689Z","iopub.execute_input":"2022-07-22T09:01:20.643210Z","iopub.status.idle":"2022-07-22T09:01:20.999938Z","shell.execute_reply.started":"2022-07-22T09:01:20.643179Z","shell.execute_reply":"2022-07-22T09:01:20.998469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"回帰","metadata":{}},{"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-22T09:01:21.001342Z","iopub.execute_input":"2022-07-22T09:01:21.001681Z","iopub.status.idle":"2022-07-22T09:01:23.391712Z","shell.execute_reply.started":"2022-07-22T09:01:21.001650Z","shell.execute_reply":"2022-07-22T09:01:23.390653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train = train[train['sales'] > 0]\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:23.393412Z","iopub.execute_input":"2022-07-22T09:01:23.393855Z","iopub.status.idle":"2022-07-22T09:01:23.427376Z","shell.execute_reply.started":"2022-07-22T09:01:23.393809Z","shell.execute_reply":"2022-07-22T09:01:23.426266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train.sales.unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:23.428687Z","iopub.execute_input":"2022-07-22T09:01:23.429635Z","iopub.status.idle":"2022-07-22T09:01:23.436701Z","shell.execute_reply.started":"2022-07-22T09:01:23.429598Z","shell.execute_reply":"2022-07-22T09:01:23.435549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:23.438079Z","iopub.execute_input":"2022-07-22T09:01:23.438926Z","iopub.status.idle":"2022-07-22T09:01:23.480387Z","shell.execute_reply.started":"2022-07-22T09:01:23.438893Z","shell.execute_reply":"2022-07-22T09:01:23.479534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.month.unique().tolist())","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:23.481957Z","iopub.execute_input":"2022-07-22T09:01:23.482300Z","iopub.status.idle":"2022-07-22T09:01:23.506974Z","shell.execute_reply.started":"2022-07-22T09:01:23.482271Z","shell.execute_reply":"2022-07-22T09:01:23.505606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.get_dummies(train,columns = ['family','store_nbr','year','month','day'],drop_first=True)#カテゴリ変数をダミー変数\ntest = pd.get_dummies(test,columns = ['family','store_nbr','year','month','day'],drop_first=True)#カテゴリ変数をダミー変数","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:23.508888Z","iopub.execute_input":"2022-07-22T09:01:23.509358Z","iopub.status.idle":"2022-07-22T09:01:27.996017Z","shell.execute_reply.started":"2022-07-22T09:01:23.509313Z","shell.execute_reply":"2022-07-22T09:01:27.994948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = ['year_2014', 'year_2015', 'year_2016', 'year_2017',\n       'month_2', 'month_3', 'month_4', 'month_5', 'month_6', 'month_7',\n       'month_8', 'month_9', 'month_10', 'month_11', 'month_12', 'day_2',\n       'day_3', 'day_4', 'day_5', 'day_6', 'day_7', 'day_8', 'day_9', 'day_10',\n       'day_11', 'day_12', 'day_13', 'day_14', 'day_15', 'day_16', 'day_17',\n       'day_18', 'day_19', 'day_20', 'day_21', 'day_22', 'day_23', 'day_24',\n       'day_25', 'day_26', 'day_27', 'day_28', 'day_29', 'day_30', 'day_31']\nfor i in col:\n    if i == 'year_2017' or i == 'month_8':\n        test[i] = 1\n    else:\n        test[i] = 0","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:27.997303Z","iopub.execute_input":"2022-07-22T09:01:27.997962Z","iopub.status.idle":"2022-07-22T09:01:28.021961Z","shell.execute_reply.started":"2022-07-22T09:01:27.997927Z","shell.execute_reply":"2022-07-22T09:01:28.020861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.sort_index(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:28.023208Z","iopub.execute_input":"2022-07-22T09:01:28.023524Z","iopub.status.idle":"2022-07-22T09:01:29.445200Z","shell.execute_reply.started":"2022-07-22T09:01:28.023495Z","shell.execute_reply":"2022-07-22T09:01:29.443854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.sort_index(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:29.446426Z","iopub.execute_input":"2022-07-22T09:01:29.446741Z","iopub.status.idle":"2022-07-22T09:01:29.464196Z","shell.execute_reply.started":"2022-07-22T09:01:29.446713Z","shell.execute_reply":"2022-07-22T09:01:29.462766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:29.470982Z","iopub.execute_input":"2022-07-22T09:01:29.471839Z","iopub.status.idle":"2022-07-22T09:01:29.511247Z","shell.execute_reply.started":"2022-07-22T09:01:29.471794Z","shell.execute_reply":"2022-07-22T09:01:29.510430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_standard =  StandardScaler()\ntrain_copied = train.copy()\ntrain_standard.fit(train_copied[['onpromotion']])\ntrain_std = pd.DataFrame(train_standard.transform(train_copied[['onpromotion']]))\n\ntrain['onpromotion'] = train_std","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:29.512563Z","iopub.execute_input":"2022-07-22T09:01:29.513143Z","iopub.status.idle":"2022-07-22T09:01:31.565404Z","shell.execute_reply.started":"2022-07-22T09:01:29.513109Z","shell.execute_reply":"2022-07-22T09:01:31.564141Z"},"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-22T09:01:31.566961Z","iopub.execute_input":"2022-07-22T09:01:31.567826Z","iopub.status.idle":"2022-07-22T09:01:31.594306Z","shell.execute_reply.started":"2022-07-22T09:01:31.567784Z","shell.execute_reply":"2022-07-22T09:01:31.592976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns[-50:]","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:31.596102Z","iopub.execute_input":"2022-07-22T09:01:31.596623Z","iopub.status.idle":"2022-07-22T09:01:31.604655Z","shell.execute_reply.started":"2022-07-22T09:01:31.596586Z","shell.execute_reply":"2022-07-22T09:01:31.603451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns[-50:]","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:31.606943Z","iopub.execute_input":"2022-07-22T09:01:31.607831Z","iopub.status.idle":"2022-07-22T09:01:31.620085Z","shell.execute_reply.started":"2022-07-22T09:01:31.607778Z","shell.execute_reply":"2022-07-22T09:01:31.619174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"#デバック用\ntrain = pd.read_csv('../input/store-sales-time-series-forecasting/train.csv')\ntest = pd.read_csv('../input/store-sales-time-series-forecasting/test.csv')\n\ntrain['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\n\ntrain = pd.get_dummies(train,columns = ['family','store_nbr'],drop_first=True)#カテゴリ変数をダミー変数\ntest = pd.get_dummies(test,columns = ['family','store_nbr'],drop_first=True)#カテゴリ変数をダミー変数\n\ntrain_standard =  StandardScaler()\ntrain_copied = train.copy()\ntrain_standard.fit(train_copied[['onpromotion']])\ntrain_std = pd.DataFrame(train_standard.transform(train_copied[['onpromotion']]))\n\ntrain['onpromotion'] = train_std\n\ntest_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-22T09:01:31.623167Z","iopub.execute_input":"2022-07-22T09:01:31.624446Z","iopub.status.idle":"2022-07-22T09:01:31.637783Z","shell.execute_reply.started":"2022-07-22T09:01:31.624393Z","shell.execute_reply":"2022-07-22T09:01:31.636656Z"},"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\ndrop_col_train = ['id', 'date','sales','store_type',\n       'dcoilwtico', 'city', 'state', 'cluster', 'event_type',\n       'locale', 'locale_name', 'description', 'transferred', 'transactions']\ntrain_x = train.copy()\ntrain_x.drop(columns = drop_col_train , inplace = True , axis = 1)\n\ntrain_y = train.sales\n\n\"\"\"order = np.argsort(np.random.random(train_y.shape))\ntrain_x = train_x[order]\ntrain_y = train_y[order]\"\"\"\n\ndrop_col_test = ['id', 'date','store_type',\n       'dcoilwtico', 'city', 'state', 'cluster', 'event_type',\n       'locale', 'locale_name', 'description', 'transferred', 'transactions']\ntest_x = test.copy()\ntest_x.drop(columns = drop_col_test , inplace = True , axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:31.638991Z","iopub.execute_input":"2022-07-22T09:01:31.640090Z","iopub.status.idle":"2022-07-22T09:01:42.086552Z","shell.execute_reply.started":"2022-07-22T09:01:31.640027Z","shell.execute_reply":"2022-07-22T09:01:42.085374Z"},"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-22T09:01:42.088127Z","iopub.execute_input":"2022-07-22T09:01:42.088561Z","iopub.status.idle":"2022-07-22T09:01:42.095304Z","shell.execute_reply.started":"2022-07-22T09:01:42.088519Z","shell.execute_reply":"2022-07-22T09:01:42.094260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"学習開始","metadata":{}},{"cell_type":"code","source":"model = LinearRegression()\nmodel.fit(train_x,train_y)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:01:42.097003Z","iopub.execute_input":"2022-07-22T09:01:42.097349Z","iopub.status.idle":"2022-07-22T09:02:01.619053Z","shell.execute_reply.started":"2022-07-22T09:01:42.097311Z","shell.execute_reply":"2022-07-22T09:02:01.617719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.coef_","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.625873Z","iopub.execute_input":"2022-07-22T09:02:01.630079Z","iopub.status.idle":"2022-07-22T09:02:01.648445Z","shell.execute_reply.started":"2022-07-22T09:02:01.629990Z","shell.execute_reply":"2022-07-22T09:02:01.647144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.intercept_","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.655589Z","iopub.execute_input":"2022-07-22T09:02:01.656717Z","iopub.status.idle":"2022-07-22T09:02:01.665313Z","shell.execute_reply.started":"2022-07-22T09:02:01.656664Z","shell.execute_reply":"2022-07-22T09:02:01.664084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.667407Z","iopub.execute_input":"2022-07-22T09:02:01.668290Z","iopub.status.idle":"2022-07-22T09:02:01.709029Z","shell.execute_reply.started":"2022-07-22T09:02:01.668240Z","shell.execute_reply":"2022-07-22T09:02:01.707848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_x)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.710773Z","iopub.execute_input":"2022-07-22T09:02:01.711866Z","iopub.status.idle":"2022-07-22T09:02:01.735018Z","shell.execute_reply.started":"2022-07-22T09:02:01.711817Z","shell.execute_reply":"2022-07-22T09:02:01.733571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.736979Z","iopub.execute_input":"2022-07-22T09:02:01.737764Z","iopub.status.idle":"2022-07-22T09:02:01.747594Z","shell.execute_reply.started":"2022-07-22T09:02:01.737700Z","shell.execute_reply":"2022-07-22T09:02:01.746367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.749912Z","iopub.execute_input":"2022-07-22T09:02:01.750955Z","iopub.status.idle":"2022-07-22T09:02:01.787786Z","shell.execute_reply.started":"2022-07-22T09:02:01.750902Z","shell.execute_reply":"2022-07-22T09:02:01.786436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['sales'] = pred\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.790683Z","iopub.execute_input":"2022-07-22T09:02:01.791744Z","iopub.status.idle":"2022-07-22T09:02:01.923704Z","shell.execute_reply.started":"2022-07-22T09:02:01.791682Z","shell.execute_reply":"2022-07-22T09:02:01.922704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-22T09:02:01.925168Z","iopub.execute_input":"2022-07-22T09:02:01.925608Z","iopub.status.idle":"2022-07-22T09:02:01.940798Z","shell.execute_reply.started":"2022-07-22T09:02:01.925572Z","shell.execute_reply":"2022-07-22T09:02:01.939356Z"},"trusted":true},"execution_count":null,"outputs":[]}]}