@@ -58,3 +58,33 @@ rmse_test = MSE(y_test, y_pred)**(1/2)
5858
5959# Print the test set RMSE
6060print('Test set RMSE: {:.2f}'.format(rmse_test))
61+
62+
63+ # Stochastic Gradient Boosting in sklearn
64+ # Import models and utility functions
65+ from sklearn.ensemble import GradientBoostingRegressor
66+ from sklearn.model_selection import train_test_split
67+ from sklearn.metrics import mean_squared_error as MSE
68+
69+ # Set seed for reproducibility
70+ SEED = 1
71+
72+ # Split dataset into 70% train and 30% test
73+ X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3,random_state=SEED)
74+
75+ # Instantiate a stochastic GradientBoostingRegressor 'sgbt'
76+ sgbt = GradientBoostingRegressor(max_depth=1, subsample=0.8, max_features=0.2, n_estimators=300, random_state=SEED)
77+ # 0.8 refers to sample 80% of datafor training
78+ # 0.2 refers to each tree uses 20% of the available features to perform best split
79+
80+ # Fit 'sgbt' to the training set
81+ sgbt.fit(X_train, y_train)
82+
83+ # Predict the test set labels
84+ y_pred = sgbt.predict(X_test)
85+
86+ # Evaluate test set RMSE 'rmse_test'
87+ rmse_test = MSE(y_test, y_pred)**(1/2)
88+
89+ # Print 'rmse_test'
90+ print('Test set RMSE: {:.2f}'.format(rmse_test))
0 commit comments