InstanceSpace Python v0.4.1

instancespace.stages.preprocessing

Preprocessing Stage Module.

This module defines the classes and methods for the preprocessing stage of a machine learning pipeline. It filters data rows based on provided options, and removes instances or features with too many missing values.

The preprocessing stage outputs a cleaned and filtered dataset that can be used for further modeling or analysis.

Classes

PreprocessingInput : NamedTuple Defines the input data structure for the preprocessing stage. PreprocessingOutput : NamedTuple Defines the output data structure for the preprocessing stage. PreprocessingStage : Stage Class that executes the preprocessing stage.

MATRIX_DIMENSIONS = 2
MIN_FEATURES = 3
def validate_viable_dimensions( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], *, require_algorithms: bool = True, context: str = 'Input data') -> None:

Validate matrix shape and the minimum viable instance-space dimensions.

class PreprocessingInput(typing.NamedTuple):

Inputs for the Preprocessing stage.

Attributes
  • feature_names (list[str]): List of feature names in the dataset.
  • algorithm_names (list[str]): List of algorithm names in the dataset.
  • instance_labels (pd.Series): Labels for each instance (row) in the dataset.
  • instance_sources (pd.Series | None): Sources for each instance, optional.
  • features (NDArray[np.double]): Feature matrix (instances x features) as a 2D numpy array.
  • algorithms (NDArray[np.double]): Algorithm matrix (instances y algorithms) as a 2D numpy array.
  • selvars_options (SelvarsOptions): Options for selecting variables (features and algorithms).
  • prelim_options (PrelimOptions): Composed preliminary-processing options. Legacy direct construction uses the same defaults as the aggregate configuration.
PreprocessingInput( feature_names: list[str], algorithm_names: list[str], instance_labels: pandas.core.series.Series, instance_sources: pandas.core.series.Series | None, features: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], algorithms: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], selvars_options: instancespace.data.options.SelvarsOptions, prelim_options: instancespace.data.options.PrelimOptions = PrelimOptions(max_perf=False, abs_perf=True, epsilon=0.2, beta_threshold=0.55, bound=True, norm=True, iqr_multiplier=5.0, preproc=True, nan_threshold=0.2))

Create new instance of PreprocessingInput(feature_names, algorithm_names, instance_labels, instance_sources, features, algorithms, selvars_options, prelim_options)

feature_names: list[str]

Alias for field number 0

algorithm_names: list[str]

Alias for field number 1

instance_labels: pandas.core.series.Series

Alias for field number 2

instance_sources: pandas.core.series.Series | None

Alias for field number 3

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

Alias for field number 4

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

Alias for field number 5

Alias for field number 6

Alias for field number 7

class PreprocessingOutput(typing.NamedTuple):

Outputs for the Preprocessing stage.

Attributes
  • inst_labels (pd.Series): Series containing labels for each instance after preprocessing.
  • feat_labels (list[str]): List of labels corresponding to the selected features.
  • algo_labels (list[str]): List of labels corresponding to the selected algorithms.
  • x (NDArray[np.double]): Preprocessed feature matrix (instances x selected features).
  • y (NDArray[np.double]): Preprocessed algorithm matrix (instances y selected algorithms).
  • s (pd.Series | None): Optional series containing the source of instances after preprocessing.
  • x_raw (NDArray[np.double]): Original feature matrix before any modifications.
  • y_raw (NDArray[np.double]): Original algorithm matrix before any modifications.
  • preprocessing_features (tuple[str, ...] | None): Retained feature names in the order consumed by PRELIM, before SIFTED.
PreprocessingOutput( inst_labels: pandas.core.series.Series, feat_labels: list[str], algo_labels: list[str], x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], s: pandas.core.series.Series | None, x_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y_raw: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], preprocessing_features: tuple[str, ...] | None = None)

Create new instance of PreprocessingOutput(inst_labels, feat_labels, algo_labels, x, y, s, x_raw, y_raw, preprocessing_features)

inst_labels: pandas.core.series.Series

Alias for field number 0

feat_labels: list[str]

Alias for field number 1

algo_labels: list[str]

Alias for field number 2

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

Alias for field number 3

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

Alias for field number 4

s: pandas.core.series.Series | None

Alias for field number 5

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

Alias for field number 6

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

Alias for field number 7

preprocessing_features: tuple[str, ...] | None

Alias for field number 8

Class for handling the preprocessing stage of the pipeline.

This stage includes tasks such as feature selection, algorithm selection, and removing instances or features with too many missing values.

Methods

select_features_and_algorithms(x, y, feat_labels, algo_labels, selvars) Selects features and algorithms from the dataset based on user options. remove_instances_with_many_missing_values(x, y, s, feat_labels, inst_labels) Removes instances (rows) and features (columns) with excessive missing values.

PreprocessingStage( feature_names: list[str], algorithm_names: list[str], instance_labels: pandas.core.series.Series, instance_sources: pandas.core.series.Series | None, features: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], algorithms: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], selvars: instancespace.data.options.SelvarsOptions)

Initialize the Preprocessing stage.

feature_names
algorithm_names
instance_labels
instance_sources
features
algorithms
selvars
@staticmethod
def select_features_and_algorithms( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], feat_labels: list[str], algo_labels: list[str], selvars: instancespace.data.options.SelvarsOptions) -> tuple[numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], list[str], list[str]]:

Select features and algorithms from the dataset.

Based on the user's configuration, this method filters the features and algorithms that should be used in subsequent stages.

Parameters
  • x (NDArray[np.double]): 2D numpy array representing the feature matrix (instances x features).
  • y (NDArray[np.double]): 2D numpy array representing the algorithm matrix (instances y algorithms).
  • feat_labels (list[str]): List of labels corresponding to the features in 'x'.
  • algo_labels (list[str]): List of labels corresponding to the algorithms in 'y'.
  • selvars (SelvarsOptions): An instance of SelvarsOptions that contains settings of the prefered algorithms and instances.
Returns
  • tuple[NDArray[np.double], NDArray[np.double], list[str], list[str]]: A tuple containing:
    • Modified feature matrix after feature selection and instance removal.
    • Modified algorithm matrix after algorithm selection and instance removal.
    • List of selected feature labels.
    • List of selected algorithm labels.
@staticmethod
def remove_instances_with_many_missing_values( x: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], y: numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], s: pandas.core.series.Series | None, feat_labels: list[str], inst_labels: pandas.core.series.Series, nan_threshold: float = 0.2) -> tuple[numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]], pandas.core.series.Series, list[str], pandas.core.series.Series | None]:

Remove instances and features with excessive missing values.

Instances (rows) with too many missing values are removed. Additionally, features (columns) that exceed a missing value threshold are also removed. Washing criterion: 1. For any row, if that row in both X and Y are NaN, remove 2. For X columns, if that column's 20% grids are filled with NaN, remove

Parameters
  • x (NDArray[np.double]): 2D numpy array representing the feature matrix (instances x features).
  • y (NDArray[np.double]): 2D numpy array representing the algorithm matrix (instances y algorithms).
  • s (pd.Series | None): Optional series containing the source of instances.
  • feat_labels (list[str]): List of labels corresponding to the features in 'x'.
  • inst_labels (pd.Series): Series containing labels for each instance.
  • nan_threshold (float): Fraction of missing values at which a feature is removed.
Returns
  • tuple[NDArray[np.double], NDArray[np.double],
  • pd.Series, list[str], pd.Series | None]: A tuple containing the modified feature matrix 'x', the modified algorithm matrix 'y',updated instance labels, list of feature labels that remain after removal, and optionally modified series 's' if provided.