v9.1.1
Menu

Random Feature Subspace

mediumFREEEnsemble MethodsMachine Learning 1

Implement random feature subspace selection used in Random Forests.

Given a dataset with mm features and a list of pre-selected feature indices (to make the output deterministic), return the data with only the selected columns.

In a real Random Forest, m\sqrt{m} features are randomly selected at each split. Here, the indices are given.

Return the subsampled feature matrix.

Example

Input
X = [[1, 2, 3, 4], [5, 6, 7, 8]]
selected_features = [0, 2]
Output
[[1, 3], [5, 7]]
Reasoning
  • X is a 2×42 \times 4 matrix: rows are samples and columns are features, so n=2n = 2 and m=4m = 4. Entry xijx_{ij} is the value of feature jj for sample ii (columns numbered from 0).
  • selected_features = [0, 2] lists the feature columns this split may use; columns 1 and 3 are dropped.
  • Row 0 is (1,2,3,4)(1, 2, 3, 4): feature 0 is 11 and feature 2 is 33, giving (1,3)(1, 3). Row 1 is (5,6,7,8)(5, 6, 7, 8): feature 0 is 55 and feature 2 is 77, giving (5,7)(5, 7).
  • The output [[1, 3], [5, 7]] is n×kn \times k with k=2k = 2, the number of selected indices. Rows keep their count and order; only the width shrinks from mm to kk.
  • Notice that output column cc is input column scs_c, so the order of selected_features decides the column order. Selecting columns 2 then 0 would give rows (3,1)(3, 1) and (7,5)(7, 5). In a real Random Forest, k≈m=4=2k \approx \sqrt{m} = \sqrt{4} = 2, the same size as here.

Constraints

  • X: 2D list (n_samples x m_features)
  • selected_features: list of column indices to keep
  • Return 2D list with only the selected columns, preserving row order

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.