{"cells":[{"metadata":{"_uuid":"da226b2a28a666ba3ed7b86e11f78184486788f5"},"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":"7a5489872a82e5919ffbc0a0f9be4f6da2e97b83","trusted":true},"cell_type":"code","source":"# Code you have previously used to load data\nimport pandas as pd\n\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\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ad1a40c329e322c62f4dbd319a32dd5021bf4255"},"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":"2e37ce4935a684973a85c73060d4d5eef89ec544","trusted":true},"cell_type":"code","source":"# print the list of columns in the dataset to find the name of the prediction target\nprint (home_data.columns)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1c7b694445a0b47760f274bb8383b55a9541bb5a","trusted":true},"cell_type":"code","source":"y = home_data.SalePrice\n\nstep_1.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c22236a16f03ae79c21dcde1c0c3413b499ebec5","trusted":true},"cell_type":"code","source":"# The lines below will show you a hint or the solution.\nstep_1.hint() \nstep_1.solution()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"745083ab30f72c33a2a3b1264e72e9de9b69a105"},"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":"11d8fb3e4627208a9013b6afd544340c98c75573","trusted":true},"cell_type":"code","source":"# Create the list of features below\nfeature_names = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']\n\n# select data corresponding to features in feature_names\nX = home_data[feature_names]\n\nstep_2.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e12a96aa4cd09fad9f5ef7f6fba97017a644d95c","trusted":true},"cell_type":"code","source":"step_2.hint()\nstep_2.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c66a380eb8a5f521da48ebdd048e359138aa79d3"},"cell_type":"markdown","source":"## Review Data\nBefore building a model, take a quick look at **X** to verify it looks sensible"},{"metadata":{"_uuid":"7d635cfc70f1d7cac7ae680321206fe9c67d75b1","trusted":true},"cell_type":"code","source":"# Review data\n# print description or statistics from X\nprint(X.head())\n\n# print the top few lines\nprint(X.head(10))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"590184b0e987c2666688ea9f0e36de66486bb5cb"},"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":"df74c1b5d19ec7da15064138913d5557129ae74a","trusted":true},"cell_type":"code","source":"from sklearn.tree import DecisionTreeRegressor\n#specify the model. \n#For model reproducibility, set a numeric value for random_state when specifying the model\niowa_model = DecisionTreeRegressor(random_state = 0)\n\n# Fit the model\niowa_model.fit(X, y)\n\nstep_3.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b89261e61ac80d3832c85d347e09b6c108fc60e9","trusted":true},"cell_type":"code","source":"step_3.hint()\nstep_3.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6feadd3cdba2a4ffec9c8ddfba98eb56c18aa4e3"},"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":"5e34e179b7d3169ab8df47bb60e3159915fe1d12","trusted":true},"cell_type":"code","source":"predictions = iowa_model.predict(X)\nprint(predictions)\nstep_4.check()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"89c12522e554ab6d61d9758e2c42f53833d27ebb","trusted":false},"cell_type":"code","source":"step_4.hint()\nstep_4.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e8d2f313865b390d80cc26412e9a1ee753572f5a"},"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":"220bb0505f2b9c1e70ef012d90f047c047c2c8e7"},"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}