{"cells":[{"metadata":{"_uuid":"abbc81a26c0fd045a519c426dbbce57396094e43"},"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":"cb0028ee66c129ec6e466ee6c855e703a08393c3","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":"757419143650ce2dec702f386528c1760323d532"},"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":"323014787706170d83d1e68f0e45e7b49e16853e","trusted":true},"cell_type":"code","source":"# print the list of columns in the dataset to find the name of the prediction target\ny = home_data.SalePrice","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c2ca21cb2dfe06382b24f23ad4359ef8a8612e6a","trusted":true},"cell_type":"code","source":"#y = _\n\nstep_1.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"49bee6302383ce3e603bb17e208c1984e4a9b8ad","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()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"090c8d2e3ded0ad79e63ecce1e74f66894278675"},"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":"4a60d2545de97751c6ee1a95686b942849be6fe7","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":"c6178d2bccba6a780b6801f19338f21ae61499d4","trusted":true},"cell_type":"code","source":"# step_2.hint()\n# step_2.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b1be928334ff8ae891a9f50224d21656621fd85a"},"cell_type":"markdown","source":"## Review Data\nBefore building a model, take a quick look at **X** to verify it looks sensible"},{"metadata":{"_uuid":"d718b26bdf7680d045daa6d04973b0fcb0fea1ec","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":"cc1ad4151b617174c958c1a3c53a10ebe98a5b7c"},"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":"841ba69255f3693e7f2bc2b3d8b74d92e370e6d5","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":{"_uuid":"80d85bf37193e1805ab9de575227e718fa292f25","trusted":true},"cell_type":"code","source":"# step_3.hint()\n# step_3.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7a359988be56add52223f1a5e7b75e5d7e3a4df6"},"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":"ea8787d2e4a0d04d6696633bef74a0c1933f1658","trusted":true},"cell_type":"code","source":"predictions = iowa_model.predict(X)\nprint(predictions)\nstep_4.check()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"34f4c55809359e9c0adc947ddf57926418c0177e","trusted":true},"cell_type":"code","source":"# step_4.hint()\n# step_4.solution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"125e9d0a8a3d917a1b6095871087fa8b095a81d7"},"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":"90af34c5382b1fe7d505492b33f39de3f054c594"},"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":{"trusted":true,"_uuid":"a0fd30a942923e5e7f4cfe285d4866cfc36b31f7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}