Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ****************************************************************
- # ****************************************************************
- # 1. Random forest in training dataset
- # ****************************************************************
- # ****************************************************************
- # Import helpful libraries
- import pandas as pd
- from sklearn.ensemble import RandomForestRegressor
- from sklearn.metrics import mean_absolute_error
- from sklearn.model_selection import train_test_split
- # Load the data, and separate the target
- iowa_file_path = '../input/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]
- X.head()
- # Split into validation and training data
- train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
- # Define a random forest model
- rf_model = RandomForestRegressor(random_state=1)
- rf_model.fit(train_X, train_y)
- rf_val_predictions = rf_model.predict(val_X)
- rf_val_mae = mean_absolute_error(rf_val_predictions, val_y)
- print("Validation MAE for Random Forest Model: {:,.0f}".format(rf_val_mae))
- # ****************************************************************
- # ****************************************************************
- # 2. Random forest in ALL THE dataset - testing will be applied to a different dataset-table-file
- # ****************************************************************
- # ****************************************************************
- # To improve accuracy, create a new Random Forest model which you will train on all training data
- rf_model_on_full_data = RandomForestRegressor(random_state=1)
- rf_model_on_full_data.fit(X, y)
- test_data_path = '../input/test.csv'
- test_data = pd.read_csv(test_data_path)
- test_X = test_data[features]
- # make predictions which we will submit
- test_preds = rf_model_on_full_data.predict(test_X)
- print(test_preds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement