{"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":"markdown","source":"**This notebook is an exercise in the [Introduction to Machine Learning](https://www.kaggle.com/learn/intro-to-machine-learning) course.  You can reference the tutorial at [this link](https://www.kaggle.com/dansbecker/your-first-machine-learning-model).**\n\n---\n","metadata":{}},{"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":{}},{"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\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:16:49.789518Z","iopub.execute_input":"2022-08-08T10:16:49.790026Z","iopub.status.idle":"2022-08-08T10:16:51.295442Z","shell.execute_reply.started":"2022-08-08T10:16:49.789925Z","shell.execute_reply":"2022-08-08T10:16:51.293574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{}},{"cell_type":"code","source":"# print the list of columns in the dataset to find the name of the prediction target\nhome_data.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:17:05.390718Z","iopub.execute_input":"2022-08-08T10:17:05.391182Z","iopub.status.idle":"2022-08-08T10:17:05.401665Z","shell.execute_reply.started":"2022-08-08T10:17:05.391146Z","shell.execute_reply":"2022-08-08T10:17:05.400735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = home_data.SalePrice\n\n# Check your answer\nstep_1.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:17:15.325582Z","iopub.execute_input":"2022-08-08T10:17:15.326938Z","iopub.status.idle":"2022-08-08T10:17:15.343944Z","shell.execute_reply.started":"2022-08-08T10:17:15.326881Z","shell.execute_reply":"2022-08-08T10:17:15.342679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The lines below will show you a hint or the solution.\nstep_1.hint() \nstep_1.solution()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:17:31.880212Z","iopub.execute_input":"2022-08-08T10:17:31.880716Z","iopub.status.idle":"2022-08-08T10:17:31.892849Z","shell.execute_reply.started":"2022-08-08T10:17:31.880663Z","shell.execute_reply":"2022-08-08T10:17:31.891710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{}},{"cell_type":"code","source":"# Create the list of features below\nfeature_names = ['LotArea','YearBuilt','1stFlrSF','2ndFlrSF','FullBath','BedroomAbvGr',\n'TotRmsAbvGrd']\n\n# Select data corresponding to features in feature_names\nX = home_data[feature_names]\n\n# Check your answer\nstep_2.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:17:44.245482Z","iopub.execute_input":"2022-08-08T10:17:44.245920Z","iopub.status.idle":"2022-08-08T10:17:44.260836Z","shell.execute_reply.started":"2022-08-08T10:17:44.245886Z","shell.execute_reply":"2022-08-08T10:17:44.259877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"step_2.hint()\nstep_2.solution()\nX.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:17:57.785939Z","iopub.execute_input":"2022-08-08T10:17:57.786460Z","iopub.status.idle":"2022-08-08T10:17:57.842005Z","shell.execute_reply.started":"2022-08-08T10:17:57.786403Z","shell.execute_reply":"2022-08-08T10:17:57.840515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Review Data\nBefore building a model, take a quick look at **X** to verify it looks sensible","metadata":{}},{"cell_type":"code","source":"# Review data\n# print description or statistics from X\n#print(_)\nX.head()\n# print the top few lines\n#print(_)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:32:15.404129Z","iopub.execute_input":"2022-08-08T08:32:15.405028Z","iopub.status.idle":"2022-08-08T08:32:15.415141Z","shell.execute_reply.started":"2022-08-08T08:32:15.404991Z","shell.execute_reply":"2022-08-08T08:32:15.414361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.\nfrom sklearn.tree import DecisionTreeRegressor\niowa_model=DecisionTreeRegressor(random_state=1)\niowa.model.fit(X,y)","metadata":{}},{"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\n\n# Fit the model\n____\nfrom sklearn.tree import DecisionTreeRegressor\n\niowa_model=DecisionTreeRegressor(random_state=1)\n\niowa_model.fit(X,y)\n# Check your answer\nstep_3.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:37:21.164162Z","iopub.execute_input":"2022-08-08T08:37:21.164615Z","iopub.status.idle":"2022-08-08T08:37:21.183055Z","shell.execute_reply.started":"2022-08-08T08:37:21.164569Z","shell.execute_reply":"2022-08-08T08:37:21.182107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"step_3.hint()\nstep_3.solution()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:18:46.746014Z","iopub.execute_input":"2022-08-08T10:18:46.747132Z","iopub.status.idle":"2022-08-08T10:18:46.766474Z","shell.execute_reply.started":"2022-08-08T10:18:46.747068Z","shell.execute_reply":"2022-08-08T10:18:46.765519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{}},{"cell_type":"code","source":"X.head()\npredictions = iowa_model.predict(X)\nprint(predictions)\n\n# Check your answer\nstep_4.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:19:21.494663Z","iopub.execute_input":"2022-08-08T10:19:21.495089Z","iopub.status.idle":"2022-08-08T10:19:21.620756Z","shell.execute_reply.started":"2022-08-08T10:19:21.495055Z","shell.execute_reply":"2022-08-08T10:19:21.619126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"step_4.hint()\nstep_4.solution()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T10:19:04.019912Z","iopub.execute_input":"2022-08-08T10:19:04.020376Z","iopub.status.idle":"2022-08-08T10:19:04.032922Z","shell.execute_reply.started":"2022-08-08T10:19:04.020341Z","shell.execute_reply":"2022-08-08T10:19:04.031985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{}},{"cell_type":"code","source":"# You can write code in this cell\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It's natural to ask how accurate the model's predictions will be and how you can improve that. That will be you're next step.\n\n# Keep Going\n\nYou are ready for **[Model Validation](https://www.kaggle.com/dansbecker/model-validation).**\n","metadata":{}},{"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [course discussion forum](https://www.kaggle.com/learn/intro-to-machine-learning/discussion) to chat with other learners.*","metadata":{}}]}