{"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#from pandas_profiling import ProfileReport\nimport re\n\nfrom sklearn.linear_model import LogisticRegression, LinearRegression\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nsns.set() # setting seaborn default for plots\n\nimport shap\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-08T02:31:20.015909Z","iopub.execute_input":"2022-07-08T02:31:20.016284Z","iopub.status.idle":"2022-07-08T02:31:20.033130Z","shell.execute_reply.started":"2022-07-08T02:31:20.016255Z","shell.execute_reply":"2022-07-08T02:31:20.031523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = \"train.csv\"\ntest_csv = \"test.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.186165Z","iopub.execute_input":"2022-07-08T02:31:09.188049Z","iopub.status.idle":"2022-07-08T02:31:09.193928Z","shell.execute_reply.started":"2022-07-08T02:31:09.187982Z","shell.execute_reply":"2022-07-08T02:31:09.192628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the csv's to dataframes and show them\n\ntrain = pd.read_csv(os.path.join(dirname, train_csv))\nx_test = pd.read_csv(os.path.join(dirname, test_csv))\n\ndisplay(train.head())\ndisplay(x_test.head())","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.196511Z","iopub.execute_input":"2022-07-08T02:31:09.197386Z","iopub.status.idle":"2022-07-08T02:31:09.580924Z","shell.execute_reply.started":"2022-07-08T02:31:09.197336Z","shell.execute_reply":"2022-07-08T02:31:09.580077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.582485Z","iopub.execute_input":"2022-07-08T02:31:09.583091Z","iopub.status.idle":"2022-07-08T02:31:09.664093Z","shell.execute_reply.started":"2022-07-08T02:31:09.583054Z","shell.execute_reply":"2022-07-08T02:31:09.662786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.668411Z","iopub.execute_input":"2022-07-08T02:31:09.669233Z","iopub.status.idle":"2022-07-08T02:31:09.746297Z","shell.execute_reply.started":"2022-07-08T02:31:09.669192Z","shell.execute_reply":"2022-07-08T02:31:09.744628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.748873Z","iopub.execute_input":"2022-07-08T02:31:09.749814Z","iopub.status.idle":"2022-07-08T02:31:09.763978Z","shell.execute_reply.started":"2022-07-08T02:31:09.749758Z","shell.execute_reply":"2022-07-08T02:31:09.762430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby('Year').count()['TradePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.765885Z","iopub.execute_input":"2022-07-08T02:31:09.767013Z","iopub.status.idle":"2022-07-08T02:31:09.857274Z","shell.execute_reply.started":"2022-07-08T02:31:09.766976Z","shell.execute_reply":"2022-07-08T02:31:09.855868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Wrangling\n","metadata":{}},{"cell_type":"markdown","source":"## 間取りを綺麗にする","metadata":{}},{"cell_type":"code","source":"train.groupby('FloorPlan').count()['TradePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.858784Z","iopub.execute_input":"2022-07-08T02:31:09.860085Z","iopub.status.idle":"2022-07-08T02:31:09.945410Z","shell.execute_reply.started":"2022-07-08T02:31:09.860033Z","shell.execute_reply":"2022-07-08T02:31:09.944263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def madori(x):\n    tmp = (\n        str(x)\n        .replace('XXX','YYY')\n    )\n    # To-do\n    # You should add more here\n    # Or CST can create nice wrangling algorithm by Excel (vlookup function is an easy solution)\n    \n    return tmp\n\ntrain['間取り'] = train['FloorPlan'].apply(madori)\nx_test['間取り'] = x_test['FloorPlan'].apply(madori)\ntrain.groupby('間取り').count()['TradePrice']","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:09.947523Z","iopub.execute_input":"2022-07-08T02:31:09.948845Z","iopub.status.idle":"2022-07-08T02:31:10.083401Z","shell.execute_reply.started":"2022-07-08T02:31:09.948795Z","shell.execute_reply":"2022-07-08T02:31:10.081781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 文字列が入っているものを数字にする","metadata":{}},{"cell_type":"code","source":"def trans_char2figure(x):\n    return (\n        x.replace('1H-1H30','60')\n        .replace('1H30-2H', '90')\n        .replace('2H-', '120')\n        .replace('30-60minutes', '30')\n    )\n    \ntrain['TimeToNearestStation'] = train['TimeToNearestStation'].fillna(-1).astype(str).apply(trans_char2figure).astype(int)\nx_test['TimeToNearestStation'] = x_test['TimeToNearestStation'].fillna(-1).astype(str).apply(trans_char2figure).astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.085119Z","iopub.execute_input":"2022-07-08T02:31:10.085531Z","iopub.status.idle":"2022-07-08T02:31:10.163330Z","shell.execute_reply.started":"2022-07-08T02:31:10.085497Z","shell.execute_reply":"2022-07-08T02:31:10.162098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To-do\n# you can add any other logic here","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.165284Z","iopub.execute_input":"2022-07-08T02:31:10.165663Z","iopub.status.idle":"2022-07-08T02:31:10.171537Z","shell.execute_reply.started":"2022-07-08T02:31:10.165632Z","shell.execute_reply":"2022-07-08T02:31:10.170093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train/Test","metadata":{}},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.173777Z","iopub.execute_input":"2022-07-08T02:31:10.174264Z","iopub.status.idle":"2022-07-08T02:31:10.184932Z","shell.execute_reply.started":"2022-07-08T02:31:10.174224Z","shell.execute_reply":"2022-07-08T02:31:10.183734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['FloorPlan'], axis=1)\nx_test = x_test.drop(['FloorPlan'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.187635Z","iopub.execute_input":"2022-07-08T02:31:10.188083Z","iopub.status.idle":"2022-07-08T02:31:10.212217Z","shell.execute_reply.started":"2022-07-08T02:31:10.188020Z","shell.execute_reply":"2022-07-08T02:31:10.210814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_cols = ['Municipality', 'DistrictName', 'NearestStation',\n       '間取り', 'AreaIsGreaterFlag', 'FrontageIsGreaterFlag', \n       'PrewarBuilding', 'Structure', 'Use', 'Purpose', 'CityPlanning',\n       'CoverageRatio', 'FloorAreaRatio', 'Renovation']\nnumerical_cols = ['TimeToNearestStation', 'Area', 'BuildingYear', 'Year', 'Quarter']\ny_colname = 'TradePrice'","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.216667Z","iopub.execute_input":"2022-07-08T02:31:10.218148Z","iopub.status.idle":"2022-07-08T02:31:10.225032Z","shell.execute_reply.started":"2022-07-08T02:31:10.218079Z","shell.execute_reply":"2022-07-08T02:31:10.223861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train[categorical_cols + numerical_cols]\ny_train = train[y_colname]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.227024Z","iopub.execute_input":"2022-07-08T02:31:10.227409Z","iopub.status.idle":"2022-07-08T02:31:10.246491Z","shell.execute_reply.started":"2022-07-08T02:31:10.227374Z","shell.execute_reply":"2022-07-08T02:31:10.245455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Simple imputation","metadata":{}},{"cell_type":"code","source":"# Categorical variables\nx_train[categorical_cols] = x_train[categorical_cols].astype(str).fillna(\"NA\")\nx_test[categorical_cols] = x_test[categorical_cols].astype(str).fillna(\"NA\")\n\n# Numerical variables\nx_train[numerical_cols] = x_train[numerical_cols].fillna(-1).astype(int)\nx_test[numerical_cols] = x_test[numerical_cols].fillna(-1).astype(int)\n\n# To-do \n# You can change the imputation logic as you like.","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.248384Z","iopub.execute_input":"2022-07-08T02:31:10.249276Z","iopub.status.idle":"2022-07-08T02:31:10.792971Z","shell.execute_reply.started":"2022-07-08T02:31:10.249240Z","shell.execute_reply":"2022-07-08T02:31:10.791428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"#clf = LogisticRegression(random_state=0).fit(x_train[numerical_cols], y_train)\nclf = LinearRegression().fit(x_train[numerical_cols], y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:25.608128Z","iopub.execute_input":"2022-07-08T02:31:25.608599Z","iopub.status.idle":"2022-07-08T02:31:25.641655Z","shell.execute_reply.started":"2022-07-08T02:31:25.608540Z","shell.execute_reply":"2022-07-08T02:31:25.640660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test","metadata":{}},{"cell_type":"code","source":"#DO NOT EDIT THIS CELL!!!\n\n# Calculate the prediction result\npredict = clf.predict(x_test[numerical_cols])\n\n# Store the prediction result as the file.\nresult = pd.DataFrame(predict)\nresult = result.reset_index()\nresult.columns = ['ID', 'TradePrice']\nresult['ID'] = result['ID'] + 1\n\n# Files named submission.csv gets submitted automatically by pressing the Submit button on right column\nresult.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:28.407952Z","iopub.execute_input":"2022-07-08T02:31:28.408626Z","iopub.status.idle":"2022-07-08T02:31:28.449056Z","shell.execute_reply.started":"2022-07-08T02:31:28.408578Z","shell.execute_reply":"2022-07-08T02:31:28.447737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:30.228111Z","iopub.execute_input":"2022-07-08T02:31:30.228547Z","iopub.status.idle":"2022-07-08T02:31:30.240857Z","shell.execute_reply.started":"2022-07-08T02:31:30.228514Z","shell.execute_reply":"2022-07-08T02:31:30.239774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sns.histplot(result['TradePrice']);","metadata":{"execution":{"iopub.status.busy":"2022-07-08T02:31:10.995898Z","iopub.status.idle":"2022-07-08T02:31:10.996730Z","shell.execute_reply.started":"2022-07-08T02:31:10.996397Z","shell.execute_reply":"2022-07-08T02:31:10.996423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization (サンプル)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}