We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
{"metadata":{"kernelspec":{"display_name":"Python
3","language":"python","name":"python3"},"language_info":{"codemirror_mode": {"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x- python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","vers ion":"3.6.5"},"kaggle":{"accelerator":"none","dataSources": [{"sourceId":10211,"databundleVersionId":111096,"sourceType":"competition"}, {"sourceId":15520,"sourceType":"datasetVersion","datasetId":11167}, {"sourceId":38454,"sourceType":"datasetVersion","datasetId":2709}],"isInternetEnabled":f alse,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_mino r":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/random-forests).**\n\n---\n","metadata":{}}, {"cell_type":"markdown","source":"## Recap\nHere's the code you've written so far.","metadata":{}},{"cell_type":"code","source":"# Code you have previously used to load data\nimport pandas as pd\nfrom sklearn.metrics import mean_absolute_error\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeRegressor\n\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# Create target object and call it y\ny = home_data.SalePrice\n# Create X\nfeatures = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']\nX = home_data[features]\n\n# Split into validation and training data\ntrain_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)\n\n# Specify Model\niowa_model = DecisionTreeRegressor(random_state=1)\n# Fit Model\niowa_model.fit(train_X, train_y)\n\ n# Make validation predictions and calculate mean absolute error\nval_predictions = iowa_model.predict(val_X)\nval_mae = mean_absolute_error(val_predictions, val_y)\ nprint(\"Validation MAE when not specifying max_leaf_nodes: {:,.0f}\".format(val_mae))\n\ n# Using best value for max_leaf_nodes\niowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)\niowa_model.fit(train_X, train_y)\nval_predictions = iowa_model.predict(val_X)\nval_mae = mean_absolute_error(val_predictions, val_y)\nprint(\"Validation MAE for best value of max_leaf_nodes: {:,.0f}\".format(val_mae))\n\n\n# Set up code checking\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.machine_learning.ex6 import *\nprint(\"\\nSetup complete\")","metadata": {"collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]}, {"cell_type":"markdown","source":"# Exercises\nData science isn't always this easy. But replacing the decision tree with a Random Forest is going to be an easy win.","metadata": {}},{"cell_type":"markdown","source":"## Step 1: Use a Random Forest","metadata":{}}, {"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_absolute_error\n\n# Define the model. Set random_state to 1\ nrf_model = RandomForestRegressor(random_state=1)\n\n# fit your model\ nrf_model.fit(train_X,train_y)\nrf_preds=rf_model.predict(val_X)\n\n# Calculate the mean absolute error of your Random Forest model on the validation data\nrf_val_mae = mean_absolute_error(val_y,rf_preds)\n\nprint(\"Validation MAE for Random Forest Model: {}\".format(rf_val_mae))\n\n# Check your answer\nstep_1.check()","metadata": {},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The lines below will show you a hint or the solution.\n# step_1.hint() \n# step_1.solution()\n","metadata": {},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So far, you have followed specific instructions at each step of your project. This helped learn key ideas and build your first model, but now you know enough to try things on your own. \n\nMachine Learning competitions are a great way to try your own ideas and learn more as you independently navigate a machine learning project. \n\n# Keep Going\n\nYou are ready for **[Machine Learning Competitions](https://www.kaggle.com/alexisbcook/machine-learning- competitions).**\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":{}}]}