# variance in machine learning

from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor # Train a Random Forest model rf = RandomForestRegressor(n_estimators=100, random_state=42) rf.fit(X_train, y_train) # Evaluate the model using cross-validation cv_scores_rf = cross_val_score(rf, X_train, y_train, cv=5, scoring='neg_mean_squared_error') cv_scores_rf = -cv_scores_rf # Calculate the average MSE and its standard deviation avg_mse_rf = cv_scores_rf.mean() std_mse_rf = cv_scores_rf.std() print(f"Random Forest Cross-Validation MSE: {avg_mse_rf:.2f} +/- {std_mse_rf:.2f}") # Train a Gradient Boosting model gb = GradientBoostingRegressor(n_estimators=100, random_state=42) gb.fit(X_train, y_train) # Evaluate the model using cross-validation cv_scores_gb = cross_val_score(gb, X_train, y_train, cv=5, scoring='neg_mean_squared_error') cv_scores_gb = -cv_scores_gb # Calculate the average MSE and its standard deviation avg_mse_gb = cv_scores_gb.mean() std_mse_gb = cv_scores_gb.std() print(f"Gradient Boosting Cross-Validation MSE: {avg_mse_gb:.2f} +/- {std_mse_gb:.2f}")