InstanceSpace Python v0.4.1

instancespace.stages.prelim

Performing preliminary data processing.

The main focus is on the prelim function, which prepares the input data for further analysis and modeling.

The prelim function takes feature and performance data matrices along with a set of processing options, and performs various preprocessing tasks such as normalization, outlier detection and removal, and binary performance classification. These tasks are guided by the options specified in the InstanceSpaceOptions object.

class BinaryPerformance(typing.NamedTuple):

Ground-truth binary-performance fields derived from raw algorithm performance.

Attributes
  • y (NDArray[np.double]): Relative (or absolute) performance matrix - y_raw transformed by the same max_perf/abs_perf branching used to derive y_bin.
  • y_bin (NDArray[np.bool_]): Binary matrix indicating instances with good algorithm performance.
  • y_best (NDArray[np.double]): Best observed performance value for each instance across all algorithms (zero values substituted with eps, matching MATLAB).
  • p (NDArray[np.int_]): 1-based index of the best-performing algorithm per instance, ties broken with MATLAB-compatible seeded random selection.
  • num_good_algos (NDArray[np.double]): Number of algorithms with good performance, per instance.
  • beta (NDArray[np.bool_]): Whether each instance clears beta_threshold's fraction of good algorithms.
BinaryPerformance( y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y_bin: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]], y_best: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], p: numpy.ndarray[typing.Any, numpy.dtype[numpy.int64]], num_good_algos: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], beta: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]])

Create new instance of BinaryPerformance(y, y_bin, y_best, p, num_good_algos, beta)

y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 0

y_bin: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]]

Alias for field number 1

y_best: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 2

p: numpy.ndarray[typing.Any, numpy.dtype[numpy.int64]]

Alias for field number 3

num_good_algos: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 4

beta: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]]

Alias for field number 5

def compute_binary_performance( y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], perf_opts: instancespace.data.options.PerformanceOptions, general_opts: instancespace.data.options.GeneralOptions, log_prefix: str = 'PRELIM') -> BinaryPerformance:

Compute the binary measure of algorithm performance from raw Y.

Ports MATLAB PRELIM.m's binary-performance section: an algorithm is "good" for an instance if its performance is within epsilon of the best (relative) or better than epsilon outright (absolute), per perf_opts.max_perf/abs_perf. Ties for the best algorithm use MATLAB's seeded twister stream and randi selection rule.

Shared by PrelimStage._prelim() (training, ground truth for the training set) and InstanceSpace.explore()'s evaluation path (ground truth for a test set using the trained PerformanceOptions) - one implementation, not a second one written to match it by hand.

def apply_bound_clip( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], hi_bound: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], lo_bound: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]) -> numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]:

Clip each feature column of x to [lo_bound, hi_bound].

Equivalent to (and verified bit-for-bit identical, including NaN handling, against) the mask-multiply arithmetic this replaces - np.clip simply expresses the same operation without hand-writing it twice. Shared by PrelimStage._bound() (training, clipping to bounds just fit from this same data) and InstanceSpace._explore_prelim() (test data, clipping to the trained model's stored bounds) - one implementation, not a second one written to match it by hand.

def apply_boxcox_zscore( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], lambda_: float, mu: float, sigma: float) -> numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]:

Apply a Box-Cox transform at a known lambda, then z-score at known mu/sigma.

