{"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-22T06:18:25.365199Z","iopub.execute_input":"2022-07-22T06:18:25.365652Z","iopub.status.idle":"2022-07-22T06:18:25.391493Z","shell.execute_reply.started":"2022-07-22T06:18:25.365557Z","shell.execute_reply":"2022-07-22T06:18:25.390591Z"},"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-22T06:18:25.393374Z","iopub.execute_input":"2022-07-22T06:18:25.393949Z","iopub.status.idle":"2022-07-22T06:18:26.128695Z","shell.execute_reply.started":"2022-07-22T06:18:25.393906Z","shell.execute_reply":"2022-07-22T06:18:26.127400Z"},"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-22T06:18:26.129983Z","iopub.execute_input":"2022-07-22T06:18:26.130286Z","iopub.status.idle":"2022-07-22T06:18:29.170043Z","shell.execute_reply.started":"2022-07-22T06:18:26.130258Z","shell.execute_reply":"2022-07-22T06:18:29.169153Z"},"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-22T06:18:29.173493Z","iopub.execute_input":"2022-07-22T06:18:29.173805Z","iopub.status.idle":"2022-07-22T06:18:29.532525Z","shell.execute_reply.started":"2022-07-22T06:18:29.173777Z","shell.execute_reply":"2022-07-22T06:18:29.531598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:29.534879Z","iopub.execute_input":"2022-07-22T06:18:29.535506Z","iopub.status.idle":"2022-07-22T06:18:29.549733Z","shell.execute_reply.started":"2022-07-22T06:18:29.535471Z","shell.execute_reply":"2022-07-22T06:18:29.549001Z"},"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-22T06:18:29.550735Z","iopub.execute_input":"2022-07-22T06:18:29.551207Z","iopub.status.idle":"2022-07-22T06:18:35.234733Z","shell.execute_reply.started":"2022-07-22T06:18:29.551176Z","shell.execute_reply":"2022-07-22T06:18:35.233739Z"},"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-22T06:18:35.236958Z","iopub.execute_input":"2022-07-22T06:18:35.237276Z","iopub.status.idle":"2022-07-22T06:18:35.310922Z","shell.execute_reply.started":"2022-07-22T06:18:35.237245Z","shell.execute_reply":"2022-07-22T06:18:35.309906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:35.312308Z","iopub.execute_input":"2022-07-22T06:18:35.312826Z","iopub.status.idle":"2022-07-22T06:18:35.336132Z","shell.execute_reply.started":"2022-07-22T06:18:35.312784Z","shell.execute_reply":"2022-07-22T06:18:35.335109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:35.339849Z","iopub.execute_input":"2022-07-22T06:18:35.340372Z","iopub.status.idle":"2022-07-22T06:18:35.365336Z","shell.execute_reply.started":"2022-07-22T06:18:35.340331Z","shell.execute_reply":"2022-07-22T06:18:35.364287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:35.366920Z","iopub.execute_input":"2022-07-22T06:18:35.367305Z","iopub.status.idle":"2022-07-22T06:18:35.373781Z","shell.execute_reply.started":"2022-07-22T06:18:35.367267Z","shell.execute_reply":"2022-07-22T06:18:35.372654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:35.375246Z","iopub.execute_input":"2022-07-22T06:18:35.375887Z","iopub.status.idle":"2022-07-22T06:18:35.385119Z","shell.execute_reply.started":"2022-07-22T06:18:35.375846Z","shell.execute_reply":"2022-07-22T06:18:35.384072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:35.386532Z","iopub.execute_input":"2022-07-22T06:18:35.387041Z","iopub.status.idle":"2022-07-22T06:18:35.394982Z","shell.execute_reply.started":"2022-07-22T06:18:35.387012Z","shell.execute_reply":"2022-07-22T06:18:35.394214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:35.396036Z","iopub.execute_input":"2022-07-22T06:18:35.396496Z","iopub.status.idle":"2022-07-22T06:18:35.415090Z","shell.execute_reply.started":"2022-07-22T06:18:35.396468Z","shell.execute_reply":"2022-07-22T06:18:35.414272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:35.416028Z","iopub.execute_input":"2022-07-22T06:18:35.416692Z","iopub.status.idle":"2022-07-22T06:18:36.549749Z","shell.execute_reply.started":"2022-07-22T06:18:35.416659Z","shell.execute_reply":"2022-07-22T06:18:36.548801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.nunique()#種類の個数","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:36.551073Z","iopub.execute_input":"2022-07-22T06:18:36.551989Z","iopub.status.idle":"2022-07-22T06:18:38.544149Z","shell.execute_reply.started":"2022-07-22T06:18:36.551952Z","shell.execute_reply":"2022-07-22T06:18:38.543035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.store_type.unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:38.545604Z","iopub.execute_input":"2022-07-22T06:18:38.545919Z","iopub.status.idle":"2022-07-22T06:18:38.723312Z","shell.execute_reply.started":"2022-07-22T06:18:38.545892Z","shell.execute_reply":"2022-07-22T06:18:38.722346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"グラフ","metadata":{}},{"cell_type":"code","source":"train.describe","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:18:38.725284Z","iopub.execute_input":"2022-07-22T06:18:38.725647Z","iopub.status.idle":"2022-07-22T06:18:38.745600Z","shell.execute_reply.started":"2022-07-22T06:18:38.725616Z","shell.execute_reply":"2022-07-22T06:18:38.744474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train[['sales','store_nbr','onpromotion','dcoilwtico','transactions']]\ndf.corr()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:28:55.501624Z","iopub.execute_input":"2022-07-22T06:28:55.502293Z","iopub.status.idle":"2022-07-22T06:28:56.025998Z","shell.execute_reply.started":"2022-07-22T06:28:55.502259Z","shell.execute_reply":"2022-07-22T06:28:56.025169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(train.dtypes.value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:25:14.761176Z","iopub.execute_input":"2022-07-22T06:25:14.761557Z","iopub.status.idle":"2022-07-22T06:25:14.769197Z","shell.execute_reply.started":"2022-07-22T06:25:14.761525Z","shell.execute_reply":"2022-07-22T06:25:14.767939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(figsize=(12, 9)) \nsns.heatmap(df.corr(), square=True, vmax=1, vmin=-1, center=0)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:29:09.791506Z","iopub.execute_input":"2022-07-22T06:29:09.791891Z","iopub.status.idle":"2022-07-22T06:29:10.329362Z","shell.execute_reply.started":"2022-07-22T06:29:09.791853Z","shell.execute_reply":"2022-07-22T06:29:10.328248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"calendar = pd.DataFrame(index = pd.date_range('2013-01-01', '2017-08-31')).to_period('D')\noil = pd.read_csv('../input/store-sales-time-series-forecasting/oil.csv',\n                  parse_dates = ['date'], infer_datetime_format = True,\n                  index_col = 'date').to_period('D')\noil['avg_oil'] = oil['dcoilwtico'].rolling(7).mean()\ncalendar = calendar.join(oil.avg_oil)\ncalendar['avg_oil'].fillna(method = 'ffill', inplace = True)\ncalendar.dropna(inplace = True)\n\n_ = sns.lineplot(data = oil.dcoilwtico.to_timestamp())","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:01:27.835927Z","iopub.execute_input":"2022-07-22T07:01:27.836463Z","iopub.status.idle":"2022-07-22T07:01:28.097698Z","shell.execute_reply.started":"2022-07-22T07:01:27.836399Z","shell.execute_reply":"2022-07-22T07:01:28.096561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-22T07:42:28.585169Z","iopub.execute_input":"2022-07-22T07:42:28.585576Z","iopub.status.idle":"2022-07-22T07:42:28.852662Z","shell.execute_reply.started":"2022-07-22T07:42:28.585540Z","shell.execute_reply":"2022-07-22T07:42:28.851632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:05.421578Z","iopub.execute_input":"2022-07-22T06:10:05.422063Z","iopub.status.idle":"2022-07-22T06:10:05.431889Z","shell.execute_reply.started":"2022-07-22T06:10:05.422026Z","shell.execute_reply":"2022-07-22T06:10:05.430638Z"},"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-22T07:42:30.403314Z","iopub.execute_input":"2022-07-22T07:42:30.404074Z","iopub.status.idle":"2022-07-22T07:42:46.217397Z","shell.execute_reply.started":"2022-07-22T07:42:30.404026Z","shell.execute_reply":"2022-07-22T07:42:46.216500Z"},"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-22T07:42:46.219168Z","iopub.execute_input":"2022-07-22T07:42:46.219726Z","iopub.status.idle":"2022-07-22T07:42:46.291484Z","shell.execute_reply.started":"2022-07-22T07:42:46.219690Z","shell.execute_reply":"2022-07-22T07:42:46.290315Z"},"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-22T07:42:46.293092Z","iopub.execute_input":"2022-07-22T07:42:46.293449Z","iopub.status.idle":"2022-07-22T07:42:46.558002Z","shell.execute_reply.started":"2022-07-22T07:42:46.293405Z","shell.execute_reply":"2022-07-22T07:42:46.556940Z"},"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-22T07:42:46.836594Z","iopub.execute_input":"2022-07-22T07:42:46.836905Z","iopub.status.idle":"2022-07-22T07:42:47.109772Z","shell.execute_reply.started":"2022-07-22T07:42:46.836867Z","shell.execute_reply":"2022-07-22T07:42:47.108791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(df.family,df.sales)\nplt.xlabel(\"family\")\nplt.ylabel(\"sales\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T07:42:47.110942Z","iopub.execute_input":"2022-07-22T07:42:47.111283Z","iopub.status.idle":"2022-07-22T07:42:47.472463Z","shell.execute_reply.started":"2022-07-22T07:42:47.111251Z","shell.execute_reply":"2022-07-22T07:42:47.471365Z"},"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-22T06:10:23.528516Z","iopub.execute_input":"2022-07-22T06:10:23.529085Z","iopub.status.idle":"2022-07-22T06:10:25.764071Z","shell.execute_reply.started":"2022-07-22T06:10:23.529053Z","shell.execute_reply":"2022-07-22T06:10:25.762741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train = train[train['sales'] > 0]\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:25.765730Z","iopub.execute_input":"2022-07-22T06:10:25.766432Z","iopub.status.idle":"2022-07-22T06:10:25.799552Z","shell.execute_reply.started":"2022-07-22T06:10:25.766396Z","shell.execute_reply":"2022-07-22T06:10:25.798334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train.sales.unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:25.801315Z","iopub.execute_input":"2022-07-22T06:10:25.802512Z","iopub.status.idle":"2022-07-22T06:10:25.810096Z","shell.execute_reply.started":"2022-07-22T06:10:25.802463Z","shell.execute_reply":"2022-07-22T06:10:25.808756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:25.811579Z","iopub.execute_input":"2022-07-22T06:10:25.812438Z","iopub.status.idle":"2022-07-22T06:10:25.845951Z","shell.execute_reply.started":"2022-07-22T06:10:25.812403Z","shell.execute_reply":"2022-07-22T06:10:25.844829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.month.unique().tolist())","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:25.847490Z","iopub.execute_input":"2022-07-22T06:10:25.848142Z","iopub.status.idle":"2022-07-22T06:10:25.873942Z","shell.execute_reply.started":"2022-07-22T06:10:25.848099Z","shell.execute_reply":"2022-07-22T06:10:25.872912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.get_dummies(train,columns = ['family','store_nbr'],drop_first=True)#カテゴリ変数をダミー変数\ntest = pd.get_dummies(test,columns = ['family','store_nbr'],drop_first=True)#カテゴリ変数をダミー変数","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:25.875042Z","iopub.execute_input":"2022-07-22T06:10:25.876052Z","iopub.status.idle":"2022-07-22T06:10:28.917743Z","shell.execute_reply.started":"2022-07-22T06:10:25.876009Z","shell.execute_reply":"2022-07-22T06:10:28.916315Z"},"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-22T06:10:28.925727Z","iopub.execute_input":"2022-07-22T06:10:28.926369Z","iopub.status.idle":"2022-07-22T06:10:28.932847Z","shell.execute_reply.started":"2022-07-22T06:10:28.926331Z","shell.execute_reply":"2022-07-22T06:10:28.931918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train = train.sort_index(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:28.934103Z","iopub.execute_input":"2022-07-22T06:10:28.935129Z","iopub.status.idle":"2022-07-22T06:10:28.943076Z","shell.execute_reply.started":"2022-07-22T06:10:28.935090Z","shell.execute_reply":"2022-07-22T06:10:28.941893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test = test.sort_index(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:28.944807Z","iopub.execute_input":"2022-07-22T06:10:28.945603Z","iopub.status.idle":"2022-07-22T06:10:28.953468Z","shell.execute_reply.started":"2022-07-22T06:10:28.945556Z","shell.execute_reply":"2022-07-22T06:10:28.952426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:28.954958Z","iopub.execute_input":"2022-07-22T06:10:28.955554Z","iopub.status.idle":"2022-07-22T06:10:28.996326Z","shell.execute_reply.started":"2022-07-22T06:10:28.955521Z","shell.execute_reply":"2022-07-22T06:10:28.995129Z"},"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-22T06:10:28.998052Z","iopub.execute_input":"2022-07-22T06:10:28.999251Z","iopub.status.idle":"2022-07-22T06:10:30.959532Z","shell.execute_reply.started":"2022-07-22T06:10:28.999204Z","shell.execute_reply":"2022-07-22T06:10:30.958449Z"},"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-22T06:10:30.960939Z","iopub.execute_input":"2022-07-22T06:10:30.961414Z","iopub.status.idle":"2022-07-22T06:10:30.984135Z","shell.execute_reply.started":"2022-07-22T06:10:30.961369Z","shell.execute_reply":"2022-07-22T06:10:30.982945Z"},"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-22T06:10:31.007591Z","iopub.execute_input":"2022-07-22T06:10:31.008623Z","iopub.status.idle":"2022-07-22T06:10:31.018780Z","shell.execute_reply.started":"2022-07-22T06:10:31.008589Z","shell.execute_reply":"2022-07-22T06:10:31.017940Z"},"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','day',\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','day',\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-22T06:10:31.019969Z","iopub.execute_input":"2022-07-22T06:10:31.020593Z","iopub.status.idle":"2022-07-22T06:10:39.611090Z","shell.execute_reply.started":"2022-07-22T06:10:31.020558Z","shell.execute_reply":"2022-07-22T06:10:39.609843Z"},"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-22T06:10:39.612680Z","iopub.execute_input":"2022-07-22T06:10:39.613014Z","iopub.status.idle":"2022-07-22T06:10:39.619730Z","shell.execute_reply.started":"2022-07-22T06:10:39.612984Z","shell.execute_reply":"2022-07-22T06:10:39.618492Z"},"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-22T06:10:39.621101Z","iopub.execute_input":"2022-07-22T06:10:39.621613Z","iopub.status.idle":"2022-07-22T06:10:52.669815Z","shell.execute_reply.started":"2022-07-22T06:10:39.621577Z","shell.execute_reply":"2022-07-22T06:10:52.668442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.coef_","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:52.671706Z","iopub.execute_input":"2022-07-22T06:10:52.672504Z","iopub.status.idle":"2022-07-22T06:10:52.689483Z","shell.execute_reply.started":"2022-07-22T06:10:52.672450Z","shell.execute_reply":"2022-07-22T06:10:52.687784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.intercept_","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:52.691818Z","iopub.execute_input":"2022-07-22T06:10:52.692308Z","iopub.status.idle":"2022-07-22T06:10:52.702150Z","shell.execute_reply.started":"2022-07-22T06:10:52.692240Z","shell.execute_reply":"2022-07-22T06:10:52.700797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:52.704191Z","iopub.execute_input":"2022-07-22T06:10:52.705649Z","iopub.status.idle":"2022-07-22T06:10:52.752407Z","shell.execute_reply.started":"2022-07-22T06:10:52.705596Z","shell.execute_reply":"2022-07-22T06:10:52.750518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_x)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:52.754735Z","iopub.execute_input":"2022-07-22T06:10:52.756600Z","iopub.status.idle":"2022-07-22T06:10:52.773830Z","shell.execute_reply.started":"2022-07-22T06:10:52.756534Z","shell.execute_reply":"2022-07-22T06:10:52.772083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:52.776006Z","iopub.execute_input":"2022-07-22T06:10:52.777200Z","iopub.status.idle":"2022-07-22T06:10:52.790594Z","shell.execute_reply.started":"2022-07-22T06:10:52.777153Z","shell.execute_reply":"2022-07-22T06:10:52.789109Z"},"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-22T06:10:52.822135Z","iopub.execute_input":"2022-07-22T06:10:52.822870Z","iopub.status.idle":"2022-07-22T06:10:52.969320Z","shell.execute_reply.started":"2022-07-22T06:10:52.822823Z","shell.execute_reply":"2022-07-22T06:10:52.968068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-22T06:10:52.971091Z","iopub.execute_input":"2022-07-22T06:10:52.971499Z","iopub.status.idle":"2022-07-22T06:10:52.985522Z","shell.execute_reply.started":"2022-07-22T06:10:52.971466Z","shell.execute_reply":"2022-07-22T06:10:52.984141Z"},"trusted":true},"execution_count":null,"outputs":[]}]}