Skip to content

Features

features


Assembly: Mathy Filename: features.py Author: Terry D. Eppler Created: 05-31-2022

Last Modified By: Terry D. Eppler Last Modified On: 05-01-2025


 features.py
 Copyright ©  2022  Terry Eppler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov

Provides feature-selection and dimensionality-reduction class/objects for Mathy modeling workflows. The module centralizes variance thresholding, canonical correlation analysis, principal component analysis, univariate feature selection, sequential backward selection, recursive feature elimination, train/test splitting, projection, transformation, and scoring utilities behind a consistent selector interface.

Selector

Selector feature-selection class/object.

Purpose

Defines the shared feature-selection interface used by dimensionality-reduction and selector class/objects. The base class stores prediction, transformed-data, accuracy, and plotting-marker state and specifies the train, project, transform, fit-transform, scoring, and splitting contracts implemented by concrete selectors.

Attributes:

Name Type Description
markers Optional[List[str]]

Matplotlib marker symbols available for selector plots.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

transformed_data Optional[ndarray]

Most recent feature matrix produced by projection or transformation.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

Source code in features.py
class Selector( ):
	"""Selector feature-selection class/object.

	Purpose:
	    Defines the shared feature-selection interface used by dimensionality-reduction and
	    selector class/objects. The base class stores prediction, transformed-data, accuracy, and
	    plotting-marker state and specifies the train, project, transform, fit-transform,
	    scoring, and splitting contracts implemented by concrete selectors.

	Attributes:
	    markers (Optional[List[str]]): Matplotlib marker symbols available for selector
	                                   plots.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    transformed_data (Optional[np.ndarray]): Most recent feature matrix produced by
	                                             projection or transformation.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation."""
	markers: Optional[ List[ str ] ]
	prediction: Optional[ np.ndarray ]
	transformed_data: Optional[ np.ndarray ]
	accuracy: Optional[ float ]

	def __init__( self ) -> None:
		"""Initialize Selector.

		Purpose:
		    Initializes shared selector runtime state, plotting markers, prediction storage,
		    transformed-data storage, and accuracy storage used by concrete feature-selection
		    class/objects.

		Returns:
		    None: This method initializes the object and does not return a value."""
		self.markers = [ '.', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', '8', 's', 'p', 'P', '*',
			'h', 'H', '+', 'x', 'X', 'd', 'D' ]
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None

	def split_data( self, X: np.ndarray, y: np.ndarray ) -> tuple:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    tuple: Tuple containing training features, testing features, training targets, and
		           testing targets.

		Raises:
		    NotImplementedError: Raised when required fitted estimator metadata is unavailable
		                         or when the base interface is called directly."""
		raise NotImplementedError

	def train( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> object | None:
		"""Fit Selector.

		Purpose:
		    Defines the training contract for concrete feature selectors and
		    dimensionality-reduction class/objects.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    NotImplementedError: Raised when required fitted estimator metadata is unavailable
		                         or when the base interface is called directly."""
		raise NotImplementedError

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> np.ndarray:
		"""Project features with Selector.

		Purpose:
		    Defines the projection contract for concrete selectors that convert feature matrices
		    into selected or reduced representations.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    NotImplementedError: Raised when required fitted estimator metadata is unavailable
		                         or when the base interface is called directly."""
		raise NotImplementedError

	def transform( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> object | None:
		"""Transform features with Selector.

		Purpose:
		    Defines the transformation contract for fitted selectors and
		    dimensionality-reduction class/objects.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Transformed feature matrix produced by the fitted selector.

		Raises:
		    NotImplementedError: Raised when required fitted estimator metadata is unavailable
		                         or when the base interface is called directly."""
		raise NotImplementedError

	def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> object | None:
		"""Fit and transform with Selector.

		Purpose:
		    Defines the fit-transform contract for selectors that learn and apply a selected or
		    reduced feature representation in one operation.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Transformed feature matrix produced after fitting the selector.

		Raises:
		    NotImplementedError: Raised when required fitted estimator metadata is unavailable
		                         or when the base interface is called directly."""
		raise NotImplementedError

	def score( self, X: np.ndarray, y: np.ndarray ) -> Dict[ str, float ] | None:
		"""Score Selector.

		Purpose:
		    Defines the scoring contract for concrete selectors that evaluate model performance
		    on selected or reduced features.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    Dict[str, float] | None: Dataframe or dictionary containing training, testing, and
		                             accuracy metrics.

		Raises:
		    NotImplementedError: Raised when required fitted estimator metadata is unavailable
		                         or when the base interface is called directly."""
		raise NotImplementedError

split_data

split_data(X: ndarray, y: ndarray) -> tuple

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Name Type Description
tuple tuple

Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
NotImplementedError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray ) -> tuple:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    tuple: Tuple containing training features, testing features, training targets, and
	           testing targets.

	Raises:
	    NotImplementedError: Raised when required fitted estimator metadata is unavailable
	                         or when the base interface is called directly."""
	raise NotImplementedError

train

train(
    X: ndarray, y: Optional[ndarray] = None
) -> object | None

Fit Selector.

Purpose

Defines the training contract for concrete feature selectors and dimensionality-reduction class/objects.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
object | None

object | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
NotImplementedError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

Source code in features.py
def train( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> object | None:
	"""Fit Selector.

	Purpose:
	    Defines the training contract for concrete feature selectors and
	    dimensionality-reduction class/objects.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    NotImplementedError: Raised when required fitted estimator metadata is unavailable
	                         or when the base interface is called directly."""
	raise NotImplementedError

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with Selector.

Purpose

Defines the projection contract for concrete selectors that convert feature matrices into selected or reduced representations.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
NotImplementedError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> np.ndarray:
	"""Project features with Selector.

	Purpose:
	    Defines the projection contract for concrete selectors that convert feature matrices
	    into selected or reduced representations.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    NotImplementedError: Raised when required fitted estimator metadata is unavailable
	                         or when the base interface is called directly."""
	raise NotImplementedError

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> object | None

Transform features with Selector.

Purpose

Defines the transformation contract for fitted selectors and dimensionality-reduction class/objects.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
object | None

object | None: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
NotImplementedError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

Source code in features.py
def transform( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> object | None:
	"""Transform features with Selector.

	Purpose:
	    Defines the transformation contract for fitted selectors and
	    dimensionality-reduction class/objects.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Transformed feature matrix produced by the fitted selector.

	Raises:
	    NotImplementedError: Raised when required fitted estimator metadata is unavailable
	                         or when the base interface is called directly."""
	raise NotImplementedError

train_transform

train_transform(
    X: ndarray, y: Optional[ndarray] = None
) -> object | None

Fit and transform with Selector.

Purpose

Defines the fit-transform contract for selectors that learn and apply a selected or reduced feature representation in one operation.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
object | None

object | None: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
NotImplementedError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

Source code in features.py
def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ] = None ) -> object | None:
	"""Fit and transform with Selector.

	Purpose:
	    Defines the fit-transform contract for selectors that learn and apply a selected or
	    reduced feature representation in one operation.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Transformed feature matrix produced after fitting the selector.

	Raises:
	    NotImplementedError: Raised when required fitted estimator metadata is unavailable
	                         or when the base interface is called directly."""
	raise NotImplementedError

score

score(X: ndarray, y: ndarray) -> Dict[str, float] | None

Score Selector.

Purpose

Defines the scoring contract for concrete selectors that evaluate model performance on selected or reduced features.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
Dict[str, float] | None

Dict[str, float] | None: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
NotImplementedError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

Source code in features.py
def score( self, X: np.ndarray, y: np.ndarray ) -> Dict[ str, float ] | None:
	"""Score Selector.

	Purpose:
	    Defines the scoring contract for concrete selectors that evaluate model performance
	    on selected or reduced features.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    Dict[str, float] | None: Dataframe or dictionary containing training, testing, and
	                             accuracy metrics.

	Raises:
	    NotImplementedError: Raised when required fitted estimator metadata is unavailable
	                         or when the base interface is called directly."""
	raise NotImplementedError

VarianceThreshold

Bases: Selector

VarianceThreshold feature-selection class/object.

Purpose

Removes low-variance features with VarianceThreshold. The class/object stores the threshold configuration, transformed feature matrix, prediction state, and train/test scoring metrics used to evaluate the reduced feature space.

Attributes:

Name Type Description
model VarianceThreshold

Underlying sklearn selector, decomposition, or cross-decomposition estimator.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

transformed_data Optional[ndarray]

Most recent feature matrix produced by projection or transformation.

threshold Optional[float]

Variance threshold used to retain features.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

training_score Optional[float]

Estimator score calculated on the training split.

testing_score Optional[float]

Estimator score calculated on the testing split.

