{"cells":[{"metadata":{"_uuid":"1a115e1cf8571297359abb1efba79aaa7d159246"},"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":"dcd57ea5dee68f07d5658e438b83243367e05f56","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":"f5b60ff106cb3a8da4b7da1422b87d9ec8660cd4"},"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":"72ffbb36f22d7ef90c0279262c281e88b986fce2","trusted":true},"cell_type":"code","source":"# print the list of columns in the dataset to find the name of the prediction target\nhome_data.columns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5543a96c3d7249cafc5ed7b311a56963e56c73be","trusted":true},"cell_type":"code","source":"y = home_data.SalePrice\n\nstep_1.check()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"8b5dd273b471c348e576e10d9c28051f20238c9b","trusted":false},"cell_type":"code","source":"# The lines below will show you a hint or the solution.\n# step_1.hint() \n# step_1.solution()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5c1f15dff24f40f2594a39ea646913d585d651b9"},"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":"2c4a9d630b96d569de93bf190ab2609ba7a439d1","trusted":true,"scrolled":false},"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":"ef49e70cfdcc4332bf92069c4a85843b4e566f40","trusted":true},"cell_type":"code","source":"#step_2.hint()\n# step_2.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3c82ad33fbed9d3dfe0a2c3e99157ab19e14e605"},"cell_type":"markdown","source":"## Review Data\nBefore building a model, take a quick look at **X** to verify it looks sensible"},{"metadata":{"_uuid":"5b41f7497dbd051762980c09ea19bb97ac69ad95","trusted":true},"cell_type":"code","source":"# Review data\n# print description or statistics from X\nprint(X.describe())\n\n# print the top few lines\nprint(X.head())\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b82858f6e3d4e3ecfe2d29a06e8204d2b559517b"},"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":"363297c9d44b2813de338ae344e62abe655949f8","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=1)\n\n# Fit the model\niowa_model.fit(X,y)\n\nstep_3.check()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"829b66f18f30d131ea7f592c2103f64224300b0e","trusted":false},"cell_type":"code","source":"# step_3.hint()\n# step_3.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7447188f7abef913f1901f61a8af3b0d3f9db9ae"},"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":"46688139ee55fcec32103940235a2d16f01d8646","trusted":true},"cell_type":"code","source":"predictions = iowa_model.predict(X)\nprint(predictions)\nstep_4.check()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"bc897b8a73d516ac6eacc60c5e30d726043b3525","trusted":false},"cell_type":"code","source":"# step_4.hint()\n# step_4.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a470585bf0a733849dd9d544a64d89a4fb9c6013"},"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":"909da4afaf27667471c45b0238f890697c37bd78"},"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}