{"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":"markdown","source":"**パッケージのインポート**","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-22T08:12:54.993966Z","iopub.execute_input":"2022-07-22T08:12:54.995234Z","iopub.status.idle":"2022-07-22T08:12:55.003793Z","shell.execute_reply.started":"2022-07-22T08:12:54.995190Z","shell.execute_reply":"2022-07-22T08:12:55.002788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n\nfrom sklearn.preprocessing import MinMaxScaler","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:12:55.005532Z","iopub.execute_input":"2022-07-22T08:12:55.006271Z","iopub.status.idle":"2022-07-22T08:12:55.013314Z","shell.execute_reply.started":"2022-07-22T08:12:55.006227Z","shell.execute_reply":"2022-07-22T08:12:55.012222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**データを読み込む**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/store-sales-time-series-forecasting/train.csv')\ntest = pd.read_csv('../input/store-sales-time-series-forecasting/test.csv')\nsubmission = pd.read_csv('../input/store-sales-time-series-forecasting/sample_submission.csv')\n#stores = pd.read_csv('../input/store-sales-time-series-forecasting/stores.csv')\n#events=pd.read_csv('../input/store-sales-time-series-forecasting/holidays_events.csv')\n#treventsans = pd.read_csv('../input/store-sales-time-series-forecasting/transactions.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:12:55.015281Z","iopub.execute_input":"2022-07-22T08:12:55.016136Z","iopub.status.idle":"2022-07-22T08:12:56.562197Z","shell.execute_reply.started":"2022-07-22T08:12:55.016090Z","shell.execute_reply":"2022-07-22T08:12:56.561253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:12:56.563340Z","iopub.execute_input":"2022-07-22T08:12:56.563645Z","iopub.status.idle":"2022-07-22T08:12:56.580441Z","shell.execute_reply.started":"2022-07-22T08:12:56.563617Z","shell.execute_reply":"2022-07-22T08:12:56.579383Z"},"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-22T08:12:56.582056Z","iopub.execute_input":"2022-07-22T08:12:56.582354Z","iopub.status.idle":"2022-07-22T08:12:58.313769Z","shell.execute_reply.started":"2022-07-22T08:12:56.582327Z","shell.execute_reply":"2022-07-22T08:12:58.312889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:12:58.314880Z","iopub.execute_input":"2022-07-22T08:12:58.315162Z","iopub.status.idle":"2022-07-22T08:12:58.334106Z","shell.execute_reply.started":"2022-07-22T08:12:58.315137Z","shell.execute_reply":"2022-07-22T08:12:58.332895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:12:58.336231Z","iopub.execute_input":"2022-07-22T08:12:58.336519Z","iopub.status.idle":"2022-07-22T08:12:58.344038Z","shell.execute_reply.started":"2022-07-22T08:12:58.336492Z","shell.execute_reply":"2022-07-22T08:12:58.343165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train[\"store_nbr\"]\ny = train[\"sales\"]\n \n# 散布図を描画\nplt.scatter(x, y)\nplt.xlabel(\"store_nbr\")\nplt.ylabel(\"sales\")\nfig.savefig(\"img1.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:12:58.345106Z","iopub.execute_input":"2022-07-22T08:12:58.345811Z","iopub.status.idle":"2022-07-22T08:13:03.622790Z","shell.execute_reply.started":"2022-07-22T08:12:58.345776Z","shell.execute_reply":"2022-07-22T08:13:03.621747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train[\"family\"]\ny = train[\"sales\"]\n \n# 散布図を描画\nplt.scatter(x, y)\nplt.xticks(rotation=90,fontsize=8)\nplt.xlabel(\"family\")\nplt.ylabel(\"sales\")\nfig.savefig(\"img2.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:03.624384Z","iopub.execute_input":"2022-07-22T08:13:03.625111Z","iopub.status.idle":"2022-07-22T08:13:09.843902Z","shell.execute_reply.started":"2022-07-22T08:13:03.625075Z","shell.execute_reply":"2022-07-22T08:13:09.842838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train[\"onpromotion\"]\ny = train[\"sales\"]\n \n# 散布図を描画\nplt.scatter(x, y,s=3)\n#plt.xticks(rotation=90)\nplt.xlabel(\"onpromotion\")\nplt.ylabel(\"sales\")\nfig.savefig(\"img3.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:09.845663Z","iopub.execute_input":"2022-07-22T08:13:09.846088Z","iopub.status.idle":"2022-07-22T08:13:12.279646Z","shell.execute_reply.started":"2022-07-22T08:13:09.846047Z","shell.execute_reply":"2022-07-22T08:13:12.278921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train[\"day\"]\ny = train[\"sales\"]\n \n# 散布図を描画\nplt.scatter(x, y)\nplt.xticks(rotation=90)\nplt.xlabel(\"day\")\nplt.ylabel(\"sales\")\nfig.savefig(\"img4.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:12.280775Z","iopub.execute_input":"2022-07-22T08:13:12.281219Z","iopub.status.idle":"2022-07-22T08:13:17.615457Z","shell.execute_reply.started":"2022-07-22T08:13:12.281190Z","shell.execute_reply":"2022-07-22T08:13:17.614357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train[\"month\"]\ny = train[\"sales\"]\n \n# 散布図を描画\nplt.scatter(x, y)\nplt.xticks(rotation=90)\nplt.xlabel(\"month\")\nplt.ylabel(\"sales\")\nfig.savefig(\"img5.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:17.616572Z","iopub.execute_input":"2022-07-22T08:13:17.616894Z","iopub.status.idle":"2022-07-22T08:13:22.994205Z","shell.execute_reply.started":"2022-07-22T08:13:17.616866Z","shell.execute_reply":"2022-07-22T08:13:22.993147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train[\"year\"]\ny = train[\"sales\"]\n \n# 散布図を描画\nplt.scatter(x, y)\nplt.xticks([2013,2014,2015,2016,2017],rotation=90)\nplt.xlabel(\"year\")\nplt.ylabel(\"sales\")\nfig.savefig(\"img6.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:22.995644Z","iopub.execute_input":"2022-07-22T08:13:22.995950Z","iopub.status.idle":"2022-07-22T08:13:28.268701Z","shell.execute_reply.started":"2022-07-22T08:13:22.995922Z","shell.execute_reply":"2022-07-22T08:13:28.267656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:28.270118Z","iopub.execute_input":"2022-07-22T08:13:28.270414Z","iopub.status.idle":"2022-07-22T08:13:28.289386Z","shell.execute_reply.started":"2022-07-22T08:13:28.270387Z","shell.execute_reply":"2022-07-22T08:13:28.288148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_standard =  MinMaxScaler()\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\n#train_standard =  StandardScaler()\n#train_copied = train.copy()\n#train_standard.fit(train_copied[['onpromotion']])\n#train_std = pd.DataFrame(train_standard.transform(train_copied[['onpromotion']]))\n\n#train['onpromotion'] = train_std","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:28.291496Z","iopub.execute_input":"2022-07-22T08:13:28.292411Z","iopub.status.idle":"2022-07-22T08:13:28.602220Z","shell.execute_reply.started":"2022-07-22T08:13:28.292363Z","shell.execute_reply":"2022-07-22T08:13:28.601117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_standard =  MinMaxScaler()\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-22T08:13:28.603549Z","iopub.execute_input":"2022-07-22T08:13:28.603904Z","iopub.status.idle":"2022-07-22T08:13:28.616902Z","shell.execute_reply.started":"2022-07-22T08:13:28.603875Z","shell.execute_reply":"2022-07-22T08:13:28.616030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:28.618651Z","iopub.execute_input":"2022-07-22T08:13:28.619740Z","iopub.status.idle":"2022-07-22T08:13:28.638305Z","shell.execute_reply.started":"2022-07-22T08:13:28.619696Z","shell.execute_reply":"2022-07-22T08:13:28.637144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:28.641459Z","iopub.execute_input":"2022-07-22T08:13:28.642102Z","iopub.status.idle":"2022-07-22T08:13:28.660537Z","shell.execute_reply.started":"2022-07-22T08:13:28.642054Z","shell.execute_reply":"2022-07-22T08:13:28.659428Z"},"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-22T08:13:28.662144Z","iopub.execute_input":"2022-07-22T08:13:28.662841Z","iopub.status.idle":"2022-07-22T08:13:30.680758Z","shell.execute_reply.started":"2022-07-22T08:13:28.662797Z","shell.execute_reply":"2022-07-22T08:13:30.679888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train.copy()\ntrain_x.drop(columns = ['sales','id','date','year'] , inplace = True , axis = 1)\n\n#train_x.drop(columns = ['year'] , inplace = True , axis = 1)\ntrain_y = train.sales\n\n\ntest_x = test.copy()\ntest_x.drop(columns = ['id','date','year'] , inplace = True , axis = 1)\n\n#train_x.drop(columns = ['year'] , inplace = True , axis = 1)\ntest_x","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:30.682291Z","iopub.execute_input":"2022-07-22T08:13:30.682967Z","iopub.status.idle":"2022-07-22T08:13:31.564197Z","shell.execute_reply.started":"2022-07-22T08:13:30.682923Z","shell.execute_reply":"2022-07-22T08:13:31.563464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=train_x\n#def minmax_norm(df_input):\n#    return (df - df.min()) / ( df.max() - df.min())\n\n#train_x1= minmax_norm(df)\n\n#print(train_x1)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:31.565317Z","iopub.execute_input":"2022-07-22T08:13:31.566150Z","iopub.status.idle":"2022-07-22T08:13:31.762216Z","shell.execute_reply.started":"2022-07-22T08:13:31.566114Z","shell.execute_reply":"2022-07-22T08:13:31.761083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=train_y\n","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:31.764896Z","iopub.execute_input":"2022-07-22T08:13:31.765218Z","iopub.status.idle":"2022-07-22T08:13:31.769616Z","shell.execute_reply.started":"2022-07-22T08:13:31.765189Z","shell.execute_reply":"2022-07-22T08:13:31.768545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df=train_y\n#def minmax_norm(df_input):\n#    return (df - df.min()) / ( df.max() - df.min())\n\n#train_y1= minmax_norm(df)\n\n#print(train_y1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:31.771003Z","iopub.execute_input":"2022-07-22T08:13:31.771299Z","iopub.status.idle":"2022-07-22T08:13:31.779855Z","shell.execute_reply.started":"2022-07-22T08:13:31.771272Z","shell.execute_reply":"2022-07-22T08:13:31.779087Z"},"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-22T08:13:31.782845Z","iopub.execute_input":"2022-07-22T08:13:31.783336Z","iopub.status.idle":"2022-07-22T08:13:48.517485Z","shell.execute_reply.started":"2022-07-22T08:13:31.783299Z","shell.execute_reply":"2022-07-22T08:13:48.516304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.coef_\n#重み確認","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:48.519374Z","iopub.execute_input":"2022-07-22T08:13:48.520643Z","iopub.status.idle":"2022-07-22T08:13:48.530259Z","shell.execute_reply.started":"2022-07-22T08:13:48.520587Z","shell.execute_reply":"2022-07-22T08:13:48.529159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.intercept_\n#切片の確認","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:48.532246Z","iopub.execute_input":"2022-07-22T08:13:48.533081Z","iopub.status.idle":"2022-07-22T08:13:48.545963Z","shell.execute_reply.started":"2022-07-22T08:13:48.533032Z","shell.execute_reply":"2022-07-22T08:13:48.544659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x\n#df=test_x\n#def minmax_norm(df_input):\n#    return (df - df.min()) / ( df.max() - df.min())\n\n#test_x1= minmax_norm(df)\n\n#print(test_x1)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:48.551825Z","iopub.execute_input":"2022-07-22T08:13:48.552869Z","iopub.status.idle":"2022-07-22T08:13:48.597402Z","shell.execute_reply.started":"2022-07-22T08:13:48.552817Z","shell.execute_reply":"2022-07-22T08:13:48.596135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_x)","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:48.603250Z","iopub.execute_input":"2022-07-22T08:13:48.603971Z","iopub.status.idle":"2022-07-22T08:13:48.621457Z","shell.execute_reply.started":"2022-07-22T08:13:48.603910Z","shell.execute_reply":"2022-07-22T08:13:48.619936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:48.623420Z","iopub.execute_input":"2022-07-22T08:13:48.624509Z","iopub.status.idle":"2022-07-22T08:13:48.633474Z","shell.execute_reply.started":"2022-07-22T08:13:48.624455Z","shell.execute_reply":"2022-07-22T08:13:48.632235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:48.635114Z","iopub.execute_input":"2022-07-22T08:13:48.635684Z","iopub.status.idle":"2022-07-22T08:13:48.655121Z","shell.execute_reply.started":"2022-07-22T08:13:48.635635Z","shell.execute_reply":"2022-07-22T08:13:48.653778Z"},"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-22T08:13:48.657254Z","iopub.execute_input":"2022-07-22T08:13:48.658035Z","iopub.status.idle":"2022-07-22T08:13:48.767924Z","shell.execute_reply.started":"2022-07-22T08:13:48.657993Z","shell.execute_reply":"2022-07-22T08:13:48.766908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-22T08:13:48.769709Z","iopub.execute_input":"2022-07-22T08:13:48.770244Z","iopub.status.idle":"2022-07-22T08:13:48.782393Z","shell.execute_reply.started":"2022-07-22T08:13:48.770213Z","shell.execute_reply":"2022-07-22T08:13:48.781273Z"},"trusted":true},"execution_count":null,"outputs":[]}]}