{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:32.142888Z","iopub.execute_input":"2022-08-13T02:19:32.143356Z","iopub.status.idle":"2022-08-13T02:19:32.150213Z","shell.execute_reply.started":"2022-08-13T02:19:32.143316Z","shell.execute_reply":"2022-08-13T02:19:32.148873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/train.csv\")\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:32.545350Z","iopub.execute_input":"2022-08-13T02:19:32.546605Z","iopub.status.idle":"2022-08-13T02:19:32.602689Z","shell.execute_reply.started":"2022-08-13T02:19:32.546549Z","shell.execute_reply":"2022-08-13T02:19:32.601104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.columns)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:32.801843Z","iopub.execute_input":"2022-08-13T02:19:32.802429Z","iopub.status.idle":"2022-08-13T02:19:32.810240Z","shell.execute_reply.started":"2022-08-13T02:19:32.802354Z","shell.execute_reply":"2022-08-13T02:19:32.809276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 住宅価格の統計量\ndf_train[\"SalePrice\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:33.042005Z","iopub.execute_input":"2022-08-13T02:19:33.042629Z","iopub.status.idle":"2022-08-13T02:19:33.055256Z","shell.execute_reply.started":"2022-08-13T02:19:33.042594Z","shell.execute_reply":"2022-08-13T02:19:33.053622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 住宅価格のヒストグラム\nplt.hist(df_train[\"SalePrice\"], bins=20)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:33.289708Z","iopub.execute_input":"2022-08-13T02:19:33.290950Z","iopub.status.idle":"2022-08-13T02:19:33.564179Z","shell.execute_reply.started":"2022-08-13T02:19:33.290894Z","shell.execute_reply":"2022-08-13T02:19:33.562947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# カテゴリ変数をダミー化\ndf_train_dum = pd.get_dummies(df_train)\ndf_train_dum.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:33.648583Z","iopub.execute_input":"2022-08-13T02:19:33.648978Z","iopub.status.idle":"2022-08-13T02:19:33.720684Z","shell.execute_reply.started":"2022-08-13T02:19:33.648947Z","shell.execute_reply":"2022-08-13T02:19:33.718822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 変数同士のSalePriceとの相関を確認し、その絶対値をとり、降順に並び替える\ndf_train_dum_abs = np.abs(df_train_dum.corr()['SalePrice'])\ndf_train_dum_abs = df_train_dum_abs.sort_values(ascending=False)\ndf_train_dum_abs","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:33.875731Z","iopub.execute_input":"2022-08-13T02:19:33.876141Z","iopub.status.idle":"2022-08-13T02:19:34.219447Z","shell.execute_reply.started":"2022-08-13T02:19:33.876107Z","shell.execute_reply":"2022-08-13T02:19:34.218538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SalePriceとの相関が高いトップ１０のインデックス名を取り出す\ntop10 = df_train_dum_abs.iloc[1:11].index\ntop10","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:34.221288Z","iopub.execute_input":"2022-08-13T02:19:34.221695Z","iopub.status.idle":"2022-08-13T02:19:34.230293Z","shell.execute_reply.started":"2022-08-13T02:19:34.221662Z","shell.execute_reply":"2022-08-13T02:19:34.229139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 相関top10の入力変数をX。SalePriceをyとする\nX = df_train_dum[top10]\ny = df_train_dum[\"SalePrice\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:34.379140Z","iopub.execute_input":"2022-08-13T02:19:34.379930Z","iopub.status.idle":"2022-08-13T02:19:34.389383Z","shell.execute_reply.started":"2022-08-13T02:19:34.379875Z","shell.execute_reply":"2022-08-13T02:19:34.388426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# trainデータとtestデータに分ける\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:34.544244Z","iopub.execute_input":"2022-08-13T02:19:34.546159Z","iopub.status.idle":"2022-08-13T02:19:34.554405Z","shell.execute_reply.started":"2022-08-13T02:19:34.546087Z","shell.execute_reply":"2022-08-13T02:19:34.553394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:34.658668Z","iopub.execute_input":"2022-08-13T02:19:34.659804Z","iopub.status.idle":"2022-08-13T02:19:34.667572Z","shell.execute_reply.started":"2022-08-13T02:19:34.659733Z","shell.execute_reply":"2022-08-13T02:19:34.666038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 線形回帰で","metadata":{}},{"cell_type":"code","source":"# 線形回帰でモデルを作ってみる\nfrom sklearn.linear_model import LinearRegression as LR\nreg = LR()\nreg.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:34.822087Z","iopub.execute_input":"2022-08-13T02:19:34.822879Z","iopub.status.idle":"2022-08-13T02:19:34.841439Z","shell.execute_reply.started":"2022-08-13T02:19:34.822826Z","shell.execute_reply":"2022-08-13T02:19:34.839509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# スコアの確認\nprint(reg.score(X_train, y_train))\nprint(reg.score(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:35.050609Z","iopub.execute_input":"2022-08-13T02:19:35.051913Z","iopub.status.idle":"2022-08-13T02:19:35.069741Z","shell.execute_reply.started":"2022-08-13T02:19:35.051869Z","shell.execute_reply":"2022-08-13T02:19:35.068164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## サポートベクターマシンで","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVR\nreg = SVR()\nreg.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:35.255477Z","iopub.execute_input":"2022-08-13T02:19:35.255914Z","iopub.status.idle":"2022-08-13T02:19:35.346674Z","shell.execute_reply.started":"2022-08-13T02:19:35.255881Z","shell.execute_reply":"2022-08-13T02:19:35.344621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# スコアの確認\nprint(reg.score(X_train, y_train))\nprint(reg.score(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:35.429655Z","iopub.execute_input":"2022-08-13T02:19:35.430411Z","iopub.status.idle":"2022-08-13T02:19:35.528245Z","shell.execute_reply.started":"2022-08-13T02:19:35.430374Z","shell.execute_reply":"2022-08-13T02:19:35.526640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 決定木で","metadata":{}},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeRegressor as DTR\nreg = DTR()\nreg.fit(X_train, y_train)\n# スコアの確認\nprint(reg.score(X_train, y_train))\nprint(reg.score(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:35.603390Z","iopub.execute_input":"2022-08-13T02:19:35.604165Z","iopub.status.idle":"2022-08-13T02:19:35.625838Z","shell.execute_reply.started":"2022-08-13T02:19:35.604127Z","shell.execute_reply":"2022-08-13T02:19:35.623898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ランダムフォレストで","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor as RFR\nreg = RFR()\nreg.fit(X_train, y_train)\n# スコアの確認\nprint(reg.score(X_train, y_train))\nprint(reg.score(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:35.769386Z","iopub.execute_input":"2022-08-13T02:19:35.769822Z","iopub.status.idle":"2022-08-13T02:19:36.297036Z","shell.execute_reply.started":"2022-08-13T02:19:35.769790Z","shell.execute_reply":"2022-08-13T02:19:36.295680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- ランダムフォレストが良さそう","metadata":{"execution":{"iopub.status.busy":"2022-08-13T01:48:25.422032Z","iopub.execute_input":"2022-08-13T01:48:25.422427Z","iopub.status.idle":"2022-08-13T01:48:25.453054Z","shell.execute_reply.started":"2022-08-13T01:48:25.422397Z","shell.execute_reply":"2022-08-13T01:48:25.451140Z"}}},{"cell_type":"markdown","source":"## テストデータを前処理する","metadata":{}},{"cell_type":"code","source":"df_test = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/test.csv\")\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:36.300181Z","iopub.execute_input":"2022-08-13T02:19:36.301232Z","iopub.status.idle":"2022-08-13T02:19:36.349995Z","shell.execute_reply.started":"2022-08-13T02:19:36.301167Z","shell.execute_reply":"2022-08-13T02:19:36.348564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# カテゴリ変数をダミー化\ndf_test_dum = pd.get_dummies(df_test)\n# 相関トップ１０を入力変数とする\ndf_test_dum_top10 = df_test_dum[top10]\ndf_test_dum_top10.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:36.352036Z","iopub.execute_input":"2022-08-13T02:19:36.352581Z","iopub.status.idle":"2022-08-13T02:19:36.419803Z","shell.execute_reply.started":"2022-08-13T02:19:36.352533Z","shell.execute_reply":"2022-08-13T02:19:36.418384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 全てのtrainデータで学習する。ランダムフォレスト","metadata":{}},{"cell_type":"code","source":"reg = RFR()\nreg.fit(X, y)\n# スコアの確認\nprint(reg.score(X, y))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:36.422645Z","iopub.execute_input":"2022-08-13T02:19:36.423412Z","iopub.status.idle":"2022-08-13T02:19:37.078759Z","shell.execute_reply.started":"2022-08-13T02:19:36.423361Z","shell.execute_reply":"2022-08-13T02:19:37.077373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## testデータを予想する","metadata":{}},{"cell_type":"code","source":"# 欠損値の対応\ndf_test_dum_top10.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:37.080533Z","iopub.execute_input":"2022-08-13T02:19:37.081742Z","iopub.status.idle":"2022-08-13T02:19:37.094089Z","shell.execute_reply.started":"2022-08-13T02:19:37.081688Z","shell.execute_reply":"2022-08-13T02:19:37.092269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_dum_top10[\"GarageCars\"].fillna(df_test_dum_top10[\"GarageCars\"].median(), inplace=True)\ndf_test_dum_top10[\"GarageArea\"].fillna(df_test_dum_top10[\"GarageArea\"].median(), inplace=True)\ndf_test_dum_top10[\"TotalBsmtSF\"].fillna(df_test_dum_top10[\"TotalBsmtSF\"].median(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:19:52.854138Z","iopub.execute_input":"2022-08-13T02:19:52.854569Z","iopub.status.idle":"2022-08-13T02:19:52.865901Z","shell.execute_reply.started":"2022-08-13T02:19:52.854536Z","shell.execute_reply":"2022-08-13T02:19:52.864572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test_dum_top10.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:21:48.366600Z","iopub.execute_input":"2022-08-13T02:21:48.367426Z","iopub.status.idle":"2022-08-13T02:21:48.379248Z","shell.execute_reply.started":"2022-08-13T02:21:48.367390Z","shell.execute_reply":"2022-08-13T02:21:48.378077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = reg.predict(df_test_dum_top10)\npred","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:22:33.448094Z","iopub.execute_input":"2022-08-13T02:22:33.448513Z","iopub.status.idle":"2022-08-13T02:22:33.508891Z","shell.execute_reply.started":"2022-08-13T02:22:33.448453Z","shell.execute_reply":"2022-08-13T02:22:33.507590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 結果を作成する\nresult = pd.DataFrame({\"Id\":df_test[\"Id\"], \"SalePrice\":pred})\nresult.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:24:58.591761Z","iopub.execute_input":"2022-08-13T02:24:58.592479Z","iopub.status.idle":"2022-08-13T02:24:58.607659Z","shell.execute_reply.started":"2022-08-13T02:24:58.592426Z","shell.execute_reply":"2022-08-13T02:24:58.606458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CSVに出力する\nresult.to_csv(\"result.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:25:56.859075Z","iopub.execute_input":"2022-08-13T02:25:56.860234Z","iopub.status.idle":"2022-08-13T02:25:56.872044Z","shell.execute_reply.started":"2022-08-13T02:25:56.860191Z","shell.execute_reply":"2022-08-13T02:25:56.870743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"result.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T02:26:11.054928Z","iopub.execute_input":"2022-08-13T02:26:11.055338Z","iopub.status.idle":"2022-08-13T02:26:11.072756Z","shell.execute_reply.started":"2022-08-13T02:26:11.055307Z","shell.execute_reply":"2022-08-13T02:26:11.071544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}