{"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\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-27T22:39:04.683496Z","iopub.execute_input":"2022-07-27T22:39:04.684743Z","iopub.status.idle":"2022-07-27T22:39:04.697842Z","shell.execute_reply.started":"2022-07-27T22:39:04.684669Z","shell.execute_reply":"2022-07-27T22:39:04.696439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_full = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv', index_col='Id')\nX_full_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv', index_col='Id')","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2022-07-27T22:39:04.700975Z","iopub.execute_input":"2022-07-27T22:39:04.701900Z","iopub.status.idle":"2022-07-27T22:39:04.800886Z","shell.execute_reply.started":"2022-07-27T22:39:04.701829Z","shell.execute_reply":"2022-07-27T22:39:04.799911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_full.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:04.802550Z","iopub.execute_input":"2022-07-27T22:39:04.803739Z","iopub.status.idle":"2022-07-27T22:39:04.850461Z","shell.execute_reply.started":"2022-07-27T22:39:04.803686Z","shell.execute_reply":"2022-07-27T22:39:04.849447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_full.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:04.852714Z","iopub.execute_input":"2022-07-27T22:39:04.854613Z","iopub.status.idle":"2022-07-27T22:39:04.893693Z","shell.execute_reply.started":"2022-07-27T22:39:04.854566Z","shell.execute_reply":"2022-07-27T22:39:04.892325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_full = X_full.select_dtypes(exclude=['object'])\nX_full_test = X_full_test.select_dtypes(exclude=['object'])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:04.895582Z","iopub.execute_input":"2022-07-27T22:39:04.896006Z","iopub.status.idle":"2022-07-27T22:39:04.909705Z","shell.execute_reply.started":"2022-07-27T22:39:04.895968Z","shell.execute_reply":"2022-07-27T22:39:04.908483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_full.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:04.911505Z","iopub.execute_input":"2022-07-27T22:39:04.912946Z","iopub.status.idle":"2022-07-27T22:39:04.933114Z","shell.execute_reply.started":"2022-07-27T22:39:04.912861Z","shell.execute_reply":"2022-07-27T22:39:04.931849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_full_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:04.934740Z","iopub.execute_input":"2022-07-27T22:39:04.935798Z","iopub.status.idle":"2022-07-27T22:39:04.961493Z","shell.execute_reply.started":"2022-07-27T22:39:04.935744Z","shell.execute_reply":"2022-07-27T22:39:04.958642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X_full.drop(['SalePrice'], axis=1)\ny = X_full.SalePrice\nX_test = X_full_test","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:04.963657Z","iopub.execute_input":"2022-07-27T22:39:04.965651Z","iopub.status.idle":"2022-07-27T22:39:04.983533Z","shell.execute_reply.started":"2022-07-27T22:39:04.965579Z","shell.execute_reply":"2022-07-27T22:39:04.981484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# criando o modelo","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.impute import SimpleImputer","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:04.985753Z","iopub.execute_input":"2022-07-27T22:39:04.987321Z","iopub.status.idle":"2022-07-27T22:39:06.419505Z","shell.execute_reply.started":"2022-07-27T22:39:04.987263Z","shell.execute_reply":"2022-07-27T22:39:06.418436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Substituição de valores ausentes","metadata":{}},{"cell_type":"code","source":"imp_mean = SimpleImputer(strategy='mean')\nX= imp_mean.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.423500Z","iopub.execute_input":"2022-07-27T22:39:06.424301Z","iopub.status.idle":"2022-07-27T22:39:06.438083Z","shell.execute_reply.started":"2022-07-27T22:39:06.424246Z","shell.execute_reply":"2022-07-27T22:39:06.436942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X,y, train_size=0.8, random_state=3)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.440185Z","iopub.execute_input":"2022-07-27T22:39:06.441744Z","iopub.status.idle":"2022-07-27T22:39:06.452080Z","shell.execute_reply.started":"2022-07-27T22:39:06.441683Z","shell.execute_reply":"2022-07-27T22:39:06.450642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = imp_mean.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.454498Z","iopub.execute_input":"2022-07-27T22:39:06.455028Z","iopub.status.idle":"2022-07-27T22:39:06.471623Z","shell.execute_reply.started":"2022-07-27T22:39:06.454977Z","shell.execute_reply":"2022-07-27T22:39:06.469833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Treinando a Árvore de Decisão","metadata":{}},{"cell_type":"code","source":"house_prices_model = DecisionTreeRegressor(random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.474848Z","iopub.execute_input":"2022-07-27T22:39:06.475818Z","iopub.status.idle":"2022-07-27T22:39:06.482811Z","shell.execute_reply.started":"2022-07-27T22:39:06.475736Z","shell.execute_reply":"2022-07-27T22:39:06.481511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"house_prices_model.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.484485Z","iopub.execute_input":"2022-07-27T22:39:06.485999Z","iopub.status.idle":"2022-07-27T22:39:06.527366Z","shell.execute_reply.started":"2022-07-27T22:39:06.485945Z","shell.execute_reply":"2022-07-27T22:39:06.526072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_predicitions = house_prices_model.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.528858Z","iopub.execute_input":"2022-07-27T22:39:06.529268Z","iopub.status.idle":"2022-07-27T22:39:06.537559Z","shell.execute_reply.started":"2022-07-27T22:39:06.529235Z","shell.execute_reply":"2022-07-27T22:39:06.536176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Avaliando o modelo ","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.539052Z","iopub.execute_input":"2022-07-27T22:39:06.540371Z","iopub.status.idle":"2022-07-27T22:39:06.551302Z","shell.execute_reply.started":"2022-07-27T22:39:06.540328Z","shell.execute_reply":"2022-07-27T22:39:06.549745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_squared_error(y_val, val_predicitions, squared=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.553314Z","iopub.execute_input":"2022-07-27T22:39:06.554111Z","iopub.status.idle":"2022-07-27T22:39:06.565007Z","shell.execute_reply.started":"2022-07-27T22:39:06.554055Z","shell.execute_reply":"2022-07-27T22:39:06.563790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_full.describe().SalePrice","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.566631Z","iopub.execute_input":"2022-07-27T22:39:06.567621Z","iopub.status.idle":"2022-07-27T22:39:06.666558Z","shell.execute_reply.started":"2022-07-27T22:39:06.567570Z","shell.execute_reply":"2022-07-27T22:39:06.665719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predicitions = house_prices_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.668116Z","iopub.execute_input":"2022-07-27T22:39:06.668690Z","iopub.status.idle":"2022-07-27T22:39:06.674132Z","shell.execute_reply.started":"2022-07-27T22:39:06.668653Z","shell.execute_reply":"2022-07-27T22:39:06.673156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predicitions","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.675678Z","iopub.execute_input":"2022-07-27T22:39:06.676368Z","iopub.status.idle":"2022-07-27T22:39:06.688411Z","shell.execute_reply.started":"2022-07-27T22:39:06.676327Z","shell.execute_reply":"2022-07-27T22:39:06.686660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Criando arquivo de submissão","metadata":{}},{"cell_type":"code","source":"output = pd.DataFrame({'Id' : X_full_test.index,\n                       'SalePrice' : test_predicitions})\noutput.set_index('Id', inplace=True)\noutput.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.690381Z","iopub.execute_input":"2022-07-27T22:39:06.691489Z","iopub.status.idle":"2022-07-27T22:39:06.708313Z","shell.execute_reply.started":"2022-07-27T22:39:06.691450Z","shell.execute_reply":"2022-07-27T22:39:06.706968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T22:39:06.710466Z","iopub.execute_input":"2022-07-27T22:39:06.710978Z","iopub.status.idle":"2022-07-27T22:39:06.731429Z","shell.execute_reply.started":"2022-07-27T22:39:06.710939Z","shell.execute_reply":"2022-07-27T22:39:06.730146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}