Verified bit-for-bit identical to scipy.stats.zscore(stats.boxcox(x, lambda_), ddof=1) when mu/sigma are themselves x's own box-cox'd mean/std (ddof=1) - i.e. this is a drop-in replacement for recomputing the z-score from scratch, not a new formula. Shared by PrelimStage._normalise() (training, applying the lambda/mu/sigma it just fit from this same column) and InstanceSpace._explore_prelim() (test data, applying the trained model's stored lambda/mu/sigma) - one implementation, not a second one written to match it by hand. x must already be positive (the min-shift-by-1 step happens before this call).

class PrelimInput(typing.NamedTuple):

Inputs for the Prelim stage.

Attributes
  • x (NDArray[np.double]): Feature matrix where each row represents an instance, and each column represents a feature.
  • y (NDArray[np.double]): Performance matrix of algorithms, with rows as instances and columns as algorithms.
  • x_raw (NDArray[np.double]): Unprocessed feature matrix, containing raw values of each instance-feature pair.
  • y_raw (NDArray[np.double]): Unprocessed performance matrix, containing raw performance values for each instance-algorithm pair.
  • s (pd.Series | None): Optional series for additional selection during processing, if available.
  • inst_labels (pd.Series): Labels for each instance in the dataset, used for identification.
  • prelim_options (PrelimOptions): Configuration options specific to the Prelim stage.
  • selvars_options (SelvarsOptions): Options for selecting variables within the Prelim stage, affecting criteria and file indices.
  • general_options (GeneralOptions): General options (e.g. the RNG seed), not specific to any one stage.
  • algo_labels (list[str]): Algorithm names aligned with performance columns; propagated after pruning.
PrelimInput( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], x_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], s: pandas.core.series.Series | None, inst_labels: pandas.core.series.Series, prelim_options: instancespace.data.options.PrelimOptions, selvars_options: instancespace.data.options.SelvarsOptions, general_options: instancespace.data.options.GeneralOptions, algo_labels: list[str])

Create new instance of PrelimInput(x, y, x_raw, y_raw, s, inst_labels, prelim_options, selvars_options, general_options, algo_labels)

x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 0

y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 1

x_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 2

y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 3

s: pandas.core.series.Series | None

Alias for field number 4

inst_labels: pandas.core.series.Series

Alias for field number 5

Alias for field number 6

Alias for field number 7

Alias for field number 8

algo_labels: list[str]

Alias for field number 9

class PrelimPredictInput(typing.NamedTuple):

Inputs for applying fitted PRELIM feature transformations.

PrelimPredictInput( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], auto_preproc: bool, bound_enabled: bool, norm_enabled: bool)

Create new instance of PrelimPredictInput(x, auto_preproc, bound_enabled, norm_enabled)

x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 0

auto_preproc: bool

Alias for field number 1

bound_enabled: bool

Alias for field number 2

norm_enabled: bool

Alias for field number 3

class PrelimOutput(typing.NamedTuple):

Outputs for the Prelim stage.

Attributes
  • med_val (NDArray[np.double]): Median values of each feature across instances in the processed data.
  • iq_range (NDArray[np.double]): Interquartile range of each feature, representing the spread of data between the 25th and 75th percentiles.
  • hi_bound (NDArray[np.double]): Upper bound values for each feature based on specified statistical measures.
  • lo_bound (NDArray[np.double]): Lower bound values for each feature based on specified statistical measures.
  • min_x (NDArray[np.double]): Minimum values for each feature in the raw feature matrix.
  • lambda_x (NDArray[np.double]): Box-Cox transformation parameters for each feature, if applicable.
  • mu_x (NDArray[np.double]): Mean values of each feature across instances in the processed data.
  • sigma_x (NDArray[np.double]): Standard deviation of each feature across instances in the processed data.
  • min_y (float): Minimum value observed in the raw performance data.
  • lambda_y (NDArray[np.double]): Box-Cox transformation parameters for the performance matrix, if applicable.
  • sigma_y (NDArray[np.double]): Standard deviation of performance values across instances.
  • mu_y (NDArray[np.double]): Mean values of performance across instances for each algorithm.
  • x (NDArray[np.double]): Processed feature matrix, where each row represents an instance and each column represents a feature.
  • y (NDArray[np.double]): Processed performance matrix, containing performance values for each instance-algorithm pair.
  • x_raw (NDArray[np.double]): Original, unprocessed feature matrix containing raw values of each instance-feature pair.
  • y_raw (NDArray[np.double]): Original, unprocessed performance matrix containing raw values for each instance-algorithm pair.
  • y_bin (NDArray[np.bool_]): Binary matrix indicating instances with good algorithm performance (True if performance is good).
  • y_best (NDArray[np.double]): Best observed performance value for each instance across all algorithms.
  • p (NDArray[np.int_]): Index of the best-performing algorithm for each instance (1-based, matching MATLAB).
  • num_good_algos (NDArray[np.double]): Number of good algorithms per instance.
  • beta (NDArray[np.bool_]): Per-instance beta-easy indicator: good count strictly exceeds the configured fraction of retained algorithms.
  • instlabels (pd.Series | None): Labels for each instance in the dataset, if provided.
  • data_dense (DataDense | None): Dense data representation, if available, containing compressed or alternative feature representations.
  • s (pd.Series | None): Optional source labels aligned with the retained instance rows.
