{"cells":[{"metadata":{"_uuid":"c6b3980364d9dd8a267e524776c7df09252a5d72"},"cell_type":"markdown","source":"## Recap\nSo far, you have loaded your data and reviewed it with the following code. Run this cell to set up your coding environment where the previous step left off."},{"metadata":{"_uuid":"66bd379d8ff9925d437d8bbaf00c651fc48bc66a","trusted":true},"cell_type":"code","source":"# Code you have previously used to load data\nimport pandas as pd\nfrom sklearn.tree import DecisionTreeRegressor\n# Path of the file to read\niowa_file_path = '../input/home-data-for-ml-course/train.csv'\n\nhome_data = pd.read_csv(iowa_file_path)\n\n# Set up code checking\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.machine_learning.ex3 import *\n\nprint(\"Setup Complete\")\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4e5addd32992d1a901607ac3cde65e9a0b39c279"},"cell_type":"markdown","source":"# Exercises\n\n## Step 1: Specify Prediction Target\nSelect the target variable, which corresponds to the sales price. Save this to a new variable called `y`. You'll need to print a list of the columns to find the name of the column you need.\n"},{"metadata":{"_uuid":"0b81a015ef827cf11d1a6822c583f6b7484852d9","trusted":true},"cell_type":"code","source":"# print the list of columns in the dataset to find the name of the prediction print\nprint(home_data)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"21c6d687d55cae65d2542c468e1edc8173b598c5","trusted":true},"cell_type":"code","source":"#y = _\ny = home_data['SalePrice']\nstep_1.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"aa0ebcbec4ede0aa6d424186b40aac86cda90c56","trusted":true},"cell_type":"code","source":"# The lines below will show you a hint or the solution.\n# step_1.hint() \n# step_1.solution()\nprint(y)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8aa054ab4c9f4183f37d5f933b445dde3b21139"},"cell_type":"markdown","source":"## Step 2: Create X\nNow you will create a DataFrame called `X` holding the predictive features.\n\nSince you want only some columns from the original data, you'll first create a list with the names of the columns you want in `X`.\n\nYou'll use just the following columns in the list (you can copy and paste the whole list to save some typing, though you'll still need to add quotes):\n    * LotArea\n    * YearBuilt\n    * 1stFlrSF\n    * 2ndFlrSF\n    * FullBath\n    * BedroomAbvGr\n    * TotRmsAbvGrd\n\nAfter you've created that list of features, use it to create the DataFrame that you'll use to fit the model."},{"metadata":{"_uuid":"49550a3fe2e9b31238e7d212d8aed51df3da414e","trusted":true},"cell_type":"code","source":"# Create the list of features below\n# feature_names = ___\nfeature_names = ['LotArea',\n    'YearBuilt',\n    '1stFlrSF',\n    '2ndFlrSF',\n    'FullBath',\n    'BedroomAbvGr',\n    'TotRmsAbvGrd']\n# select data corresponding to features in feature_names\n#X = _\nX = home_data[feature_names]\nstep_2.check()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c681f73c35197a08d9763f4869292c643295a1cc","trusted":true},"cell_type":"code","source":"# \nstep_2.hint()\n# \nstep_2.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"debe1acdf053d0a8079fca710d1953e9f51ff9c0"},"cell_type":"markdown","source":"## Review Data\nBefore building a model, take a quick look at **X** to verify it looks sensible"},{"metadata":{"_uuid":"3dde6c472a8fd4f622cfda3a7cd1a775a365a811","trusted":true},"cell_type":"code","source":"# Review data\n# print description or statistics from X\n#print(_)\nprint(X)\n# print the top few lines\n#print(_)\nX.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7a77c001da7860e864ffddd6a1f8138d30bfb35d"},"cell_type":"markdown","source":"## Step 3: Specify and Fit Model\nCreate a `DecisionTreeRegressor` and save it iowa_model. Ensure you've done the relevant import from sklearn to run this command.\n\nThen fit the model you just created using the data in `X` and `y` that you saved above."},{"metadata":{"_uuid":"13b7bf3366b28e9d440a51b5ecbfa14c47515efd","trusted":true},"cell_type":"code","source":"# from _ import _\n#specify the model. \n#For model reproducibility, set a numeric value for random_state when specifying the model\n\niowa_model = DecisionTreeRegressor(random_state=1)\n# Fit the model\niowa_model.fit(X, y)\n\nstep_3.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6bbb7cac5ca1ec1c8d094df37d69fa90546197d5","trusted":true},"cell_type":"code","source":"# step_3.hint()\n# step_3.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"303510be5a680321e660cddd37e2aee0c3eec906"},"cell_type":"markdown","source":"## Step 4: Make Predictions\nMake predictions with the model's `predict` command using `X` as the data. Save the results to a variable called `predictions`."},{"metadata":{"_uuid":"e8baf1ce4a6b75d79ba8994972bba46986f8a38e","trusted":true},"cell_type":"code","source":"predictions = iowa_model.predict(X)\nprint(predictions)\nstep_4.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d9188017eb275aa08ae85a69fc6e9290205dde6a","trusted":true},"cell_type":"code","source":"# \nstep_4.hint()\n# \nstep_4.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8fc02fd79401bdf0886cd5121a0702a8c07c8808"},"cell_type":"markdown","source":"## Think About Your Results\n\nUse the `head` method to compare the top few predictions to the actual home values (in `y`) for those same homes. Anything surprising?\n\nYou'll understand why this happened if you keep going."},{"metadata":{"_uuid":"964cc5affdc01f7250ff8a54c984592453b3afc6"},"cell_type":"markdown","source":"\n## Keep Going\nYou've built a decision tree model.  It's natural to ask how accurate the model's predictions will be and how you can improve that. Learn how to do that with **[Model Validation](https://www.kaggle.com/dansbecker/model-validation)**.\n\n---\n**[Course Home Page](https://www.kaggle.com/learn/machine-learning)**\n\n\n"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}