liveBook · Manning

Random subspaces

As explained in Outlier Detection in Python

Random subspace selection is a technique for constructing feature subsets by choosing dimensions randomly.

Random subspace selection constructs feature subsets by choosing dimensions randomly. In outlier detection, multiple small subspaces can efficiently expose strong outliers when domain knowledge is limited, though important outliers may be missed. section 10.1.2

Construct a diverse set of comparable subspaces by:

  • Selecting the least-used feature as the starting point for each subspace. section 10.1.2
  • Adding features that minimize overlap with features already selected, while choosing randomly among ties. section 10.1.2
  • Keeping the feature count constant when simpler tuning and more comparable detector scores matter; varying counts can add diversity but complicate score combination. section 10.1.2
Listing 10.2 Algorithm to select random subspaces
import pandas as pd
import numpy as np
 
def get_random_subspaces(features_arr, num_base_detectors,
                         num_feats_per_detector):
    num_feats = len(features_arr)
    feat_sets_arr = []
    ft_used_counts = np.zeros(num_feats) 
    ft_pair_mtx = np.zeros((num_feats, num_feats))  
 
    for _ in range(num_base_detectors):        
        min_count = ft_used_counts.min()                          #1
        idxs = np.where(ft_used_counts == min_count)[0]    
 
        feat_set = [np.random.choice(idxs)]                       #2
 
        while len(feat_set) < num_feats_per_detector:             #3
            mtx_with_set = ft_pair_mtx[:, feat_set]
            sums = mtx_with_set.sum(axis=1)
            min_sum = sums.min()
            min_idxs = np.where(sums==min_sum)[0]
            new_feat = np.random.choice(min_idxs)
            feat_set.append(new_feat)
            feat_set = list(set(feat_set))
            
            for c in feat_set:                                    #4
                ft_pair_mtx[c][new_feat] += 1
                ft_pair_mtx[new_feat][c] += 1
            
        for c in feat_set:                                        #5
            ft_used_counts[c] += 1
 
        feat_sets_arr.append(feat_set)
 
    return feat_sets_arr
 
np.random.seed(0)
features_arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] 
num_base_detectors = 4
num_feats_per_detector = 5
 
feat_sets_arr = get_random_subspaces(features_arr, 
    num_base_detectors, num_feats_per_detector)
for feat_set in feat_sets_arr:    
    print([features_arr[x] for x in feat_set])

Use the selection algorithm to generate multiple feature subsets while tracking feature usage and pairwise overlap. section 10.1.2

How Outlier Detection in Python covers it

How deep it goes
Explains selection logic, diversity, subspace size, tuning, and score comparability, but stops short of detector-specific execution.
Examples
Demonstrates a Python subset-selection algorithm and shows generated feature sets with limited overlap.
What it assumes
Assumes a feature collection and base detectors, while motivating random selection when domain knowledge is unavailable.
What you can do after
Build multiple balanced, diverse feature subsets for downstream detector ensembles.
How it connects
Connects random subspaces with feature subspaces, outlier detection, base detectors, and k-nearest neighbors.

Also explained in

Compare how 3 books explain random subspaces →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.