PrelimOutput( med_val: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], iq_range: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], hi_bound: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], lo_bound: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], min_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], lambda_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], mu_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], sigma_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], min_y: float, lambda_y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], sigma_y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], mu_y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], x_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y_bin: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]], y_best: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], p: numpy.ndarray[typing.Any, numpy.dtype[numpy.int64]], num_good_algos: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], beta: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]], instlabels: pandas.core.series.Series | None, data_dense: instancespace.data.model.DataDense | None, s: pandas.core.series.Series | None, algo_labels: list[str])

Create new instance of PrelimOutput(med_val, iq_range, hi_bound, lo_bound, min_x, lambda_x, mu_x, sigma_x, min_y, lambda_y, sigma_y, mu_y, x, y, x_raw, y_raw, y_bin, y_best, p, num_good_algos, beta, instlabels, data_dense, s, algo_labels)

med_val: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 0

iq_range: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 1

hi_bound: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 2

lo_bound: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 3

min_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 4

lambda_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 5

mu_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 6

sigma_x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 7

min_y: float

Alias for field number 8

lambda_y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 9

sigma_y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 10

mu_y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 11

x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 12

y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 13

x_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 14

y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 15

y_bin: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]]

Alias for field number 16

y_best: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 17

p: numpy.ndarray[typing.Any, numpy.dtype[numpy.int64]]

Alias for field number 18

num_good_algos: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]

Alias for field number 19

beta: numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]]

Alias for field number 20

instlabels: pandas.core.series.Series | None

Alias for field number 21

Alias for field number 22

s: pandas.core.series.Series | None

Alias for field number 23

algo_labels: list[str]

Alias for field number 24

See file docstring.

PrelimStage( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], x_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], s: pandas.core.series.Series | None, inst_labels: pandas.core.series.Series, prelim_opts: instancespace.data.options.PrelimOptions, selvars_opts: instancespace.data.options.SelvarsOptions, general_opts: instancespace.data.options.GeneralOptions)

See file docstring.

x
y
prelim_opts
selvars_opts
general_opts
x_raw
y_raw
s
inst_labels
@staticmethod
def predict( inputs: PrelimPredictInput, fitted: instancespace.data.model.PrelimOut) -> numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]:

Apply fitted bounds and normalisation without re-fitting PRELIM.

@staticmethod
def prelim( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], x_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], s: pandas.core.series.Series | None, inst_labels: pandas.core.series.Series, prelim_opts: instancespace.data.options.PrelimOptions, selvars_opts: instancespace.data.options.SelvarsOptions, general_opts: instancespace.data.options.GeneralOptions) -> tuple[numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.int64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.bool_]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], float, numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]]:

Perform preliminary processing on the input data 'x' and 'y'.

Parameters
  • x (NDArray[np.double]): The feature matrix (instances x features) to process.
  • y (NDArray[np.double]): The performance matrix (instances x algorithms) to process.
  • x_raw (NDArray[np.double]): The raw feature matrix.
  • y_raw (NDArray[np.double]): The raw performance matrix.
  • s (pd.Series | None): The instance source labels, or None.
  • inst_labels (pd.Series): The instance labels.
  • prelim_opts (PrelimOptions): The options for the preliminary processing.
  • selvars_opts (SelvarsOptions): The options for the instance and feature selection.
  • general_opts (GeneralOptions): The general options for all stages.
Returns
  • tuple: The first items are the processed data: x, y, y_bin, y_best, p, num_good_algos, and beta. The remaining items are the fitted bound and normalization parameters: med_val, iq_range, hi_bound, lo_bound, min_x, lambda_x, mu_x, sigma_x, min_y, lambda_y, sigma_y, and mu_y.