Advertisement
makispaiktis

Kaggle - Exercise 5 - RandomForestRegressor

Jun 20th, 2023
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. # ************************************************************************
  2. # ************************************************************************
  3. # 1. Decision Tree Regressor
  4. # ************************************************************************
  5. # ************************************************************************
  6.  
  7. # Code you have previously used to load data
  8. import pandas as pd
  9. from sklearn.metrics import mean_absolute_error
  10. from sklearn.model_selection import train_test_split
  11. from sklearn.tree import DecisionTreeRegressor
  12.  
  13.  
  14. # Path of the file to read
  15. iowa_file_path = '../input/home-data-for-ml-course/train.csv'
  16. home_data = pd.read_csv(iowa_file_path)
  17.  
  18. y = home_data.SalePrice
  19. features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
  20. X = home_data[features]
  21.  
  22. # Split into validation and training data
  23. train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
  24.  
  25. # Specify and Fit the Model
  26. iowa_model = DecisionTreeRegressor(random_state=1)
  27. iowa_model.fit(train_X, train_y)
  28.  
  29. # Make validation predictions and calculate mean absolute error
  30. val_predictions = iowa_model.predict(val_X)
  31. val_mae = mean_absolute_error(val_predictions, val_y)
  32. print("Validation MAE when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
  33.  
  34. # Using best value for max_leaf_nodes
  35. iowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)
  36. iowa_model.fit(train_X, train_y)
  37. val_predictions = iowa_model.predict(val_X)
  38. val_mae = mean_absolute_error(val_predictions, val_y)
  39. print("Validation MAE for best value of max_leaf_nodes: {:,.0f}".format(val_mae))
  40.  
  41.  
  42.  
  43. # ************************************************************************
  44. # ************************************************************************
  45. # 2, Random Forest Regressor
  46. # ************************************************************************
  47. # ************************************************************************
  48.  
  49. from sklearn.ensemble import RandomForestRegressor
  50.  
  51. # Define the model. Set random_state to 1
  52. rf_model = RandomForestRegressor(random_state=1)
  53. rf_model.fit(train_X, train_y)
  54. val_preds = rf_model.predict(val_X)
  55. rf_val_mae = mean_absolute_error(val_y, val_preds)
  56.  
  57. print("Validation MAE for Random Forest: {:,.0f}".format(rf_val_mae))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement