Random forests use many decision trees to make predictions. Which of the following best explains how this helps reduce overfitting?
Think about how randomness and averaging help in machine learning models.
Random forests build many trees on random subsets of data and features. Averaging their predictions lowers variance and reduces overfitting compared to a single tree.
What is the output of the following Python code using scikit-learn's RandomForestClassifier?
from sklearn.ensemble import RandomForestClassifier import numpy as np X_train = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) y_train = np.array([0, 1, 0, 1]) model = RandomForestClassifier(random_state=42) model.fit(X_train, y_train) X_test = np.array([[2, 3]]) pred_probs = model.predict_proba(X_test) print(pred_probs)
Consider how the model predicts class probabilities based on training data and the test input.
The model predicts class 1 with full confidence for input [2, 3] because the trees vote mostly for class 1 based on training data.
What is the most likely effect of increasing the n_estimators parameter (number of trees) in a RandomForestClassifier?
Think about how more trees affect ensemble models.
More trees usually improve accuracy and stability by averaging more predictions but require more time to train.
You train a RandomForestClassifier on a dataset where 95% of samples belong to class 0 and 5% to class 1. Which metric is best to evaluate your model's performance?
Think about why accuracy can be misleading with imbalanced classes.
F1-score balances precision and recall, making it better for imbalanced datasets than accuracy alone.
What error will this code raise when training a RandomForestClassifier, and why?
from sklearn.ensemble import RandomForestClassifier X_train = [[1, 2], [3, 4], [5, 6]] y_train = [0, 1] model = RandomForestClassifier() model.fit(X_train, y_train)
Check if the number of samples in features and labels match.
The feature list has 3 samples but the label list has only 2, causing a ValueError about inconsistent sample sizes.
