{"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":"# ライブラリの読み込み\nimport numpy as np\nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\n\nfrom sklearn import preprocessing\nfrom xgboost import XGBRegressor\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_log_error\nfrom sklearn.model_selection import train_test_split\n\n# ファイルパスを取得\nfor dirname, _, filenames in os.walk('/kaggle'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:46:18.452724Z","iopub.execute_input":"2022-07-25T01:46:18.453404Z","iopub.status.idle":"2022-07-25T01:46:18.470384Z","shell.execute_reply.started":"2022-07-25T01:46:18.453344Z","shell.execute_reply":"2022-07-25T01:46:18.468766Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainデータ\ntrain_data = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/train.csv', index_col=0)\n\n# testデータ\ntest_data = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/test.csv', index_col=0)\n\n# holidayデータ\ndata_holi = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/holidays_events.csv')\n\n# storeデータ\ndata_store = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/stores.csv')\n\n# oilデータ\ndata_oil = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/holidays_events.csv')\n\n# transactionデータ\ndata_trans = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/transactions.csv')\n\nsamp_subm = pd.read_csv('/kaggle/input/store-sales-time-series-forecasting/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:46:19.749791Z","iopub.execute_input":"2022-07-25T01:46:19.750309Z","iopub.status.idle":"2022-07-25T01:46:27.705832Z","shell.execute_reply.started":"2022-07-25T01:46:19.750272Z","shell.execute_reply":"2022-07-25T01:46:27.704396Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dataの中身\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:46:32.014955Z","iopub.execute_input":"2022-07-25T01:46:32.016088Z","iopub.status.idle":"2022-07-25T01:46:32.042860Z","shell.execute_reply.started":"2022-07-25T01:46:32.016037Z","shell.execute_reply":"2022-07-25T01:46:32.040049Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# testデータの中身\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:46:32.438094Z","iopub.execute_input":"2022-07-25T01:46:32.438588Z","iopub.status.idle":"2022-07-25T01:46:32.452400Z","shell.execute_reply.started":"2022-07-25T01:46:32.438550Z","shell.execute_reply":"2022-07-25T01:46:32.451074Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 説明変数、目的変数\nfeatures = ['store_nbr', 'family', 'onpromotion']\ntarget = 'sales'","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:46:32.576073Z","iopub.execute_input":"2022-07-25T01:46:32.576576Z","iopub.status.idle":"2022-07-25T01:46:32.583008Z","shell.execute_reply.started":"2022-07-25T01:46:32.576532Z","shell.execute_reply":"2022-07-25T01:46:32.581272Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 説明変数、目的変数\nfeatures = ['store_nbr', 'family', 'onpromotion']\ntarget = 'sales'","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:46:32.806783Z","iopub.execute_input":"2022-07-25T01:46:32.808154Z","iopub.status.idle":"2022-07-25T01:46:32.814723Z","shell.execute_reply.started":"2022-07-25T01:46:32.808080Z","shell.execute_reply":"2022-07-25T01:46:32.813369Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_weekday(s):\n    return s.dayofweek\n\ndef extract_month(s):\n    return s.month\n\ndef extract_year(s):\n    return s.year","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:47:38.043363Z","iopub.execute_input":"2022-07-25T01:47:38.043825Z","iopub.status.idle":"2022-07-25T01:47:38.050691Z","shell.execute_reply.started":"2022-07-25T01:47:38.043790Z","shell.execute_reply":"2022-07-25T01:47:38.049479Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# year,weekday,monthをtrain_data,test_dataに追加\ntrain_data['date'] = pd.to_datetime(train_data['date'])\ntrain_data['weekday'] = train_data['date'].apply(extract_weekday)\ntrain_data['year'] = train_data['date'].apply(extract_year)\ntrain_data['month'] = train_data['date'].apply(extract_month)\n\ntest_data['date'] = pd.to_datetime(test_data['date'])\ntest_data['weekday'] = test_data['date'].apply(extract_weekday)\ntest_data['year'] = test_data['date'].apply(extract_year)\ntest_data['month'] = test_data['date'].apply(extract_month)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:47:39.091351Z","iopub.execute_input":"2022-07-25T01:47:39.092237Z","iopub.status.idle":"2022-07-25T01:48:38.583527Z","shell.execute_reply.started":"2022-07-25T01:47:39.092194Z","shell.execute_reply":"2022-07-25T01:48:38.582106Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 説明変数に追加\nfeatures.append('weekday')\nfeatures.append('year')\nfeatures.append('month')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:48:38.586651Z","iopub.execute_input":"2022-07-25T01:48:38.587191Z","iopub.status.idle":"2022-07-25T01:48:38.595478Z","shell.execute_reply.started":"2022-07-25T01:48:38.587142Z","shell.execute_reply":"2022-07-25T01:48:38.594000Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'family'変数をencoding\nenc = preprocessing.LabelEncoder()\nenc.fit(train_data['family'])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:48:38.597025Z","iopub.execute_input":"2022-07-25T01:48:38.598009Z","iopub.status.idle":"2022-07-25T01:48:38.779157Z","shell.execute_reply.started":"2022-07-25T01:48:38.597957Z","shell.execute_reply":"2022-07-25T01:48:38.777851Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 'family'を0-32のダミー変数に変換\ntrain_data['family'] = enc.transform(train_data['family'])\ntest_data['family']  = enc.transform(test_data['family'])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:48:38.782969Z","iopub.execute_input":"2022-07-25T01:48:38.783467Z","iopub.status.idle":"2022-07-25T01:48:39.629126Z","shell.execute_reply.started":"2022-07-25T01:48:38.783421Z","shell.execute_reply":"2022-07-25T01:48:39.627859Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 学習データ、テストデータ\nX_train = train_data[features]\ny_train = train_data[target]\nX_test  = test_data[features]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:48:39.630799Z","iopub.execute_input":"2022-07-25T01:48:39.631309Z","iopub.status.idle":"2022-07-25T01:48:39.910071Z","shell.execute_reply.started":"2022-07-25T01:48:39.631262Z","shell.execute_reply":"2022-07-25T01:48:39.908889Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_val,y_train,y_val = train_test_split(X_train,y_train,test_size=0.33, random_state=2021)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:48:39.913013Z","iopub.execute_input":"2022-07-25T01:48:39.913328Z","iopub.status.idle":"2022-07-25T01:48:40.644371Z","shell.execute_reply.started":"2022-07-25T01:48:39.913300Z","shell.execute_reply":"2022-07-25T01:48:40.643153Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# XGB Regressionを用いる\nmodel = XGBRegressor(objective='reg:squaredlogerror', n_estimators=200)\nmodel.fit(X_train,y_train)\ny_val_pred = model.predict(X_val)\ny_val_pred = np.where(y_val_pred<0, 0, y_val_pred)\nprint('Root Mean Squared Logaritmic Error:', np.sqrt(mean_squared_log_error(y_val, y_val_pred)))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:48:40.645849Z","iopub.execute_input":"2022-07-25T01:48:40.646476Z","iopub.status.idle":"2022-07-25T01:52:04.594241Z","shell.execute_reply.started":"2022-07-25T01:48:40.646439Z","shell.execute_reply":"2022-07-25T01:52:04.592827Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Linear Regeressionを用いる\nreg = LinearRegression(normalize=True).fit(X_train,y_train)\ny_val_pred = reg.predict(X_val)\ny_val_pred = np.where(y_val_pred<0, 0, y_val_pred)\nprint('Root Mean Squared Logaritmic Error:', np.sqrt(mean_squared_log_error(y_val, y_val_pred)))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:52:05.287866Z","iopub.execute_input":"2022-07-25T01:52:05.288330Z","iopub.status.idle":"2022-07-25T01:52:05.941210Z","shell.execute_reply.started":"2022-07-25T01:52:05.288283Z","shell.execute_reply":"2022-07-25T01:52:05.939864Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 予測(XGB Regression)\ny_test_XGB = model.predict(X_test)\ny_test_REG = model.predict(X_test)\nsamp_subm[target] = (0.8*y_test_XGB+0.2*y_test_REG)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:54:08.466146Z","iopub.execute_input":"2022-07-25T01:54:08.466680Z","iopub.status.idle":"2022-07-25T01:54:08.599826Z","shell.execute_reply.started":"2022-07-25T01:54:08.466636Z","shell.execute_reply":"2022-07-25T01:54:08.598747Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm[target] = np.where(samp_subm[target]<0, 0, samp_subm[target])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:54:19.406298Z","iopub.execute_input":"2022-07-25T01:54:19.406736Z","iopub.status.idle":"2022-07-25T01:54:19.414438Z","shell.execute_reply.started":"2022-07-25T01:54:19.406704Z","shell.execute_reply":"2022-07-25T01:54:19.413357Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:54:19.878332Z","iopub.execute_input":"2022-07-25T01:54:19.879343Z","iopub.status.idle":"2022-07-25T01:54:19.968879Z","shell.execute_reply.started":"2022-07-25T01:54:19.879293Z","shell.execute_reply":"2022-07-25T01:54:19.967176Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T01:54:20.174864Z","iopub.execute_input":"2022-07-25T01:54:20.175351Z","iopub.status.idle":"2022-07-25T01:54:20.192692Z","shell.execute_reply.started":"2022-07-25T01:54:20.175310Z","shell.execute_reply":"2022-07-25T01:54:20.191622Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"editable":false},"execution_count":null,"outputs":[]}]}