Source code in features.py
class VarianceThreshold( Selector ):
	"""VarianceThreshold feature-selection class/object.

	Purpose:
	    Removes low-variance features with  VarianceThreshold. The
	    class/object stores the threshold configuration, transformed feature matrix, prediction
	    state, and train/test scoring metrics used to evaluate the reduced feature space.

	Attributes:
	    model (sf.VarianceThreshold): Underlying sklearn selector, decomposition, or
	                                  cross-decomposition estimator.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    transformed_data (Optional[np.ndarray]): Most recent feature matrix produced by
	                                             projection or transformation.
	    threshold (Optional[float]): Variance threshold used to retain features.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation.
	    training_score (Optional[float]): Estimator score calculated on the training split.
	    testing_score (Optional[float]): Estimator score calculated on the testing split."""
	model: sf.VarianceThreshold
	prediction: Optional[ np.ndarray ]
	transformed_data: Optional[ np.ndarray ]
	threshold: Optional[ float ]
	accuracy: Optional[ float ]
	training_score: Optional[ float ]
	testing_score: Optional[ float ]

	def __init__( self, thresh: float = 0.0 ) -> None:
		"""Initialize VarianceThreshold.

		Purpose:
		    Initializes the variance-threshold selector with the requested variance cutoff,
		    wrapped sklearn estimator, prediction state, transformed-data storage, and score
		    fields.

		Args:
		    thresh (float): Variance threshold used to retain features.

		Returns:
		    None: This method initializes the object and does not return a value."""
		super( ).__init__( )
		self.threshold = thresh
		self.model = sf.VarianceThreshold( threshold=self.threshold )
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None
		self.training_score = None
		self.testing_score = None

	def __dir__( self ) -> List[ str ]:
		"""List public members.

		Purpose:
		    Returns the stable list of public members exposed by the VarianceThreshold class/object
		    for interactive inspection, notebook exploration, and IDE discovery.

		Returns:
		    List[str]: Public member names exposed by the class/object."""
		return [ 'threshold', 'model', 'prediction', 'transformed_data', 'split_data', 'train',
			'project', 'score', 'transform', 'train_transform' ]

	def split_data( self, X: np.ndarray, y: np.ndarray, size: float = 0.2,
			random: int = 42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
		    size (float): Testing-set proportion used by train/test splitting.
		    random (int): Random seed used by train/test splitting.

		Returns:
		    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
		                                                           features, testing features,
		                                                           training targets, and testing
		                                                           targets.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
			return (X_train, X_test, y_train, y_test)
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'VarianceThreshold'
			exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
			Logger( ).write( exception )
			raise exception

	def train( self, X: np.ndarray,
			y: Optional[ np.ndarray ] = None ) -> sf.VarianceThreshold | None:
		"""Fit VarianceThreshold.

		Purpose:
		    Fits the variance-threshold selector to the supplied feature matrix so low-variance
		    columns can be removed.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    sf.VarianceThreshold | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.model.fit( X )
			return self
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'VarianceThreshold'
			exception.method = 'train( self, *args ) -> sf.VarianceThreshold | None'
			Logger( ).write( exception )
			raise exception

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Project features with VarianceThreshold.

		Purpose:
		    Projects the supplied feature matrix with the fitted VarianceThreshold selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.prediction = self.transform( X )
			return self.prediction
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'VarianceThreshold'
			exception.method = 'project( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
		"""Score VarianceThreshold.

		Purpose:
		    Evaluates classifier performance after applying the fitted VarianceThreshold
		    selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
		                  metrics.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			if self.transformed_data is None:
				self.transformed_data = self.transform( X )
			_support = self.model.get_support( )
			_original_count = int( X.shape[ 1 ] )
			_retained_count = int( self.transformed_data.shape[ 1 ] )
			_removed_count = int( _original_count - _retained_count )
			_metrics = \
			{
					'Threshold': [ self.threshold ],
					'Original Features': [ _original_count ],
					'Retained Features': [ _retained_count ],
					'Removed Features': [ _removed_count ],
					'Selection Ratio': [ _retained_count / _original_count if _original_count else 0.0 ],
					'Removed Ratio': [
							_removed_count / _original_count if _original_count else 0.0 ],
					'Minimum Retained Variance': [
							float( np.min( self.model.variances_[ _support ] ) )
							if np.any( _support ) else np.nan ],
					'Maximum Retained Variance': [
							float( np.max( self.model.variances_[ _support ] ) )
							if np.any( _support ) else np.nan ],
			}
			return pd.DataFrame( _metrics )
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'VarianceThreshold'
			exception.method = 'score( self, *args ) -> pd.DataFrame'
			Logger( ).write( exception )
			raise exception

	def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Transform features with VarianceThreshold.

		Purpose:
		    Transforms the supplied feature matrix with the fitted VarianceThreshold selector
		    and stores the transformed output.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'VarianceThreshold'
			exception.method = 'transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Fit and transform with VarianceThreshold.

		Purpose:
		    Fits the VarianceThreshold selector and returns the transformed feature matrix in
		    one operation.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced after fitting the selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.fit_transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'VarianceThreshold'
			exception.method = 'train_transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

split_data

split_data(
    X: ndarray,
    y: ndarray,
    size: float = 0.2,
    random: int = 42,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required
size float

Testing-set proportion used by train/test splitting.

0.2
random int

Random seed used by train/test splitting.

42

Returns:

Type Description
Tuple[ndarray, ndarray, ndarray, ndarray]

Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray, size: float = 0.2,
		random: int = 42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
	    size (float): Testing-set proportion used by train/test splitting.
	    random (int): Random seed used by train/test splitting.

	Returns:
	    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
	                                                           features, testing features,
	                                                           training targets, and testing
	                                                           targets.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
		return (X_train, X_test, y_train, y_test)
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'VarianceThreshold'
		exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
		Logger( ).write( exception )
		raise exception

train

train(
    X: ndarray, y: Optional[ndarray] = None
) -> sf.VarianceThreshold | None

Fit VarianceThreshold.

Purpose

Fits the variance-threshold selector to the supplied feature matrix so low-variance columns can be removed.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
VarianceThreshold | None

sf.VarianceThreshold | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train( self, X: np.ndarray,
		y: Optional[ np.ndarray ] = None ) -> sf.VarianceThreshold | None:
	"""Fit VarianceThreshold.

	Purpose:
	    Fits the variance-threshold selector to the supplied feature matrix so low-variance
	    columns can be removed.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    sf.VarianceThreshold | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.model.fit( X )
		return self
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'VarianceThreshold'
		exception.method = 'train( self, *args ) -> sf.VarianceThreshold | None'
		Logger( ).write( exception )
		raise exception

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with VarianceThreshold.

Purpose

Projects the supplied feature matrix with the fitted VarianceThreshold selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Project features with VarianceThreshold.

	Purpose:
	    Projects the supplied feature matrix with the fitted VarianceThreshold selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.prediction = self.transform( X )
		return self.prediction
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'VarianceThreshold'
		exception.method = 'project( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

score

score(
    X: ndarray, y: Optional[ndarray] = None
) -> pd.DataFrame

Score VarianceThreshold.

Purpose

Evaluates classifier performance after applying the fitted VarianceThreshold selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
DataFrame

pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
	"""Score VarianceThreshold.

	Purpose:
	    Evaluates classifier performance after applying the fitted VarianceThreshold
	    selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
	                  metrics.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		if self.transformed_data is None:
			self.transformed_data = self.transform( X )
		_support = self.model.get_support( )
		_original_count = int( X.shape[ 1 ] )
		_retained_count = int( self.transformed_data.shape[ 1 ] )
		_removed_count = int( _original_count - _retained_count )
		_metrics = \
		{
				'Threshold': [ self.threshold ],
				'Original Features': [ _original_count ],
				'Retained Features': [ _retained_count ],
				'Removed Features': [ _removed_count ],
				'Selection Ratio': [ _retained_count / _original_count if _original_count else 0.0 ],
				'Removed Ratio': [
						_removed_count / _original_count if _original_count else 0.0 ],
				'Minimum Retained Variance': [
						float( np.min( self.model.variances_[ _support ] ) )
						if np.any( _support ) else np.nan ],
				'Maximum Retained Variance': [
						float( np.max( self.model.variances_[ _support ] ) )
						if np.any( _support ) else np.nan ],
		}
		return pd.DataFrame( _metrics )
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'VarianceThreshold'
		exception.method = 'score( self, *args ) -> pd.DataFrame'
		Logger( ).write( exception )
		raise exception

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Transform features with VarianceThreshold.

Purpose

Transforms the supplied feature matrix with the fitted VarianceThreshold selector and stores the transformed output.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Transform features with VarianceThreshold.

	Purpose:
	    Transforms the supplied feature matrix with the fitted VarianceThreshold selector
	    and stores the transformed output.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'VarianceThreshold'
		exception.method = 'transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

train_transform

train_transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Fit and transform with VarianceThreshold.

Purpose

Fits the VarianceThreshold selector and returns the transformed feature matrix in one operation.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Fit and transform with VarianceThreshold.

	Purpose:
	    Fits the VarianceThreshold selector and returns the transformed feature matrix in
	    one operation.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced after fitting the selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.fit_transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'VarianceThreshold'
		exception.method = 'train_transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

CCA

Bases: Selector

CCA feature-selection class/object.

Purpose

Projects paired feature and target matrices with canonical correlation analysis. The class/object fits sklearn cross-decomposition CCA, stores canonical components, supports supervised projections, and evaluates downstream classifier performance on the transformed representation.

Attributes:

Name Type Description
model Optional[object]

Underlying sklearn selector, decomposition, or cross-decomposition estimator.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

n_components Optional[int]

Number of components retained or projected by the selector.

scale Optional[bool]

Flag controlling feature scaling inside canonical correlation analysis.

max_iter Optional[int]

Maximum solver iterations used by the wrapped estimator.

transformed_data Optional[Tuple[ndarray, ndarray]]

Most recent feature matrix produced by projection or transformation.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

training_score Optional[float]

Estimator score calculated on the training split.

testing_score Optional[float]

Estimator score calculated on the testing split.

Source code in features.py
class CCA( Selector ):
	"""CCA feature-selection class/object.

	Purpose:
	    Projects paired feature and target matrices with canonical correlation analysis. The
	    class/object fits sklearn cross-decomposition CCA, stores canonical components, supports
	    supervised projections, and evaluates downstream classifier performance on the
	    transformed representation.

	Attributes:
	    model (Optional[object]): Underlying sklearn selector, decomposition, or
	                              cross-decomposition estimator.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    n_components (Optional[int]): Number of components retained or projected by the
	                                  selector.
	    scale (Optional[bool]): Flag controlling feature scaling inside canonical
	                            correlation analysis.
	    max_iter (Optional[int]): Maximum solver iterations used by the wrapped estimator.
	    transformed_data (Optional[Tuple[np.ndarray, np.ndarray]]): Most recent feature
	                                                                matrix produced by
	                                                                projection or
	                                                                transformation.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation.
	    training_score (Optional[float]): Estimator score calculated on the training split.
	    testing_score (Optional[float]): Estimator score calculated on the testing split."""
	model: Optional[ CanonicalCorrelationAnalysis ]
	prediction: Optional[ np.ndarray ]
	n_components: Optional[ int ]
	scale: Optional[ bool ]
	max_iter: Optional[ int ]
	transformed_data: Optional[ Tuple[ np.ndarray, np.ndarray ] ]
	accuracy: Optional[ float ]
	training_score: Optional[ float ]
	testing_score: Optional[ float ]

	def __init__( self, num: int=2, scale: bool=True, size: int=500 ) -> None:
		"""Initialize CCA.

		Purpose:
		    Initializes the canonical-correlation selector with component count, scaling
		    behavior, maximum iteration count, wrapped sklearn estimator, transformed-data
		    storage, and score fields.

		Args:
		    num (int): Number of components or features retained by the selector.
		    scale (bool): Flag controlling whether CCA scales input variables.
		    size (int): Testing-set proportion used by train/test splitting.

		Returns:
		    None: This method initializes the object and does not return a value."""
		super( ).__init__( )
		self.scale = scale
		self.n_components = num
		self.max_iter = size
		self.model = CanonicalCorrelationAnalysis( n_components=self.n_components, scale=self.scale,
			max_iter=self.max_iter )
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None
		self.training_score = None
		self.testing_score = None

	def __dir__( self ) -> List[ str ]:
		"""List public members.

		Purpose:
		    Returns the stable list of public members exposed by the CCA class/object for interactive
		    inspection, notebook exploration, and IDE discovery.

		Returns:
		    List[str]: Public member names exposed by the class/object."""
		return [ 'model', 'n_components', 'scale', 'max_iter', 'prediction', 'transformed_data',
			'split_data', 'train', 'project', 'score', 'transform', 'train_transform' ]

	def split_data( self, X: np.ndarray, y: np.ndarray, size: float = 0.2,
			random: int = 42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
		    size (float): Testing-set proportion used by train/test splitting.
		    random (int): Random seed used by train/test splitting.

		Returns:
		    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
		                                                           features, testing features,
		                                                           training targets, and testing
		                                                           targets.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
			return (X_train, X_test, y_train, y_test)
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'CCA'
			exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
			Logger( ).write( exception )
			raise exception

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Project features with CCA.

		Purpose:
		    Projects feature data into the canonical correlation space learned by the fitted CCA
		    estimator.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.prediction = self.model.predict( X )
			return self.prediction
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'CCA'
			exception.method = 'project( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def score( self, X: np.ndarray, y: np.ndarray ) -> pd.DataFrame:
		"""Score CCA.

		Purpose:
		    Evaluates classifier performance on canonical-correlation transformed training and
		    testing partitions.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
		                  metrics.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_training, X_testing, y_training, y_testing = self.split_data( X, y )
			self.training_score = self.model.score( X_training, y_training )
			self.testing_score = self.model.score( X_testing, y_testing )
			y_pred = self.project( X )
			_target_count = int( y.shape[ 1 ] ) if len( y.shape ) > 1 else 1
			_prediction_count = int( y_pred.shape[ 1 ] ) if len( y_pred.shape ) > 1 else 1
			_metrics = \
			{
					'Components': [ self.n_components ],
					'Training Score': [ float( self.training_score ) ],
					'Testing Score': [ float( self.testing_score ) ],
					'Predictor Count': [ int( X.shape[ 1 ] ) ],
					'Target Count': [ _target_count ],
					'Prediction Count': [ _prediction_count ],
			}
			df_metrics = pd.DataFrame( _metrics )
			return df_metrics
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'CCA'
			exception.method = 'score( self, *args ) -> pd.DataFrame'
			Logger( ).write( exception )
			raise exception

	def train( self, X: np.ndarray, y: np.ndarray ) -> object | None:
		"""Fit CCA.

		Purpose:
		    Fits canonical correlation analysis to paired feature and target arrays so shared
		    covariance directions can be learned.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			self.model.fit( X, y )
			return self
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'CCA'
			exception.method = 'train( self, *args ) -> object | None'
			Logger( ).write( exception )
			raise exception

	def transform( self, X: np.ndarray,
			y: Optional[ np.ndarray ]=None ) -> Tuple[ np.ndarray, np.ndarray ] | np.ndarray:
		"""Transform features with CCA.

		Purpose:
		    Transforms feature data, and target data when supplied, with the fitted canonical
		    correlation estimator.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    Tuple[np.ndarray, np.ndarray] | np.ndarray: Transformed feature matrix produced by
		                                                the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			if y is None:
				self.transformed_data = self.model.transform( X )
			else:
				self.transformed_data = self.model.transform( X, y )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'CCA'
			exception.method = 'transform( self, *args ) -> Tuple[np.ndarray, np.ndarray] | np.ndarray'
			Logger( ).write( exception )
			raise exception

	def train_transform( self, X: np.ndarray,
		y: np.ndarray ) -> Tuple[ np.ndarray, np.ndarray ] | np.ndarray:
		"""Fit and transform with CCA.

		Purpose:
		    Fits the CCA selector and returns the transformed feature matrix in one operation.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    Tuple[np.ndarray, np.ndarray] | np.ndarray: Transformed feature matrix produced
		                                                after fitting the selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			self.transformed_data = self.model.fit_transform( X, y )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'CCA'
			exception.method = 'train_transform( self, *args ) -> Tuple[np.ndarray, np.ndarray] | np.ndarray'
			Logger( ).write( exception )
			raise exception

split_data

split_data(
    X: ndarray,
    y: ndarray,
    size: float = 0.2,
    random: int = 42,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required
size float

Testing-set proportion used by train/test splitting.

0.2
random int

Random seed used by train/test splitting.

42

Returns:

Type Description
Tuple[ndarray, ndarray, ndarray, ndarray]

Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray, size: float = 0.2,
		random: int = 42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
	    size (float): Testing-set proportion used by train/test splitting.
	    random (int): Random seed used by train/test splitting.

	Returns:
	    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
	                                                           features, testing features,
	                                                           training targets, and testing
	                                                           targets.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
		return (X_train, X_test, y_train, y_test)
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'CCA'
		exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
		Logger( ).write( exception )
		raise exception

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with CCA.

Purpose

Projects feature data into the canonical correlation space learned by the fitted CCA estimator.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Project features with CCA.

	Purpose:
	    Projects feature data into the canonical correlation space learned by the fitted CCA
	    estimator.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.prediction = self.model.predict( X )
		return self.prediction
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'CCA'
		exception.method = 'project( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

score

score(X: ndarray, y: ndarray) -> pd.DataFrame

Score CCA.

Purpose

Evaluates classifier performance on canonical-correlation transformed training and testing partitions.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
DataFrame

pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def score( self, X: np.ndarray, y: np.ndarray ) -> pd.DataFrame:
	"""Score CCA.

	Purpose:
	    Evaluates classifier performance on canonical-correlation transformed training and
	    testing partitions.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
	                  metrics.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_training, X_testing, y_training, y_testing = self.split_data( X, y )
		self.training_score = self.model.score( X_training, y_training )
		self.testing_score = self.model.score( X_testing, y_testing )
		y_pred = self.project( X )
		_target_count = int( y.shape[ 1 ] ) if len( y.shape ) > 1 else 1
		_prediction_count = int( y_pred.shape[ 1 ] ) if len( y_pred.shape ) > 1 else 1
		_metrics = \
		{
				'Components': [ self.n_components ],
				'Training Score': [ float( self.training_score ) ],
				'Testing Score': [ float( self.testing_score ) ],
				'Predictor Count': [ int( X.shape[ 1 ] ) ],
				'Target Count': [ _target_count ],
				'Prediction Count': [ _prediction_count ],
		}
		df_metrics = pd.DataFrame( _metrics )
		return df_metrics
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'CCA'
		exception.method = 'score( self, *args ) -> pd.DataFrame'
		Logger( ).write( exception )
		raise exception

train

train(X: ndarray, y: ndarray) -> object | None

Fit CCA.

Purpose

Fits canonical correlation analysis to paired feature and target arrays so shared covariance directions can be learned.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
object | None

object | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train( self, X: np.ndarray, y: np.ndarray ) -> object | None:
	"""Fit CCA.

	Purpose:
	    Fits canonical correlation analysis to paired feature and target arrays so shared
	    covariance directions can be learned.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		self.model.fit( X, y )
		return self
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'CCA'
		exception.method = 'train( self, *args ) -> object | None'
		Logger( ).write( exception )
		raise exception

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> Tuple[np.ndarray, np.ndarray] | np.ndarray

Transform features with CCA.

Purpose

Transforms feature data, and target data when supplied, with the fitted canonical correlation estimator.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
Tuple[ndarray, ndarray] | ndarray

Tuple[np.ndarray, np.ndarray] | np.ndarray: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def transform( self, X: np.ndarray,
		y: Optional[ np.ndarray ]=None ) -> Tuple[ np.ndarray, np.ndarray ] | np.ndarray:
	"""Transform features with CCA.

	Purpose:
	    Transforms feature data, and target data when supplied, with the fitted canonical
	    correlation estimator.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    Tuple[np.ndarray, np.ndarray] | np.ndarray: Transformed feature matrix produced by
	                                                the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		if y is None:
			self.transformed_data = self.model.transform( X )
		else:
			self.transformed_data = self.model.transform( X, y )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'CCA'
		exception.method = 'transform( self, *args ) -> Tuple[np.ndarray, np.ndarray] | np.ndarray'
		Logger( ).write( exception )
		raise exception

train_transform

train_transform(
    X: ndarray, y: ndarray
) -> Tuple[np.ndarray, np.ndarray] | np.ndarray

Fit and transform with CCA.

Purpose

Fits the CCA selector and returns the transformed feature matrix in one operation.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
Tuple[ndarray, ndarray] | ndarray

Tuple[np.ndarray, np.ndarray] | np.ndarray: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train_transform( self, X: np.ndarray,
	y: np.ndarray ) -> Tuple[ np.ndarray, np.ndarray ] | np.ndarray:
	"""Fit and transform with CCA.

	Purpose:
	    Fits the CCA selector and returns the transformed feature matrix in one operation.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    Tuple[np.ndarray, np.ndarray] | np.ndarray: Transformed feature matrix produced
	                                                after fitting the selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		self.transformed_data = self.model.fit_transform( X, y )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'CCA'
		exception.method = 'train_transform( self, *args ) -> Tuple[np.ndarray, np.ndarray] | np.ndarray'
		Logger( ).write( exception )
		raise exception

PCA

Bases: Selector

PCA feature-selection class/object.

Purpose

Projects numeric features into a lower-dimensional principal-component space with sklearn decomposition PCA. The class/object stores component configuration, explained-variance metadata, transformed features, and train/test evaluation metrics for reduced feature sets.

Attributes:

Name Type Description
model Optional[object]

Underlying sklearn selector, decomposition, or cross-decomposition estimator.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

svd_solver Optional[str]

SVD solver strategy used by PCA.

n_components Optional[int]

Number of components retained or projected by the selector.

transformed_data Optional[ndarray]

Most recent feature matrix produced by projection or transformation.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

training_score Optional[float]

Estimator score calculated on the training split.

testing_score Optional[float]

Estimator score calculated on the testing split.

Source code in features.py
class PCA( Selector ):
	"""PCA feature-selection class/object.

	Purpose:
	    Projects numeric features into a lower-dimensional principal-component space with
	    sklearn decomposition PCA. The class/object stores component configuration,
	    explained-variance metadata, transformed features, and train/test evaluation metrics
	    for reduced feature sets.

	Attributes:
	    model (Optional[object]): Underlying sklearn selector, decomposition, or
	                              cross-decomposition estimator.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    svd_solver (Optional[str]): SVD solver strategy used by PCA.
	    n_components (Optional[int]): Number of components retained or projected by the
	                                  selector.
	    transformed_data (Optional[np.ndarray]): Most recent feature matrix produced by
	                                             projection or transformation.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation.
	    training_score (Optional[float]): Estimator score calculated on the training split.
	    testing_score (Optional[float]): Estimator score calculated on the testing split."""
	model: Optional[ PrincipalComponentAnalysis ]
	prediction: Optional[ np.ndarray ]
	svd_solver: Optional[ str ]
	n_components: Optional[ int ]
	transformed_data: Optional[ np.ndarray ]
	accuracy: Optional[ float ]
	training_score: Optional[ float ]
	testing_score: Optional[ float ]

	def __init__( self, num: int = 2, solver: str = 'auto' ) -> None:
		"""Initialize PCA.

		Purpose:
		    Initializes the principal-component selector with component count, SVD solver
		    configuration, wrapped sklearn estimator, transformed-data storage, and score
		    fields.

		Args:
		    num (int): Number of components or features retained by the selector.
		    solver (str): SVD solver strategy passed to the PCA estimator.

		Returns:
		    None: This method initializes the object and does not return a value."""
		super( ).__init__( )
		self.n_components = num
		self.svd_solver = solver
		self.model = PrincipalComponentAnalysis( n_components=self.n_components,
			svd_solver=self.svd_solver )
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None
		self.training_score = None
		self.testing_score = None

	def __dir__( self ) -> List[ str ]:
		"""List public members.

		Purpose:
		    Returns the stable list of public members exposed by the PCA class/object for interactive
		    inspection, notebook exploration, and IDE discovery.

		Returns:
		    List[str]: Public member names exposed by the class/object."""
		return [ 'model', 'prediction', 'svd_solver', 'n_components', 'transformed_data',
			'split_data', 'train', 'project', 'score', 'transform', 'train_transform' ]

	@property
	def explained_variance_ratio( self ) -> float:
		"""Performs the `explained_variance_ratio` operation defined by `PCA`.

		Purpose:
		    Performs the `explained_variance_ratio` operation defined by `PCA`.

		Returns:
		    float: Value produced by the operation."""
		if self.model is not None:
			return self.model.explained_variance_ratio_

	def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
			random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
		    size (float): Testing-set proportion used by train/test splitting.
		    random (int): Random seed used by train/test splitting.

		Returns:
		    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
		                                                           features, testing features,
		                                                           training targets, and testing
		                                                           targets.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
			return (X_train, X_test, y_train, y_test)
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'PCA'
			exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
			Logger( ).write( exception )
			raise exception

	def train( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> object | None:
		"""Fit PCA.

		Purpose:
		    Fits principal component analysis to the supplied feature matrix so principal axes
		    and explained variance can be learned.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.model.fit( X )
			return self
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'PCA'
			exception.method = 'train( self, *args ) -> object | None'
			Logger( ).write( exception )
			raise exception

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Project features with PCA.

		Purpose:
		    Projects feature data into the principal-component space learned by the fitted PCA
		    estimator.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.prediction = self.transform( X )
			return self.prediction
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'PCA'
			exception.method = 'project( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
		"""Score PCA.

		Purpose:
		    Evaluates classifier performance on PCA-transformed training and testing partitions.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
		                  metrics.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			if self.transformed_data is None:
				self.transformed_data = self.transform( X )
			_original_count = int( X.shape[ 1 ] )
			_component_count = int( self.transformed_data.shape[ 1 ] )
			_total_explained = float( np.sum( self.model.explained_variance_ratio_ ) ) \
				if hasattr( self.model, 'explained_variance_ratio_' ) else np.nan
			_metrics = { 'Original Features': [ _original_count ],
				'Components': [ _component_count ],
				'Explained Variance Total': [ _total_explained ], 'Largest Component Variance': [
					float( np.max( self.model.explained_variance_ratio_ ) ) if hasattr( self.model,
						'explained_variance_ratio_' ) else np.nan ],
				'Smallest Component Variance': [
					float( np.min( self.model.explained_variance_ratio_ ) ) if hasattr( self.model,
						'explained_variance_ratio_' ) else np.nan ],
				'Solver': [ self.svd_solver ], }
			df_metrics = pd.DataFrame( _metrics )
			return df_metrics
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'PCA'
			exception.method = 'score( self, *args ) -> pd.DataFrame'
			Logger( ).write( exception )
			raise exception

	def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Transform features with PCA.

		Purpose:
		    Transforms the supplied feature matrix with the fitted PCA selector and stores the
		    transformed output.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'PCA'
			exception.method = 'transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Fit and transform with PCA.

		Purpose:
		    Fits the PCA selector and returns the transformed feature matrix in one operation.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced after fitting the selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.fit_transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'PCA'
			exception.method = 'train_transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

explained_variance_ratio property

explained_variance_ratio: float

Performs the explained_variance_ratio operation defined by PCA.

Purpose

Performs the explained_variance_ratio operation defined by PCA.

Returns:

Name Type Description
float float

Value produced by the operation.

split_data

split_data(
    X: ndarray,
    y: ndarray,
    size: float = 0.2,
    random: int = 42,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required
size float

Testing-set proportion used by train/test splitting.

0.2
random int

Random seed used by train/test splitting.

42

Returns:

Type Description
Tuple[ndarray, ndarray, ndarray, ndarray]

Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
		random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
	    size (float): Testing-set proportion used by train/test splitting.
	    random (int): Random seed used by train/test splitting.

	Returns:
	    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
	                                                           features, testing features,
	                                                           training targets, and testing
	                                                           targets.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
		return (X_train, X_test, y_train, y_test)
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'PCA'
		exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
		Logger( ).write( exception )
		raise exception

train

train(
    X: ndarray, y: Optional[ndarray] = None
) -> object | None

Fit PCA.

Purpose

Fits principal component analysis to the supplied feature matrix so principal axes and explained variance can be learned.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
object | None

object | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> object | None:
	"""Fit PCA.

	Purpose:
	    Fits principal component analysis to the supplied feature matrix so principal axes
	    and explained variance can be learned.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.model.fit( X )
		return self
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'PCA'
		exception.method = 'train( self, *args ) -> object | None'
		Logger( ).write( exception )
		raise exception

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with PCA.

Purpose

Projects feature data into the principal-component space learned by the fitted PCA estimator.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Project features with PCA.

	Purpose:
	    Projects feature data into the principal-component space learned by the fitted PCA
	    estimator.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.prediction = self.transform( X )
		return self.prediction
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'PCA'
		exception.method = 'project( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

score

score(
    X: ndarray, y: Optional[ndarray] = None
) -> pd.DataFrame

Score PCA.

Purpose

Evaluates classifier performance on PCA-transformed training and testing partitions.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
DataFrame

pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
	"""Score PCA.

	Purpose:
	    Evaluates classifier performance on PCA-transformed training and testing partitions.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
	                  metrics.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		if self.transformed_data is None:
			self.transformed_data = self.transform( X )
		_original_count = int( X.shape[ 1 ] )
		_component_count = int( self.transformed_data.shape[ 1 ] )
		_total_explained = float( np.sum( self.model.explained_variance_ratio_ ) ) \
			if hasattr( self.model, 'explained_variance_ratio_' ) else np.nan
		_metrics = { 'Original Features': [ _original_count ],
			'Components': [ _component_count ],
			'Explained Variance Total': [ _total_explained ], 'Largest Component Variance': [
				float( np.max( self.model.explained_variance_ratio_ ) ) if hasattr( self.model,
					'explained_variance_ratio_' ) else np.nan ],
			'Smallest Component Variance': [
				float( np.min( self.model.explained_variance_ratio_ ) ) if hasattr( self.model,
					'explained_variance_ratio_' ) else np.nan ],
			'Solver': [ self.svd_solver ], }
		df_metrics = pd.DataFrame( _metrics )
		return df_metrics
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'PCA'
		exception.method = 'score( self, *args ) -> pd.DataFrame'
		Logger( ).write( exception )
		raise exception

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Transform features with PCA.

Purpose

Transforms the supplied feature matrix with the fitted PCA selector and stores the transformed output.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Transform features with PCA.

	Purpose:
	    Transforms the supplied feature matrix with the fitted PCA selector and stores the
	    transformed output.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'PCA'
		exception.method = 'transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

train_transform

train_transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Fit and transform with PCA.

Purpose

Fits the PCA selector and returns the transformed feature matrix in one operation.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Fit and transform with PCA.

	Purpose:
	    Fits the PCA selector and returns the transformed feature matrix in one operation.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced after fitting the selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.fit_transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'PCA'
		exception.method = 'train_transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

SelectBest

Bases: Selector

SelectBest feature-selection class/object.

Purpose

Selects the top scoring features with sklearn.feature_selection.SelectKBest. The class/object applies univariate statistical tests, stores scores and transformed matrices, and evaluates classifier performance after retaining the configured number of features.

Attributes:

Name Type Description
model Optional[object]

Underlying sklearn selector, decomposition, or cross-decomposition estimator.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

score_function Optional[object]

Univariate scoring function used to rank candidate features.

n_features Optional[int]

Number of top-ranked features retained by SelectBest.

transformed_data Optional[ndarray]

Most recent feature matrix produced by projection or transformation.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

training_score Optional[float]

Estimator score calculated on the training split.

testing_score Optional[float]

Estimator score calculated on the testing split.

Source code in features.py
class SelectBest( Selector ):
	"""SelectBest feature-selection class/object.

	Purpose:
	    Selects the top scoring features with sklearn.feature_selection.SelectKBest. The
	    class/object applies univariate statistical tests, stores scores and transformed
	    matrices, and evaluates classifier performance after retaining the configured number
	    of features.

	Attributes:
	    model (Optional[object]): Underlying sklearn selector, decomposition, or
	                              cross-decomposition estimator.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    score_function (Optional[object]): Univariate scoring function used to rank
	                                       candidate features.
	    n_features (Optional[int]): Number of top-ranked features retained by SelectBest.
	    transformed_data (Optional[np.ndarray]): Most recent feature matrix produced by
	                                             projection or transformation.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation.
	    training_score (Optional[float]): Estimator score calculated on the training split.
	    testing_score (Optional[float]): Estimator score calculated on the testing split."""
	model: Optional[ sf.SelectKBest ]
	prediction: Optional[ np.ndarray ]
	score_function: Optional[ object ]
	n_features: Optional[ int ]
	transformed_data: Optional[ np.ndarray ]
	accuracy: Optional[ float ]
	training_score: Optional[ float ]
	testing_score: Optional[ float ]

	def __init__( self, score_func: object = sf.chi2, num: int = 10 ) -> None:
		"""Initialize SelectBest.

		Purpose:
		    Initializes the top-k univariate selector with its scoring function, feature count,
		    wrapped sklearn estimator, transformed-data storage, and score fields.

		Args:
		    score_func (object): Univariate score function used to rank candidate features.
		    num (int): Number of components or features retained by the selector.

		Returns:
		    None: This method initializes the object and does not return a value."""
		super( ).__init__( )
		self.score_function = score_func
		self.n_features = num
		self.model = sf.SelectKBest( score_func=self.score_function, k=self.n_features )
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None
		self.training_score = None
		self.testing_score = None

	def __dir__( self ) -> List[ str ]:
		"""List public members.

		Purpose:
		    Returns the stable list of public members exposed by the SelectBest class/object for
		    interactive inspection, notebook exploration, and IDE discovery.

		Returns:
		    List[str]: Public member names exposed by the class/object."""
		return [ 'model', 'prediction', 'score_function', 'n_features', 'transformed_data',
			'split_data', 'chi_square', 'train', 'project', 'score', 'transform',
			'train_transform' ]

	def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
			random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
		    size (float): Testing-set proportion used by train/test splitting.
		    random (int): Random seed used by train/test splitting.

		Returns:
		    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
		                                                           features, testing features,
		                                                           training targets, and testing
		                                                           targets.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
			return (X_train, X_test, y_train, y_test)
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectBest'
			exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
			Logger( ).write( exception )
			raise exception

	def chi_square( self, X: np.ndarray, y: np.ndarray ) -> Tuple[ np.ndarray, np.ndarray ] | None:
		"""Calculate chi-square scores.

		Purpose:
		    Computes chi-square feature scores and p-values for the supplied feature matrix and
		    target vector using the SelectBest scoring configuration.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    Tuple[np.ndarray, np.ndarray] | None: Tuple containing chi-square scores and
		                                          p-values.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			return sf.chi2( X, y )
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectBest'
			exception.method = 'chi_square( self, *args ) -> Tuple[np.ndarray, np.ndarray] | None'
			Logger( ).write( exception )
			raise exception

	def train( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> object | None:
		"""Fit SelectBest.

		Purpose:
		    Fits the top-k univariate selector to feature and target arrays so the highest
		    scoring features can be retained.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.model.fit( X, y )
			return self
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectBest'
			exception.method = 'train( self, *args ) -> object | None'
			Logger( ).write( exception )
			raise exception

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Project features with SelectBest.

		Purpose:
		    Projects the supplied feature matrix with the fitted SelectBest selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.prediction = self.transform( X )
			return self.prediction
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectBest'
			exception.method = 'project( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
		"""Score SelectBest.

		Purpose:
		    Evaluates classifier performance after applying the fitted SelectBest selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
		                  metrics.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			if self.transformed_data is None:
				self.transformed_data = self.transform( X )
			_support = self.model.get_support( )
			_scores = self.model.scores_
			_pvalues = self.model.pvalues_ if hasattr( self.model, 'pvalues_' ) else None
			df_scores = pd.DataFrame( { 'Feature': np.arange( 0, X.shape[ 1 ] ), 'Score': _scores,
				'PValue': _pvalues if _pvalues is not None else np.full( X.shape[ 1 ], np.nan ),
				'Selected': _support, } )
			return df_scores
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectBest'
			exception.method = 'score( self, *args ) -> pd.DataFrame'
			Logger( ).write( exception )
			raise exception

	def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Transform features with SelectBest.

		Purpose:
		    Transforms the supplied feature matrix with the fitted SelectBest selector and
		    stores the transformed output.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectBest'
			exception.method = 'transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Fit and transform with SelectBest.

		Purpose:
		    Fits the SelectBest selector and returns the transformed feature matrix in one
		    operation.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced after fitting the selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.fit_transform( X, y )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectBest'
			exception.method = 'train_transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

split_data

split_data(
    X: ndarray,
    y: ndarray,
    size: float = 0.2,
    random: int = 42,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required
size float

Testing-set proportion used by train/test splitting.

0.2
random int

Random seed used by train/test splitting.

42

Returns:

Type Description
Tuple[ndarray, ndarray, ndarray, ndarray]

Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
		random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
	    size (float): Testing-set proportion used by train/test splitting.
	    random (int): Random seed used by train/test splitting.

	Returns:
	    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
	                                                           features, testing features,
	                                                           training targets, and testing
	                                                           targets.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
		return (X_train, X_test, y_train, y_test)
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectBest'
		exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
		Logger( ).write( exception )
		raise exception

chi_square

chi_square(
    X: ndarray, y: ndarray
) -> Tuple[np.ndarray, np.ndarray] | None

Calculate chi-square scores.

Purpose

Computes chi-square feature scores and p-values for the supplied feature matrix and target vector using the SelectBest scoring configuration.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
Tuple[ndarray, ndarray] | None

Tuple[np.ndarray, np.ndarray] | None: Tuple containing chi-square scores and p-values.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def chi_square( self, X: np.ndarray, y: np.ndarray ) -> Tuple[ np.ndarray, np.ndarray ] | None:
	"""Calculate chi-square scores.

	Purpose:
	    Computes chi-square feature scores and p-values for the supplied feature matrix and
	    target vector using the SelectBest scoring configuration.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    Tuple[np.ndarray, np.ndarray] | None: Tuple containing chi-square scores and
	                                          p-values.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		return sf.chi2( X, y )
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectBest'
		exception.method = 'chi_square( self, *args ) -> Tuple[np.ndarray, np.ndarray] | None'
		Logger( ).write( exception )
		raise exception

train

train(
    X: ndarray, y: Optional[ndarray] = None
) -> object | None

Fit SelectBest.

Purpose

Fits the top-k univariate selector to feature and target arrays so the highest scoring features can be retained.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
object | None

object | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> object | None:
	"""Fit SelectBest.

	Purpose:
	    Fits the top-k univariate selector to feature and target arrays so the highest
	    scoring features can be retained.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.model.fit( X, y )
		return self
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectBest'
		exception.method = 'train( self, *args ) -> object | None'
		Logger( ).write( exception )
		raise exception

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with SelectBest.

Purpose

Projects the supplied feature matrix with the fitted SelectBest selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Project features with SelectBest.

	Purpose:
	    Projects the supplied feature matrix with the fitted SelectBest selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.prediction = self.transform( X )
		return self.prediction
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectBest'
		exception.method = 'project( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

score

score(
    X: ndarray, y: Optional[ndarray] = None
) -> pd.DataFrame

Score SelectBest.

Purpose

Evaluates classifier performance after applying the fitted SelectBest selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
DataFrame

pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
	"""Score SelectBest.

	Purpose:
	    Evaluates classifier performance after applying the fitted SelectBest selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
	                  metrics.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		if self.transformed_data is None:
			self.transformed_data = self.transform( X )
		_support = self.model.get_support( )
		_scores = self.model.scores_
		_pvalues = self.model.pvalues_ if hasattr( self.model, 'pvalues_' ) else None
		df_scores = pd.DataFrame( { 'Feature': np.arange( 0, X.shape[ 1 ] ), 'Score': _scores,
			'PValue': _pvalues if _pvalues is not None else np.full( X.shape[ 1 ], np.nan ),
			'Selected': _support, } )
		return df_scores
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectBest'
		exception.method = 'score( self, *args ) -> pd.DataFrame'
		Logger( ).write( exception )
		raise exception

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Transform features with SelectBest.

Purpose

Transforms the supplied feature matrix with the fitted SelectBest selector and stores the transformed output.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Transform features with SelectBest.

	Purpose:
	    Transforms the supplied feature matrix with the fitted SelectBest selector and
	    stores the transformed output.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectBest'
		exception.method = 'transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

train_transform

train_transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Fit and transform with SelectBest.

Purpose

Fits the SelectBest selector and returns the transformed feature matrix in one operation.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Fit and transform with SelectBest.

	Purpose:
	    Fits the SelectBest selector and returns the transformed feature matrix in one
	    operation.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced after fitting the selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.fit_transform( X, y )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectBest'
		exception.method = 'train_transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

SelectPercent

Bases: Selector

SelectPercent feature-selection class/object.

Purpose

Selects the highest scoring feature percentile with sklearn.feature_selection.SelectPercentile. The object applies univariate statistical tests, stores transformed matrices, and evaluates classifier performance after retaining the configured percentage of features.

Attributes:

Name Type Description
model Optional[object]

Underlying sklearn selector, decomposition, or cross-decomposition estimator.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

score_function Optional[object]

Univariate scoring function used to rank candidate features.

percentile Optional[int]

Percentage of top-ranked features retained by SelectPercent.

transformed_data Optional[ndarray]

Most recent feature matrix produced by projection or transformation.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

training_score Optional[float]

Estimator score calculated on the training split.

testing_score Optional[float]

Estimator score calculated on the testing split.

Source code in features.py
class SelectPercent( Selector ):
	"""SelectPercent feature-selection class/object.

	Purpose:
	    Selects the highest scoring feature percentile with
	    sklearn.feature_selection.SelectPercentile. The object applies univariate
	    statistical tests, stores transformed matrices, and evaluates classifier performance
	    after retaining the configured percentage of features.

	Attributes:
	    model (Optional[object]): Underlying sklearn selector, decomposition, or
	                              cross-decomposition estimator.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    score_function (Optional[object]): Univariate scoring function used to rank
	                                       candidate features.
	    percentile (Optional[int]): Percentage of top-ranked features retained by
	                                SelectPercent.
	    transformed_data (Optional[np.ndarray]): Most recent feature matrix produced by
	                                             projection or transformation.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation.
	    training_score (Optional[float]): Estimator score calculated on the training split.
	    testing_score (Optional[float]): Estimator score calculated on the testing split."""
	model: Optional[ sf.SelectPercentile ]
	prediction: Optional[ np.ndarray ]
	score_function: Optional[ object ]
	percentile: Optional[ int ]
	transformed_data: Optional[ np.ndarray ]
	accuracy: Optional[ float ]
	training_score: Optional[ float ]
	testing_score: Optional[ float ]

	def __init__( self, score_func: object = sf.chi2, pct: int = 10 ) -> None:
		"""Initialize SelectPercent.

		Purpose:
		    Initializes the percentile-based univariate selector with its scoring function,
		    retained percentile, wrapped sklearn estimator, transformed-data storage, and score
		    fields.

		Args:
		    score_func (object): Univariate score function used to rank candidate features.
		    pct (int): Percentage of top-ranked features retained by SelectPercent.

		Returns:
		    None: This method initializes the object and does not return a value."""
		super( ).__init__( )
		self.score_function = score_func
		self.percentile = pct
		self.model = sf.SelectPercentile( score_func=self.score_function,
			percentile=self.percentile )
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None
		self.training_score = None
		self.testing_score = None

	def __dir__( self ) -> List[ str ]:
		"""List public members.

		Purpose:
		    Returns the stable list of public members exposed by the SelectPercent class/object for
		    interactive inspection, notebook exploration, and IDE discovery.

		Returns:
		    List[str]: Public member names exposed by the class/object."""
		return [ 'model', 'prediction', 'score_function', 'percentile', 'transformed_data',
			'split_data', 'chi_square', 'train', 'project', 'score', 'transform',
			'train_transform' ]

	def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
			random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
		    size (float): Testing-set proportion used by train/test splitting.
		    random (int): Random seed used by train/test splitting.

		Returns:
		    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
		                                                           features, testing features,
		                                                           training targets, and testing
		                                                           targets.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
			return (X_train, X_test, y_train, y_test)
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectPercent'
			exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
			Logger( ).write( exception )
			raise exception

	def chi_square( self, X: np.ndarray, y: np.ndarray ) -> Tuple[ np.ndarray, np.ndarray ] | None:
		"""Calculate chi-square scores.

		Purpose:
		    Computes chi-square feature scores and p-values for the supplied feature matrix and
		    target vector using the SelectPercent scoring configuration.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    Tuple[np.ndarray, np.ndarray] | None: Tuple containing chi-square scores and
		                                          p-values.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			return sf.chi2( X, y )
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectPercent'
			exception.method = 'chi_square( self, *args ) -> Tuple[np.ndarray, np.ndarray] | None'
			Logger( ).write( exception )
			raise exception

	def train( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> object | None:
		"""Fit SelectPercent.

		Purpose:
		    Fits the percentile-based univariate selector to feature and target arrays so the
		    highest scoring percentage of features can be retained.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.model.fit( X, y )
			return self
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectPercent'
			exception.method = 'train( self, *args ) -> object | None'
			Logger( ).write( exception )
			raise exception

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Project features with SelectPercent.

		Purpose:
		    Projects the supplied feature matrix with the fitted SelectPercent selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.prediction = self.transform( X )
			return self.prediction
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectPercent'
			exception.method = 'project( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
		"""Score SelectPercent.

		Purpose:
		    Evaluates classifier performance after applying the fitted SelectPercent selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
		                  metrics.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			if self.transformed_data is None:
				self.transformed_data = self.transform( X )
			_support = self.model.get_support( )
			_scores = self.model.scores_
			_pvalues = self.model.pvalues_ if hasattr( self.model, 'pvalues_' ) else None
			df_scores = pd.DataFrame( { 'Feature': np.arange( 0, X.shape[ 1 ] ), 'Score': _scores,
				'PValue': _pvalues if _pvalues is not None else np.full( X.shape[ 1 ], np.nan ),
				'Selected': _support, } )
			return df_scores
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectPercent'
			exception.method = 'score( self, *args ) -> pd.DataFrame'
			Logger( ).write( exception )
			raise exception

	def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Transform features with SelectPercent.

		Purpose:
		    Transforms the supplied feature matrix with the fitted SelectPercent selector and
		    stores the transformed output.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectPercent'
			exception.method = 'transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Fit and transform with SelectPercent.

		Purpose:
		    Fits the SelectPercent selector and returns the transformed feature matrix in one
		    operation.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced after fitting the selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.fit_transform( X, y )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SelectPercent'
			exception.method = 'train_transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

split_data

split_data(
    X: ndarray,
    y: ndarray,
    size: float = 0.2,
    random: int = 42,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required
size float

Testing-set proportion used by train/test splitting.

0.2
random int

Random seed used by train/test splitting.

42

Returns:

Type Description
Tuple[ndarray, ndarray, ndarray, ndarray]

Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
		random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
	    size (float): Testing-set proportion used by train/test splitting.
	    random (int): Random seed used by train/test splitting.

	Returns:
	    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
	                                                           features, testing features,
	                                                           training targets, and testing
	                                                           targets.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y, test_size=size, random_state=random )
		return (X_train, X_test, y_train, y_test)
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectPercent'
		exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
		Logger( ).write( exception )
		raise exception

chi_square

chi_square(
    X: ndarray, y: ndarray
) -> Tuple[np.ndarray, np.ndarray] | None

Calculate chi-square scores.

Purpose

Computes chi-square feature scores and p-values for the supplied feature matrix and target vector using the SelectPercent scoring configuration.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
Tuple[ndarray, ndarray] | None

Tuple[np.ndarray, np.ndarray] | None: Tuple containing chi-square scores and p-values.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def chi_square( self, X: np.ndarray, y: np.ndarray ) -> Tuple[ np.ndarray, np.ndarray ] | None:
	"""Calculate chi-square scores.

	Purpose:
	    Computes chi-square feature scores and p-values for the supplied feature matrix and
	    target vector using the SelectPercent scoring configuration.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    Tuple[np.ndarray, np.ndarray] | None: Tuple containing chi-square scores and
	                                          p-values.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		return sf.chi2( X, y )
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectPercent'
		exception.method = 'chi_square( self, *args ) -> Tuple[np.ndarray, np.ndarray] | None'
		Logger( ).write( exception )
		raise exception

train

train(
    X: ndarray, y: Optional[ndarray] = None
) -> object | None

Fit SelectPercent.

Purpose

Fits the percentile-based univariate selector to feature and target arrays so the highest scoring percentage of features can be retained.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
object | None

object | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> object | None:
	"""Fit SelectPercent.

	Purpose:
	    Fits the percentile-based univariate selector to feature and target arrays so the
	    highest scoring percentage of features can be retained.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.model.fit( X, y )
		return self
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectPercent'
		exception.method = 'train( self, *args ) -> object | None'
		Logger( ).write( exception )
		raise exception

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with SelectPercent.

Purpose

Projects the supplied feature matrix with the fitted SelectPercent selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Project features with SelectPercent.

	Purpose:
	    Projects the supplied feature matrix with the fitted SelectPercent selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.prediction = self.transform( X )
		return self.prediction
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectPercent'
		exception.method = 'project( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

score

score(
    X: ndarray, y: Optional[ndarray] = None
) -> pd.DataFrame

Score SelectPercent.

Purpose

Evaluates classifier performance after applying the fitted SelectPercent selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
DataFrame

pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
	"""Score SelectPercent.

	Purpose:
	    Evaluates classifier performance after applying the fitted SelectPercent selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
	                  metrics.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		if self.transformed_data is None:
			self.transformed_data = self.transform( X )
		_support = self.model.get_support( )
		_scores = self.model.scores_
		_pvalues = self.model.pvalues_ if hasattr( self.model, 'pvalues_' ) else None
		df_scores = pd.DataFrame( { 'Feature': np.arange( 0, X.shape[ 1 ] ), 'Score': _scores,
			'PValue': _pvalues if _pvalues is not None else np.full( X.shape[ 1 ], np.nan ),
			'Selected': _support, } )
		return df_scores
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectPercent'
		exception.method = 'score( self, *args ) -> pd.DataFrame'
		Logger( ).write( exception )
		raise exception

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Transform features with SelectPercent.

Purpose

Transforms the supplied feature matrix with the fitted SelectPercent selector and stores the transformed output.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Transform features with SelectPercent.

	Purpose:
	    Transforms the supplied feature matrix with the fitted SelectPercent selector and
	    stores the transformed output.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectPercent'
		exception.method = 'transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

train_transform

train_transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Fit and transform with SelectPercent.

Purpose

Fits the SelectPercent selector and returns the transformed feature matrix in one operation.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train_transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Fit and transform with SelectPercent.

	Purpose:
	    Fits the SelectPercent selector and returns the transformed feature matrix in one
	    operation.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced after fitting the selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.fit_transform( X, y )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SelectPercent'
		exception.method = 'train_transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

SBS

Bases: Selector

SBS feature-selection class.

Purpose

Implements sequential backward selection with a cloned classifier and scoring function. The class iteratively removes features, tracks candidate subsets, records subset scores, and preserves the selected feature indices for projection and evaluation.

Attributes:

Name Type Description
scoring Optional[callable]

Callable used to score classifier predictions during sequential selection.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

classifier Optional[Classifier]

Classifier instance cloned or fitted by the feature selector.

random_state Optional[int]

Random seed used for reproducible train/test splitting.

test_size Optional[float]

Testing-set proportion used during feature-selection evaluation.

k_features Optional[int]

Target number of features retained by sequential backward selection.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

indices_ Optional[Tuple[int, ...]]

Selected feature-index tuple produced by the selector.

subsets_ Optional[List[Tuple[int, ...]]]

Feature-index subsets evaluated during sequential selection.

scores_ Optional[List[float]]

Scores recorded for evaluated feature subsets.

k_score_ Optional[float]

Best score associated with the selected feature subset.

Source code in features.py
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
class SBS( Selector ):
	"""SBS feature-selection class.

	Purpose:
	    Implements sequential backward selection with a cloned classifier and scoring
	    function. The class iteratively removes features, tracks candidate subsets,
	    records subset scores, and preserves the selected feature indices for projection and
	    evaluation.

	Attributes:
	    scoring (Optional[callable]): Callable used to score classifier predictions during
	                                  sequential selection.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    classifier (Optional[Classifier]): Classifier instance cloned or fitted by the
	                                       feature selector.
	    random_state (Optional[int]): Random seed used for reproducible train/test
	                                  splitting.
	    test_size (Optional[float]): Testing-set proportion used during feature-selection
	                                 evaluation.
	    k_features (Optional[int]): Target number of features retained by sequential
	                                backward selection.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation.
	    indices_ (Optional[Tuple[int, ...]]): Selected feature-index tuple produced by the
	                                          selector.
	    subsets_ (Optional[List[Tuple[int, ...]]]): Feature-index subsets evaluated during
	                                                sequential selection.
	    scores_ (Optional[List[float]]): Scores recorded for evaluated feature subsets.
	    k_score_ (Optional[float]): Best score associated with the selected feature subset."""
	scoring: Optional[ callable ]
	prediction: Optional[ np.ndarray ]
	classifier: Optional[ Classifier ]
	random_state: Optional[ int ]
	test_size: Optional[ float ]
	k_features: Optional[ int ]
	accuracy: Optional[ float ]
	indices_: Optional[ Tuple[ int, ... ] ]
	subsets_: Optional[ List[ Tuple[ int, ... ] ] ]
	scores_: Optional[ List[ float ] ]
	k_score_: Optional[ float ]

	def __init__( self, classifier: Classifier, k_features: int, scoring: callable=accuracy_score,
			test_size: float=0.25, random_state: int=1 ) -> None:
		"""Initialize SBS.

		Purpose:
		    Initializes sequential backward selection with a classifier, target feature count,
		    scoring callable, split configuration, and tracking fields for selected subsets and
		    scores.

		Args:
		    classifier (Classifier): Classifier used for feature-selection evaluation.
		    k_features (int): Target number of features retained by sequential backward
		                      selection.
		    scoring (callable): Callable used to evaluate candidate feature subsets.
		    test_size (float): Testing-set proportion used during selector evaluation.
		    random_state (int): Random seed used during selector evaluation.

		Returns:
		    None: This method initializes the object and does not return a value."""
		super( ).__init__( )
		self.scoring = scoring
		self.classifier = clone( classifier )
		self.k_features = k_features
		self.test_size = test_size
		self.random_state = random_state
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None
		self.indices_ = None
		self.subsets_ = None
		self.scores_ = None
		self.k_score_ = None

	def __dir__( self ) -> List[ str ]:
		"""List public members.

		Purpose:
		    Returns the stable list of public members exposed by the SBS class/object for interactive
		    inspection, notebook exploration, and IDE discovery.

		Returns:
		    List[str]: Public member names exposed by the class/object."""
		return [ 'scoring', 'classifier', 'k_features', 'test_size', 'random_state', 'prediction',
			'transformed_data', 'indices_', 'subsets_', 'scores_', 'k_score_', 'split_data',
			'train', 'project', 'score', 'transform', 'train_transform', 'calc_score' ]

	def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
			random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
		    size (float): Testing-set proportion used by train/test splitting.
		    random (int): Random seed used by train/test splitting.

		Returns:
		    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
		                                                           features, testing features,
		                                                           training targets, and testing
		                                                           targets.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y, test_size=size,
				random_state=random )
			return (X_train, X_test, y_train, y_test)
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SBS'
			exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
			Logger( ).write( exception )
			raise exception

	def train( self, X: np.ndarray, y: np.ndarray ) -> object | None:
		"""Fit SBS.

		Purpose:
		    Runs sequential backward selection by repeatedly evaluating feature subsets and
		    removing features until the target subset size is reached.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y,
				test_size=self.test_size,
				random_state=self.random_state )
			dim = X_train.shape[ 1 ]
			self.indices_ = tuple( range( dim ) )
			self.subsets_ = [ self.indices_ ]
			score = self.calc_score( X_train, y_train, X_test, y_test, self.indices_ )
			self.scores_ = [ score ]
			while dim > self.k_features:
				scores = [ ]
				subsets = [ ]

				for p in combinations( self.indices_, r=dim - 1 ):
					score = self.calc_score( X_train, y_train, X_test, y_test, p )
					scores.append( score )
					subsets.append( p )

				best = int( np.argmax( scores ) )
				self.indices_ = subsets[ best ]
				self.subsets_.append( self.indices_ )
				dim -= 1
				self.scores_.append( scores[ best ] )

			self.k_score_ = self.scores_[ -1 ]
			return self
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SBS'
			exception.method = 'train( self, *args ) -> object | None'
			Logger( ).write( exception )
			raise exception

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Project features with SBS.

		Purpose:
		    Projects the supplied feature matrix onto the feature indices selected by sequential
		    backward selection.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.prediction = self.transform( X )
			return self.prediction
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SBS'
			exception.method = 'project( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
		"""Score SBS.

		Purpose:
		    Evaluates classifier performance after applying the fitted SBS selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
		                  metrics.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			_original_count = int( X.shape[ 1 ] )
			_retained_count = len( self.indices_ ) if self.indices_ is not None else 0
			_metrics = { 'Original Features': [ _original_count ],
				'Retained Features': [ _retained_count ],
				'Removed Features': [ _original_count - _retained_count ],
				'Best Score': [ float( self.k_score_ ) if self.k_score_ is not None else np.nan ],
				'Iterations': [ len( self.subsets_ ) if self.subsets_ is not None else 0 ], }
			return pd.DataFrame( _metrics )
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SBS'
			exception.method = 'score( self, *args ) -> pd.DataFrame'
			Logger( ).write( exception )
			raise exception

	def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Transform features with SBS.

		Purpose:
		    Transforms the supplied feature matrix with the fitted SBS selector and stores the
		    transformed output.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation.
		    ValueError: Raised when the `transform` operation cannot complete."""
		try:
			throw_if( 'X', X )
			if self.indices_ is None:
				raise ValueError( 'The SBS class/object must be trained before calling transform.' )
			self.transformed_data = X[ :, self.indices_ ]
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SBS'
			exception.method = 'transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def train_transform( self, X: np.ndarray, y: np.ndarray ) -> np.ndarray:
		"""Fit and transform with SBS.

		Purpose:
		    Fits sequential backward selection and returns the feature matrix projected onto the
		    selected subset.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced after fitting the selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			self.train( X, y )
			self.transformed_data = self.transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SBS'
			exception.method = 'train_transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def calc_score( self, X_train: np.ndarray, y_train: np.ndarray,
			X_test: np.ndarray, y_test: np.ndarray, indices: Tuple[ int, ... ] ) -> float:
		"""Calculate subset score.

		Purpose:
		    Fits a cloned classifier on the selected training columns and returns prediction
		    accuracy on the matching testing columns.

		Args:
		    X_train (np.ndarray): Training feature matrix used to fit the classifier.
		    y_train (np.ndarray): Training target vector used to fit the classifier.
		    X_test (np.ndarray): Testing feature matrix used to generate predictions.
		    y_test (np.ndarray): Testing target vector used to evaluate predictions.
		    indices (Tuple[int, ...]): Feature-index subset evaluated by the scoring routine.

		Returns:
		    float: Accuracy score calculated for the supplied feature-index subset.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X_train', X_train )
			throw_if( 'y_train', y_train )
			throw_if( 'X_test', X_test )
			throw_if( 'y_test', y_test )
			throw_if( 'indices', indices )
			self.classifier.fit( X_train[ :, indices ], y_train )
			y_pred = self.classifier.predict( X_test[ :, indices ] )
			score = self.scoring( y_test, y_pred )
			return score
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'SBS'
			exception.method = 'calc_score( self, *args ) -> float'
			Logger( ).write( exception )
			raise exception

split_data

split_data(
    X: ndarray,
    y: ndarray,
    size: float = 0.2,
    random: int = 42,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required
size float

Testing-set proportion used by train/test splitting.

0.2
random int

Random seed used by train/test splitting.

42

Returns:

Type Description
Tuple[ndarray, ndarray, ndarray, ndarray]

Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray, size: float=0.2,
		random: int=42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
	    size (float): Testing-set proportion used by train/test splitting.
	    random (int): Random seed used by train/test splitting.

	Returns:
	    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
	                                                           features, testing features,
	                                                           training targets, and testing
	                                                           targets.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y, test_size=size,
			random_state=random )
		return (X_train, X_test, y_train, y_test)
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SBS'
		exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
		Logger( ).write( exception )
		raise exception

train

train(X: ndarray, y: ndarray) -> object | None

Fit SBS.

Purpose

Runs sequential backward selection by repeatedly evaluating feature subsets and removing features until the target subset size is reached.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
object | None

object | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train( self, X: np.ndarray, y: np.ndarray ) -> object | None:
	"""Fit SBS.

	Purpose:
	    Runs sequential backward selection by repeatedly evaluating feature subsets and
	    removing features until the target subset size is reached.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y,
			test_size=self.test_size,
			random_state=self.random_state )
		dim = X_train.shape[ 1 ]
		self.indices_ = tuple( range( dim ) )
		self.subsets_ = [ self.indices_ ]
		score = self.calc_score( X_train, y_train, X_test, y_test, self.indices_ )
		self.scores_ = [ score ]
		while dim > self.k_features:
			scores = [ ]
			subsets = [ ]

			for p in combinations( self.indices_, r=dim - 1 ):
				score = self.calc_score( X_train, y_train, X_test, y_test, p )
				scores.append( score )
				subsets.append( p )

			best = int( np.argmax( scores ) )
			self.indices_ = subsets[ best ]
			self.subsets_.append( self.indices_ )
			dim -= 1
			self.scores_.append( scores[ best ] )

		self.k_score_ = self.scores_[ -1 ]
		return self
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SBS'
		exception.method = 'train( self, *args ) -> object | None'
		Logger( ).write( exception )
		raise exception

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with SBS.

Purpose

Projects the supplied feature matrix onto the feature indices selected by sequential backward selection.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Project features with SBS.

	Purpose:
	    Projects the supplied feature matrix onto the feature indices selected by sequential
	    backward selection.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.prediction = self.transform( X )
		return self.prediction
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SBS'
		exception.method = 'project( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

score

score(
    X: ndarray, y: Optional[ndarray] = None
) -> pd.DataFrame

Score SBS.

Purpose

Evaluates classifier performance after applying the fitted SBS selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
DataFrame

pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
	"""Score SBS.

	Purpose:
	    Evaluates classifier performance after applying the fitted SBS selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
	                  metrics.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		_original_count = int( X.shape[ 1 ] )
		_retained_count = len( self.indices_ ) if self.indices_ is not None else 0
		_metrics = { 'Original Features': [ _original_count ],
			'Retained Features': [ _retained_count ],
			'Removed Features': [ _original_count - _retained_count ],
			'Best Score': [ float( self.k_score_ ) if self.k_score_ is not None else np.nan ],
			'Iterations': [ len( self.subsets_ ) if self.subsets_ is not None else 0 ], }
		return pd.DataFrame( _metrics )
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SBS'
		exception.method = 'score( self, *args ) -> pd.DataFrame'
		Logger( ).write( exception )
		raise exception

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Transform features with SBS.

Purpose

Transforms the supplied feature matrix with the fitted SBS selector and stores the transformed output.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

ValueError

Raised when the transform operation cannot complete.

Source code in features.py
def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Transform features with SBS.

	Purpose:
	    Transforms the supplied feature matrix with the fitted SBS selector and stores the
	    transformed output.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation.
	    ValueError: Raised when the `transform` operation cannot complete."""
	try:
		throw_if( 'X', X )
		if self.indices_ is None:
			raise ValueError( 'The SBS class/object must be trained before calling transform.' )
		self.transformed_data = X[ :, self.indices_ ]
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SBS'
		exception.method = 'transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

train_transform

train_transform(X: ndarray, y: ndarray) -> np.ndarray

Fit and transform with SBS.

Purpose

Fits sequential backward selection and returns the feature matrix projected onto the selected subset.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train_transform( self, X: np.ndarray, y: np.ndarray ) -> np.ndarray:
	"""Fit and transform with SBS.

	Purpose:
	    Fits sequential backward selection and returns the feature matrix projected onto the
	    selected subset.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced after fitting the selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		self.train( X, y )
		self.transformed_data = self.transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SBS'
		exception.method = 'train_transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

calc_score

calc_score(
    X_train: ndarray,
    y_train: ndarray,
    X_test: ndarray,
    y_test: ndarray,
    indices: Tuple[int, ...],
) -> float

Calculate subset score.

Purpose

Fits a cloned classifier on the selected training columns and returns prediction accuracy on the matching testing columns.

Parameters:

Name Type Description Default
X_train ndarray

Training feature matrix used to fit the classifier.

required
y_train ndarray

Training target vector used to fit the classifier.

required
X_test ndarray

Testing feature matrix used to generate predictions.

required
y_test ndarray

Testing target vector used to evaluate predictions.

required
indices Tuple[int, ...]

Feature-index subset evaluated by the scoring routine.

required

Returns:

Name Type Description
float float

Accuracy score calculated for the supplied feature-index subset.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def calc_score( self, X_train: np.ndarray, y_train: np.ndarray,
		X_test: np.ndarray, y_test: np.ndarray, indices: Tuple[ int, ... ] ) -> float:
	"""Calculate subset score.

	Purpose:
	    Fits a cloned classifier on the selected training columns and returns prediction
	    accuracy on the matching testing columns.

	Args:
	    X_train (np.ndarray): Training feature matrix used to fit the classifier.
	    y_train (np.ndarray): Training target vector used to fit the classifier.
	    X_test (np.ndarray): Testing feature matrix used to generate predictions.
	    y_test (np.ndarray): Testing target vector used to evaluate predictions.
	    indices (Tuple[int, ...]): Feature-index subset evaluated by the scoring routine.

	Returns:
	    float: Accuracy score calculated for the supplied feature-index subset.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X_train', X_train )
		throw_if( 'y_train', y_train )
		throw_if( 'X_test', X_test )
		throw_if( 'y_test', y_test )
		throw_if( 'indices', indices )
		self.classifier.fit( X_train[ :, indices ], y_train )
		y_pred = self.classifier.predict( X_test[ :, indices ] )
		score = self.scoring( y_test, y_pred )
		return score
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'SBS'
		exception.method = 'calc_score( self, *args ) -> float'
		Logger( ).write( exception )
		raise exception

RFE

Bases: Selector

RFE feature-selection class/object.

Purpose

Performs recursive feature elimination with an sklearn-compatible estimator. The class recursively prunes low-importance features, stores feature rankings, supports projection into the selected feature set, and evaluates downstream classifier performance.

Attributes:

Name Type Description
model Optional[RFE]

Underlying sklearn selector, decomposition, or cross-decomposition estimator.

prediction Optional[ndarray]

Most recent classifier predictions generated during scoring.

classifier Optional[NearestNeighbor]

Classifier instance cloned or fitted by the feature selector.

transformed_data Optional[ndarray]

Most recent feature matrix produced by projection or transformation.

n_features_to_select Optional[int]

Number of features retained by recursive feature elimination.

verbose Optional[int]

Verbosity level passed to the recursive feature elimination estimator.

accuracy Optional[float]

Most recent accuracy score produced by selector evaluation.

training_score Optional[float]

Estimator score calculated on the training split.

testing_score Optional[float]

Estimator score calculated on the testing split.

Source code in features.py
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
class RFE( Selector ):
	"""RFE feature-selection class/object.

	Purpose:
	    Performs recursive feature elimination with an sklearn-compatible estimator. The
	    class recursively prunes low-importance features, stores feature rankings,
	    supports projection into the selected feature set, and evaluates downstream
	    classifier performance.

	Attributes:
	    model (Optional[sf.RFE]): Underlying sklearn selector, decomposition, or
	                              cross-decomposition estimator.
	    prediction (Optional[np.ndarray]): Most recent classifier predictions generated
	                                       during scoring.
	    classifier (Optional[NearestNeighbor]): Classifier instance cloned or fitted by the
	                                            feature selector.
	    transformed_data (Optional[np.ndarray]): Most recent feature matrix produced by
	                                             projection or transformation.
	    n_features_to_select (Optional[int]): Number of features retained by recursive
	                                          feature elimination.
	    verbose (Optional[int]): Verbosity level passed to the recursive feature elimination
	                             estimator.
	    accuracy (Optional[float]): Most recent accuracy score produced by selector
	                                evaluation.
	    training_score (Optional[float]): Estimator score calculated on the training split.
	    testing_score (Optional[float]): Estimator score calculated on the testing split."""
	model: Optional[ sf.RFE ]
	prediction: Optional[ np.ndarray ]
	classifier: Optional[ NearestNeighbor ]
	transformed_data: Optional[ np.ndarray ]
	n_features_to_select: Optional[ int ]
	verbose: Optional[ int ]
	accuracy: Optional[ float ]
	training_score: Optional[ float ]
	testing_score: Optional[ float ]

	def __init__( self, k_features: int = None, verbose: int = 0 ) -> None:
		"""Initialize RFE.

		Purpose:
		    Initializes recursive feature elimination with a nearest-neighbor classifier,
		    retained feature count, verbosity configuration, wrapped sklearn RFE estimator,
		    transformed-data storage, and score fields.

		Args:
		    k_features (int): Target number of features retained by sequential backward
		                      selection.
		    verbose (int): Verbosity level passed to recursive feature elimination.

		Returns:
		    None: This method initializes the object and does not return a value."""
		super( ).__init__( )
		self.n_features_to_select = k_features
		self.classifier = NearestNeighbor( )
		self.verbose = verbose
		self.model = sf.RFE( estimator=self.classifier,
			n_features_to_select=self.n_features_to_select,
			verbose=self.verbose )
		self.prediction = None
		self.transformed_data = None
		self.accuracy = None
		self.training_score = None
		self.testing_score = None

	def __dir__( self ) -> List[ str ]:
		"""List public members.

		Purpose:
		    Returns the stable list of public members exposed by the RFE class/object for interactive
		    inspection, notebook exploration, and IDE discovery.

		Returns:
		    List[str]: Public member names exposed by the class/object."""
		return [ 'classifier', 'n_features_to_select', 'verbose', 'prediction', 'transformed_data',
			'features_in', 'ranking', 'split_data', 'train', 'project', 'score', 'transform',
			'train_transform' ]

	@property
	def features_in( self ) -> int:
		"""Return fitted feature count.

		Purpose:
		    Returns the number of input features observed by the fitted recursive feature
		    elimination estimator.

		Returns:
		    int: Number of input features observed by the fitted RFE estimator.

		Raises:
		    AttributeError: Raised when required fitted estimator metadata is unavailable or
		                    when the base interface is called directly."""
		if not hasattr( self.model, 'n_features_in_' ):
			raise AttributeError( 'The model data has not been trained!' )
		return self.model.n_features_in_

	@property
	def ranking( self ) -> np.ndarray:
		"""Return feature rankings.

		Purpose:
		    Returns the feature-ranking array produced by the fitted recursive feature
		    elimination estimator.

		Returns:
		    np.ndarray: Feature ranking array produced by recursive feature elimination.

		Raises:
		    AttributeError: Raised when required fitted estimator metadata is unavailable or
		                    when the base interface is called directly."""
		if not hasattr( self.model, 'ranking_' ):
			raise AttributeError( 'The model data has not been trained!' )
		return self.model.ranking_

	def split_data( self, X: np.ndarray, y: np.ndarray, size: float = 0.2,
			random: int = 42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
		"""Split training and testing data.

		Purpose:
		    Splits feature and target arrays into aligned training and testing partitions using
		    the configured random seed and test-size proportion.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
		    size (float): Testing-set proportion used by train/test splitting.
		    random (int): Random seed used by train/test splitting.

		Returns:
		    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
		                                                           features, testing features,
		                                                           training targets, and testing
		                                                           targets.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			X_train, X_test, y_train, y_test = split( X, y, test_size=size,
				random_state=random )
			return (X_train, X_test, y_train, y_test)
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'RFE'
			exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
			Logger( ).write( exception )
			raise exception

	def train( self, X: np.ndarray, y: np.ndarray ) -> object | None:
		"""Fit RFE.

		Purpose:
		    Fits recursive feature elimination to feature and target arrays so selected features
		    and rankings can be learned.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    object | None: Fitted selector class/object or fitted estimator result.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			self.model.fit( X, y )
			return self
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'RFE'
			exception.method = 'train( self, *args ) -> object | None'
			Logger( ).write( exception )
			raise exception

	def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Project features with RFE.

		Purpose:
		    Projects the supplied feature matrix with the fitted RFE selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.prediction = self.transform( X )
			return self.prediction
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'RFE'
			exception.method = 'project( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
		"""Score RFE.

		Purpose:
		    Evaluates classifier performance after applying the fitted RFE selector.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
		                  metrics.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			_support = self.model.get_support( )
			df_scores = pd.DataFrame(
				{ 'Feature': np.arange( 0, X.shape[ 1 ] ), 'Ranking': self.model.ranking_,
					'Selected': _support, } )
			return df_scores
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'RFE'
			exception.method = 'score( self, *args ) -> pd.DataFrame'
			Logger( ).write( exception )
			raise exception

	def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
		"""Transform features with RFE.

		Purpose:
		    Transforms the supplied feature matrix with the fitted RFE selector and stores the
		    transformed output.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced by the fitted selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			self.transformed_data = self.model.transform( X )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'RFE'
			exception.method = 'transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

	def train_transform( self, X: np.ndarray, y: np.ndarray ) -> np.ndarray:
		"""Fit and transform with RFE.

		Purpose:
		    Fits the RFE selector and returns the transformed feature matrix in one operation.

		Args:
		    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
		                    scoring routine.
		    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

		Returns:
		    np.ndarray: Transformed feature matrix produced after fitting the selector.

		Raises:
		    Error: Raised when validation, fitting, transformation, projection, or scoring fails
		           inside the wrapped selector operation."""
		try:
			throw_if( 'X', X )
			throw_if( 'y', y )
			self.transformed_data = self.model.fit_transform( X, y )
			return self.transformed_data
		except Exception as e:
			exception = Error( e )
			exception.module = 'mathy'
			exception.cause = 'RFE'
			exception.method = 'train_transform( self, *args ) -> np.ndarray'
			Logger( ).write( exception )
			raise exception

features_in property

features_in: int

Return fitted feature count.

Purpose

Returns the number of input features observed by the fitted recursive feature elimination estimator.

Returns:

Name Type Description
int int

Number of input features observed by the fitted RFE estimator.

Raises:

Type Description
AttributeError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

ranking property

ranking: ndarray

Return feature rankings.

Purpose

Returns the feature-ranking array produced by the fitted recursive feature elimination estimator.

Returns:

Type Description
ndarray

np.ndarray: Feature ranking array produced by recursive feature elimination.

Raises:

Type Description
AttributeError

Raised when required fitted estimator metadata is unavailable or when the base interface is called directly.

split_data

split_data(
    X: ndarray,
    y: ndarray,
    size: float = 0.2,
    random: int = 42,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

Split training and testing data.

Purpose

Splits feature and target arrays into aligned training and testing partitions using the configured random seed and test-size proportion.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required
size float

Testing-set proportion used by train/test splitting.

0.2
random int

Random seed used by train/test splitting.

42

Returns:

Type Description
Tuple[ndarray, ndarray, ndarray, ndarray]

Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training features, testing features, training targets, and testing targets.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def split_data( self, X: np.ndarray, y: np.ndarray, size: float = 0.2,
		random: int = 42 ) -> Tuple[ np.ndarray, np.ndarray, np.ndarray, np.ndarray ]:
	"""Split training and testing data.

	Purpose:
	    Splits feature and target arrays into aligned training and testing partitions using
	    the configured random seed and test-size proportion.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.
	    size (float): Testing-set proportion used by train/test splitting.
	    random (int): Random seed used by train/test splitting.

	Returns:
	    Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: Tuple containing training
	                                                           features, testing features,
	                                                           training targets, and testing
	                                                           targets.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		X_train, X_test, y_train, y_test = split( X, y, test_size=size,
			random_state=random )
		return (X_train, X_test, y_train, y_test)
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'RFE'
		exception.method = 'split_data( self, *args ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]'
		Logger( ).write( exception )
		raise exception

train

train(X: ndarray, y: ndarray) -> object | None

Fit RFE.

Purpose

Fits recursive feature elimination to feature and target arrays so selected features and rankings can be learned.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
object | None

object | None: Fitted selector class/object or fitted estimator result.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train( self, X: np.ndarray, y: np.ndarray ) -> object | None:
	"""Fit RFE.

	Purpose:
	    Fits recursive feature elimination to feature and target arrays so selected features
	    and rankings can be learned.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    object | None: Fitted selector class/object or fitted estimator result.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		self.model.fit( X, y )
		return self
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'RFE'
		exception.method = 'train( self, *args ) -> object | None'
		Logger( ).write( exception )
		raise exception

project

project(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Project features with RFE.

Purpose

Projects the supplied feature matrix with the fitted RFE selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Projected or selected feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def project( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Project features with RFE.

	Purpose:
	    Projects the supplied feature matrix with the fitted RFE selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Projected or selected feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.prediction = self.transform( X )
		return self.prediction
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'RFE'
		exception.method = 'project( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

score

score(
    X: ndarray, y: Optional[ndarray] = None
) -> pd.DataFrame

Score RFE.

Purpose

Evaluates classifier performance after applying the fitted RFE selector.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
DataFrame

pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy metrics.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def score( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> pd.DataFrame:
	"""Score RFE.

	Purpose:
	    Evaluates classifier performance after applying the fitted RFE selector.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    pd.DataFrame: Dataframe or dictionary containing training, testing, and accuracy
	                  metrics.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		_support = self.model.get_support( )
		df_scores = pd.DataFrame(
			{ 'Feature': np.arange( 0, X.shape[ 1 ] ), 'Ranking': self.model.ranking_,
				'Selected': _support, } )
		return df_scores
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'RFE'
		exception.method = 'score( self, *args ) -> pd.DataFrame'
		Logger( ).write( exception )
		raise exception

transform

transform(
    X: ndarray, y: Optional[ndarray] = None
) -> np.ndarray

Transform features with RFE.

Purpose

Transforms the supplied feature matrix with the fitted RFE selector and stores the transformed output.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y Optional[ndarray]

Target vector or target matrix aligned to the rows of X.

None

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced by the fitted selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def transform( self, X: np.ndarray, y: Optional[ np.ndarray ]=None ) -> np.ndarray:
	"""Transform features with RFE.

	Purpose:
	    Transforms the supplied feature matrix with the fitted RFE selector and stores the
	    transformed output.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (Optional[np.ndarray]): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced by the fitted selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		self.transformed_data = self.model.transform( X )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'RFE'
		exception.method = 'transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

train_transform

train_transform(X: ndarray, y: ndarray) -> np.ndarray

Fit and transform with RFE.

Purpose

Fits the RFE selector and returns the transformed feature matrix in one operation.

Parameters:

Name Type Description Default
X ndarray

Feature matrix supplied to the selector, projector, estimator, or scoring routine.

required
y ndarray

Target vector or target matrix aligned to the rows of X.

required

Returns:

Type Description
ndarray

np.ndarray: Transformed feature matrix produced after fitting the selector.

Raises:

Type Description
Error

Raised when validation, fitting, transformation, projection, or scoring fails inside the wrapped selector operation.

Source code in features.py
def train_transform( self, X: np.ndarray, y: np.ndarray ) -> np.ndarray:
	"""Fit and transform with RFE.

	Purpose:
	    Fits the RFE selector and returns the transformed feature matrix in one operation.

	Args:
	    X (np.ndarray): Feature matrix supplied to the selector, projector, estimator, or
	                    scoring routine.
	    y (np.ndarray): Target vector or target matrix aligned to the rows of `X`.

	Returns:
	    np.ndarray: Transformed feature matrix produced after fitting the selector.

	Raises:
	    Error: Raised when validation, fitting, transformation, projection, or scoring fails
	           inside the wrapped selector operation."""
	try:
		throw_if( 'X', X )
		throw_if( 'y', y )
		self.transformed_data = self.model.fit_transform( X, y )
		return self.transformed_data
	except Exception as e:
		exception = Error( e )
		exception.module = 'mathy'
		exception.cause = 'RFE'
		exception.method = 'train_transform( self, *args ) -> np.ndarray'
		Logger( ).write( exception )
		raise exception

throw_if

throw_if(name: str, value: object) -> None

Validate a required clustering argument.

Purpose

Enforces the presence of required clustering inputs before estimator execution. The validation accepts populated NumPy arrays and standard Python containers while rejecting null values and empty collections that would otherwise cause downstream sklearn operations to fail or produce undefined clustering results.

Parameters:

Name Type Description Default
name str

Argument name used in the validation error message.

required
value object

Argument value checked for a null or empty state.

required

Returns:

Name Type Description
None None

This function performs its work through side effects and does not return a value.

Raises:

Type Description
ValueError

Raised when value is None or empty.

Source code in features.py
def throw_if( name: str, value: object ) -> None:
	"""Validate a required clustering argument.

	Purpose:
	    Enforces the presence of required clustering inputs before estimator execution. The
	    validation accepts populated NumPy arrays and standard Python containers while
	    rejecting null values and empty collections that would otherwise cause downstream
	    sklearn operations to fail or produce undefined clustering results.

	Args:
	    name (str): Argument name used in the validation error message.
	    value (object): Argument value checked for a null or empty state.

	Returns:
	    None: This function performs its work through side effects and does not return a
	          value.

	Raises:
	    ValueError: Raised when `value` is None or empty."""
	if value is None:
		raise ValueError( f'Argument "{name}" cannot be empty!' )

	if isinstance( value, np.ndarray ) and value.size == 0:
		raise ValueError( f'Argument "{name}" cannot be empty!' )

	if isinstance( value, (str, list, tuple, dict, set) ) and len( value ) == 0:
		raise ValueError( f'Argument "{name}" cannot be empty!' )