Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ************************************************************************
- # ************************************************************************
- # 1. Decision Tree Regressor
- # ************************************************************************
- # ************************************************************************
- # Code you have previously used to load data
- import pandas as pd
- from sklearn.metrics import mean_absolute_error
- from sklearn.model_selection import train_test_split
- from sklearn.tree import DecisionTreeRegressor
- # Path of the file to read
- iowa_file_path = '../input/home-data-for-ml-course/train.csv'
- home_data = pd.read_csv(iowa_file_path)
- y = home_data.SalePrice
- features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
- X = home_data[features]
- # Split into validation and training data
- train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
- # Specify and Fit the Model
- iowa_model = DecisionTreeRegressor(random_state=1)
- iowa_model.fit(train_X, train_y)
- # Make validation predictions and calculate mean absolute error
- val_predictions = iowa_model.predict(val_X)
- val_mae = mean_absolute_error(val_predictions, val_y)
- print("Validation MAE when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
- # Using best value for max_leaf_nodes
- iowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)
- iowa_model.fit(train_X, train_y)
- val_predictions = iowa_model.predict(val_X)
- val_mae = mean_absolute_error(val_predictions, val_y)
- print("Validation MAE for best value of max_leaf_nodes: {:,.0f}".format(val_mae))
- # ************************************************************************
- # ************************************************************************
- # 2, Random Forest Regressor
- # ************************************************************************
- # ************************************************************************
- from sklearn.ensemble import RandomForestRegressor
- # Define the model. Set random_state to 1
- rf_model = RandomForestRegressor(random_state=1)
- rf_model.fit(train_X, train_y)
- val_preds = rf_model.predict(val_X)
- rf_val_mae = mean_absolute_error(val_y, val_preds)
- print("Validation MAE for Random Forest: {:,.0f}".format(rf_val_mae))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement