{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:53:58.128067Z","iopub.execute_input":"2026-07-26T18:53:58.128343Z","iopub.status.idle":"2026-07-26T18:54:00.668487Z","shell.execute_reply.started":"2026-07-26T18:53:58.128310Z","shell.execute_reply":"2026-07-26T18:54:00.667608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = '/kaggle/input/store-sales-time-series-forecasting/'\ntrain = pd.read_csv('/kaggle/input/competitions/Store-Sales-Forecasting/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:00.670512Z","iopub.execute_input":"2026-07-26T18:54:00.670979Z","iopub.status.idle":"2026-07-26T18:54:03.898948Z","shell.execute_reply.started":"2026-07-26T18:54:00.670949Z","shell.execute_reply":"2026-07-26T18:54:03.897807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:03.900066Z","iopub.execute_input":"2026-07-26T18:54:03.900384Z","iopub.status.idle":"2026-07-26T18:54:03.928077Z","shell.execute_reply.started":"2026-07-26T18:54:03.900348Z","shell.execute_reply":"2026-07-26T18:54:03.927096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv(\"/kaggle/input/competitions/Store-Sales-Forecasting/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:03.929305Z","iopub.execute_input":"2026-07-26T18:54:03.929698Z","iopub.status.idle":"2026-07-26T18:54:03.975712Z","shell.execute_reply.started":"2026-07-26T18:54:03.929657Z","shell.execute_reply":"2026-07-26T18:54:03.974655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"date\"] = pd.to_datetime(train[\"date\"])\ntest[\"date\"] = pd.to_datetime(test[\"date\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:03.976850Z","iopub.execute_input":"2026-07-26T18:54:03.977171Z","iopub.status.idle":"2026-07-26T18:54:04.277535Z","shell.execute_reply.started":"2026-07-26T18:54:03.977139Z","shell.execute_reply":"2026-07-26T18:54:04.276511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"dayofweek\"] = train[\"date\"].dt.dayofweek\ntest[\"dayofweek\"] = test[\"date\"].dt.dayofweek","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:04.278761Z","iopub.execute_input":"2026-07-26T18:54:04.279105Z","iopub.status.idle":"2026-07-26T18:54:04.396416Z","shell.execute_reply.started":"2026-07-26T18:54:04.279069Z","shell.execute_reply":"2026-07-26T18:54:04.395608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nsales_by_day = train.groupby(\"dayofweek\")[\"sales\"].mean()\n\nsales_by_day.plot(kind=\"bar\")\nplt.xlabel(\"Day of Week\")\nplt.ylabel(\"Average Sales\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:04.399494Z","iopub.execute_input":"2026-07-26T18:54:04.399865Z","iopub.status.idle":"2026-07-26T18:54:04.721257Z","shell.execute_reply.started":"2026-07-26T18:54:04.399836Z","shell.execute_reply":"2026-07-26T18:54:04.720307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holidays_events = pd.read_csv(\"/kaggle/input/competitions/Store-Sales-Forecasting/holidays_events.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:04.722342Z","iopub.execute_input":"2026-07-26T18:54:04.722972Z","iopub.status.idle":"2026-07-26T18:54:04.736220Z","shell.execute_reply.started":"2026-07-26T18:54:04.722934Z","shell.execute_reply":"2026-07-26T18:54:04.735230Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holiday = holidays_events[[\"date\", \"type\", \"transferred\"]].copy()\n\nholiday[\"date\"] = pd.to_datetime(holiday[\"date\"])\n\ntrain = train.merge(\n    holiday,\n    on=\"date\",\n    how=\"left\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:04.737535Z","iopub.execute_input":"2026-07-26T18:54:04.737886Z","iopub.status.idle":"2026-07-26T18:54:05.586152Z","shell.execute_reply.started":"2026-07-26T18:54:04.737858Z","shell.execute_reply":"2026-07-26T18:54:05.585149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"is_holiday\"] = train[\"type\"].notnull().astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:05.587246Z","iopub.execute_input":"2026-07-26T18:54:05.587530Z","iopub.status.idle":"2026-07-26T18:54:05.717902Z","shell.execute_reply.started":"2026-07-26T18:54:05.587503Z","shell.execute_reply":"2026-07-26T18:54:05.716910Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.groupby(\n    [\"dayofweek\", \"is_holiday\"]\n)[\"sales\"].mean()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:05.719006Z","iopub.execute_input":"2026-07-26T18:54:05.719248Z","iopub.status.idle":"2026-07-26T18:54:05.896300Z","shell.execute_reply.started":"2026-07-26T18:54:05.719225Z","shell.execute_reply":"2026-07-26T18:54:05.895028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\npivot = train.pivot_table(\n    values=\"sales\",\n    index=\"dayofweek\",\n    columns=\"is_holiday\",\n    aggfunc=\"mean\"\n)\n\npivot.plot(kind=\"bar\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:05.897186Z","iopub.execute_input":"2026-07-26T18:54:05.897964Z","iopub.status.idle":"2026-07-26T18:54:06.277286Z","shell.execute_reply.started":"2026-07-26T18:54:05.897932Z","shell.execute_reply":"2026-07-26T18:54:06.276376Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.groupby([\"dayofweek\", \"is_holiday\"]).size()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:06.278407Z","iopub.execute_input":"2026-07-26T18:54:06.278752Z","iopub.status.idle":"2026-07-26T18:54:06.409648Z","shell.execute_reply.started":"2026-07-26T18:54:06.278713Z","shell.execute_reply":"2026-07-26T18:54:06.408662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"date\"] = pd.to_datetime(train[\"date\"])\n\ntrain_data = train[train[\"date\"] < \"2017-01-01\"]\nvalid_data = train[train[\"date\"] >= \"2017-01-01\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:06.410816Z","iopub.execute_input":"2026-07-26T18:54:06.411135Z","iopub.status.idle":"2026-07-26T18:54:06.759386Z","shell.execute_reply.started":"2026-07-26T18:54:06.411108Z","shell.execute_reply":"2026-07-26T18:54:06.758592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"date\"] = pd.to_datetime(train[\"date\"])\n\ntrain_data = train[train[\"date\"] < \"2017-01-01\"]\nvalid_data = train[train[\"date\"] >= \"2017-01-01\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:06.760538Z","iopub.execute_input":"2026-07-26T18:54:06.760874Z","iopub.status.idle":"2026-07-26T18:54:07.178355Z","shell.execute_reply.started":"2026-07-26T18:54:06.760847Z","shell.execute_reply":"2026-07-26T18:54:07.177612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\"dayofweek\"]\n\nX_train = train_data[features]\ny_train = train_data[\"sales\"]\n\nX_valid = valid_data[features]\ny_valid = valid_data[\"sales\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:07.179518Z","iopub.execute_input":"2026-07-26T18:54:07.179830Z","iopub.status.idle":"2026-07-26T18:54:07.197201Z","shell.execute_reply.started":"2026-07-26T18:54:07.179797Z","shell.execute_reply":"2026-07-26T18:54:07.196191Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeRegressor\n\nmodel = DecisionTreeRegressor(random_state=42)\nmodel.fit(X_train, y_train)\n\npred = model.predict(X_valid)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:07.198279Z","iopub.execute_input":"2026-07-26T18:54:07.198622Z","iopub.status.idle":"2026-07-26T18:54:08.700955Z","shell.execute_reply.started":"2026-07-26T18:54:07.198587Z","shell.execute_reply":"2026-07-26T18:54:08.700166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import root_mean_squared_log_error\n\nscore1 = root_mean_squared_log_error(y_valid, pred)\nprint(score1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:08.702039Z","iopub.execute_input":"2026-07-26T18:54:08.702597Z","iopub.status.idle":"2026-07-26T18:54:08.727012Z","shell.execute_reply.started":"2026-07-26T18:54:08.702558Z","shell.execute_reply":"2026-07-26T18:54:08.726163Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"曜日のみの制度↑","metadata":{}},{"cell_type":"code","source":"features = [\"dayofweek\", \"is_holiday\"]\n\nX_train = train_data[features]\nX_valid = valid_data[features]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:32.716155Z","iopub.execute_input":"2026-07-26T18:54:32.716456Z","iopub.status.idle":"2026-07-26T18:54:32.743035Z","shell.execute_reply.started":"2026-07-26T18:54:32.716431Z","shell.execute_reply":"2026-07-26T18:54:32.742042Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = DecisionTreeRegressor(random_state=42)\nmodel.fit(X_train, y_train)\n\npred = model.predict(X_valid)\n\nscore2 = root_mean_squared_log_error(y_valid, pred)\nprint(score2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:35.055993Z","iopub.execute_input":"2026-07-26T18:54:35.056328Z","iopub.status.idle":"2026-07-26T18:54:35.348453Z","shell.execute_reply.started":"2026-07-26T18:54:35.056299Z","shell.execute_reply":"2026-07-26T18:54:35.347625Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"曜日と祝日のみの制度↑","metadata":{}},{"cell_type":"code","source":"train[\"month\"] = train[\"date\"].dt.month","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:37.173769Z","iopub.execute_input":"2026-07-26T18:54:37.174074Z","iopub.status.idle":"2026-07-26T18:54:37.256910Z","shell.execute_reply.started":"2026-07-26T18:54:37.174049Z","shell.execute_reply":"2026-07-26T18:54:37.255941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"monthly_sales = train.groupby(\"month\")[\"sales\"].mean()\nprint(monthly_sales)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:38.571990Z","iopub.execute_input":"2026-07-26T18:54:38.572431Z","iopub.status.idle":"2026-07-26T18:54:38.636929Z","shell.execute_reply.started":"2026-07-26T18:54:38.572401Z","shell.execute_reply":"2026-07-26T18:54:38.636013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nmonthly_sales = train.groupby(\"month\")[\"sales\"].mean()\n\nplt.figure(figsize=(8,5))\nmonthly_sales.plot(kind=\"bar\")\n\nplt.xlabel(\"Month\")\nplt.ylabel(\"Average Sales\")\nplt.title(\"Average Sales by Month\")\nplt.xticks(rotation=0)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:40.677928Z","iopub.execute_input":"2026-07-26T18:54:40.678310Z","iopub.status.idle":"2026-07-26T18:54:40.895226Z","shell.execute_reply.started":"2026-07-26T18:54:40.678277Z","shell.execute_reply":"2026-07-26T18:54:40.894301Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"date\"] = pd.to_datetime(train[\"date\"])\n\ntrain[\"dayofweek\"] = train[\"date\"].dt.dayofweek\ntrain[\"month\"] = train[\"date\"].dt.month\n\ntrain_data = train[train[\"date\"] < \"2017-01-01\"]\nvalid_data = train[train[\"date\"] >= \"2017-01-01\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:42.979072Z","iopub.execute_input":"2026-07-26T18:54:42.979484Z","iopub.status.idle":"2026-07-26T18:54:43.534858Z","shell.execute_reply.started":"2026-07-26T18:54:42.979449Z","shell.execute_reply":"2026-07-26T18:54:43.533736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    \"dayofweek\",\n    \"is_holiday\",\n    \"month\"\n]\n\nX_train = train_data[features]\ny_train = train_data[\"sales\"]\n\nX_valid = valid_data[features]\ny_valid = valid_data[\"sales\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:45.959368Z","iopub.execute_input":"2026-07-26T18:54:45.959966Z","iopub.status.idle":"2026-07-26T18:54:45.995700Z","shell.execute_reply.started":"2026-07-26T18:54:45.959929Z","shell.execute_reply":"2026-07-26T18:54:45.994693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeRegressor\nfrom sklearn.metrics import root_mean_squared_log_error\n\nmodel = DecisionTreeRegressor(random_state=42)\nmodel.fit(X_train, y_train)\n\npred = model.predict(X_valid)\n\nscore3 = root_mean_squared_log_error(y_valid, pred)\nprint(\"RMSLE:\", score3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:48.132536Z","iopub.execute_input":"2026-07-26T18:54:48.132927Z","iopub.status.idle":"2026-07-26T18:54:48.849515Z","shell.execute_reply.started":"2026-07-26T18:54:48.132896Z","shell.execute_reply":"2026-07-26T18:54:48.848589Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"曜日、祝日、月のみの精度","metadata":{}},{"cell_type":"markdown","source":"**ここから7/17**","metadata":{}},{"cell_type":"code","source":"oil = pd.read_csv(\"/kaggle/input/competitions/Store-Sales-Forecasting/oil.csv\")\noil.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:50.791069Z","iopub.execute_input":"2026-07-26T18:54:50.791615Z","iopub.status.idle":"2026-07-26T18:54:50.821323Z","shell.execute_reply.started":"2026-07-26T18:54:50.791572Z","shell.execute_reply":"2026-07-26T18:54:50.820415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oil[\"date\"] = pd.to_datetime(oil[\"date\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:52.897219Z","iopub.execute_input":"2026-07-26T18:54:52.897578Z","iopub.status.idle":"2026-07-26T18:54:52.904136Z","shell.execute_reply.started":"2026-07-26T18:54:52.897518Z","shell.execute_reply":"2026-07-26T18:54:52.903249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = train.merge(oil, on=\"date\", how=\"left\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:54.269987Z","iopub.execute_input":"2026-07-26T18:54:54.270806Z","iopub.status.idle":"2026-07-26T18:54:54.917410Z","shell.execute_reply.started":"2026-07-26T18:54:54.270774Z","shell.execute_reply":"2026-07-26T18:54:54.916395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:56.359310Z","iopub.execute_input":"2026-07-26T18:54:56.359665Z","iopub.status.idle":"2026-07-26T18:54:56.375700Z","shell.execute_reply.started":"2026-07-26T18:54:56.359635Z","shell.execute_reply":"2026-07-26T18:54:56.374784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"dcoilwtico\"] = train[\"dcoilwtico\"].ffill()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:54:59.927826Z","iopub.execute_input":"2026-07-26T18:54:59.928234Z","iopub.status.idle":"2026-07-26T18:54:59.957495Z","shell.execute_reply.started":"2026-07-26T18:54:59.928196Z","shell.execute_reply":"2026-07-26T18:54:59.956617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"dcoilwtico\"] = train[\"dcoilwtico\"].bfill()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:01.447830Z","iopub.execute_input":"2026-07-26T18:55:01.448153Z","iopub.status.idle":"2026-07-26T18:55:01.474019Z","shell.execute_reply.started":"2026-07-26T18:55:01.448125Z","shell.execute_reply":"2026-07-26T18:55:01.473087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    \"dayofweek\",\n    \"is_holiday\",\n    \"month\",\n    \"dcoilwtico\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:04.480827Z","iopub.execute_input":"2026-07-26T18:55:04.481960Z","iopub.status.idle":"2026-07-26T18:55:04.485952Z","shell.execute_reply.started":"2026-07-26T18:55:04.481916Z","shell.execute_reply":"2026-07-26T18:55:04.484996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"dcoilwtico\"] = train[\"dcoilwtico\"].bfill()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:08.155815Z","iopub.execute_input":"2026-07-26T18:55:08.156141Z","iopub.status.idle":"2026-07-26T18:55:08.179804Z","shell.execute_reply.started":"2026-07-26T18:55:08.156112Z","shell.execute_reply":"2026-07-26T18:55:08.178836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(oil.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:09.608853Z","iopub.execute_input":"2026-07-26T18:55:09.609183Z","iopub.status.idle":"2026-07-26T18:55:09.614583Z","shell.execute_reply.started":"2026-07-26T18:55:09.609154Z","shell.execute_reply":"2026-07-26T18:55:09.613758Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:11.169938Z","iopub.execute_input":"2026-07-26T18:55:11.170261Z","iopub.status.idle":"2026-07-26T18:55:11.175170Z","shell.execute_reply.started":"2026-07-26T18:55:11.170235Z","shell.execute_reply":"2026-07-26T18:55:11.174170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = train[train[\"date\"] < \"2017-01-01\"]\nvalid_data = train[train[\"date\"] >= \"2017-01-01\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:13.208069Z","iopub.execute_input":"2026-07-26T18:55:13.208399Z","iopub.status.idle":"2026-07-26T18:55:13.470977Z","shell.execute_reply.started":"2026-07-26T18:55:13.208370Z","shell.execute_reply":"2026-07-26T18:55:13.469968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    \"dayofweek\",\n    \"is_holiday\",\n    \"month\",\n    \"dcoilwtico\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:16.001754Z","iopub.execute_input":"2026-07-26T18:55:16.002175Z","iopub.status.idle":"2026-07-26T18:55:16.006578Z","shell.execute_reply.started":"2026-07-26T18:55:16.002062Z","shell.execute_reply":"2026-07-26T18:55:16.005674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = train_data[features]\nX_valid = valid_data[features]\n\nmodel.fit(X_train, y_train)\npred = model.predict(X_valid)\n\nscore = root_mean_squared_log_error(y_valid, pred)\nprint(score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:17.744798Z","iopub.execute_input":"2026-07-26T18:55:17.745716Z","iopub.status.idle":"2026-07-26T18:55:19.371780Z","shell.execute_reply.started":"2026-07-26T18:55:17.745683Z","shell.execute_reply":"2026-07-26T18:55:19.370894Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    \"dayofweek\",\n    \"is_holiday\",\n    \"month\",\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:22.868744Z","iopub.execute_input":"2026-07-26T18:55:22.869443Z","iopub.status.idle":"2026-07-26T18:55:22.874570Z","shell.execute_reply.started":"2026-07-26T18:55:22.869404Z","shell.execute_reply":"2026-07-26T18:55:22.873778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = train_data[features]\nX_valid = valid_data[features]\n\nmodel.fit(X_train, y_train)\npred = model.predict(X_valid)\n\nscore = root_mean_squared_log_error(y_valid, pred)\nprint(score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:24.287640Z","iopub.execute_input":"2026-07-26T18:55:24.287971Z","iopub.status.idle":"2026-07-26T18:55:25.006895Z","shell.execute_reply.started":"2026-07-26T18:55:24.287942Z","shell.execute_reply":"2026-07-26T18:55:25.005981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from lightgbm import LGBMRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:27.224911Z","iopub.execute_input":"2026-07-26T18:55:27.225267Z","iopub.status.idle":"2026-07-26T18:55:30.844600Z","shell.execute_reply.started":"2026-07-26T18:55:27.225235Z","shell.execute_reply":"2026-07-26T18:55:30.843834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = LGBMRegressor(\n    n_estimators=800,\n    learning_rate=0.1,\n    random_state=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:33.211745Z","iopub.execute_input":"2026-07-26T18:55:33.212495Z","iopub.status.idle":"2026-07-26T18:55:33.216429Z","shell.execute_reply.started":"2026-07-26T18:55:33.212463Z","shell.execute_reply":"2026-07-26T18:55:33.215691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:55:34.918926Z","iopub.execute_input":"2026-07-26T18:55:34.919508Z","iopub.status.idle":"2026-07-26T18:56:10.997279Z","shell.execute_reply.started":"2026-07-26T18:55:34.919463Z","shell.execute_reply":"2026-07-26T18:56:10.996402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = model.predict(X_valid)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:56:27.418470Z","iopub.execute_input":"2026-07-26T18:56:27.418809Z","iopub.status.idle":"2026-07-26T18:56:37.348433Z","shell.execute_reply.started":"2026-07-26T18:56:27.418783Z","shell.execute_reply":"2026-07-26T18:56:37.347674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\npred = np.maximum(pred, 0)\n\nscore = root_mean_squared_log_error(y_valid, pred)\nprint(score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:56:40.289024Z","iopub.execute_input":"2026-07-26T18:56:40.289415Z","iopub.status.idle":"2026-07-26T18:56:40.314951Z","shell.execute_reply.started":"2026-07-26T18:56:40.289387Z","shell.execute_reply":"2026-07-26T18:56:40.314097Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"model = LGBMRegressor(\n    n_estimators=100,\n    learning_rate=0.1,\n    random_state=42\n)\nこのとき3.3358428421125477\n\nmodel = LGBMRegressor(\n    n_estimators=500,\n    learning_rate=0.1,\n    random_state=42\n)\nこのとき3.3352128149653617","metadata":{}},{"cell_type":"code","source":"test[\"month\"] = test[\"date\"].dt.month","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:56:52.419477Z","iopub.execute_input":"2026-07-26T18:56:52.419864Z","iopub.status.idle":"2026-07-26T18:56:52.427295Z","shell.execute_reply.started":"2026-07-26T18:56:52.419834Z","shell.execute_reply":"2026-07-26T18:56:52.426067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features = [\n    \"dayofweek\",\n    \"is_holiday\",\n    \"month\"\n]\n\nX_test = test[features]\n\npred = model.predict(X_test)\npred = np.maximum(pred, 0)\n\nsubmission = pd.DataFrame({\n    \"id\": test[\"id\"],\n    \"sales\": pred\n})\n\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-26T18:58:02.518032Z","iopub.execute_input":"2026-07-26T18:58:02.518457Z","iopub.status.idle":"2026-07-26T18:58:03.308626Z","shell.execute_reply.started":"2026-07-26T18:58:02.518425Z","shell.execute_reply":"2026-07-26T18:58:03.307862Z"}},"outputs":[],"execution_count":null}]}