gaitsetpy.classification API documentation

# gaitsetpy.classification

classification: A module for training and evaluating classification models.

This module provides both the new class-based classification models and legacy function-based API. All classification models inherit from BaseClassificationModel and are registered with the ClassificationManager.

Available Models:

- Random Forest (fully implemented)
- MLP (PyTorch) - TODO
- LSTM (PyTorch) - TODO
- BiLSTM (PyTorch) - TODO
- GNN (PyTorch Geometric) - TODO

Utilities:

- Dataset loading and preprocessing
- Model training and evaluation
- Feature preprocessing and preparation

Maintainer: @aharshit123456

View Source

      1"""
      2classification: A module for training and evaluating classification models.
      3
      4This module provides both the new class-based classification models and legacy function-based API.
      5All classification models inherit from BaseClassificationModel and are registered with the ClassificationManager.
      6
      7Available Models:
      8- Random Forest (fully implemented)
      9- MLP (PyTorch) - TODO
     10- LSTM (PyTorch) - TODO
     11- BiLSTM (PyTorch) - TODO
     12- GNN (PyTorch Geometric) - TODO
     13
     14Utilities:
     15- Dataset loading and preprocessing
     16- Model training and evaluation
     17- Feature preprocessing and preparation
     18
     19Maintainer: @aharshit123456
     20"""
     21
     22# Import the new class-based classification models
     23from .models.random_forest import RandomForestModel
     24
     25# Import legacy functions for backward compatibility
     26from .models.random_forest import create_random_forest_model
     27from .utils.preprocess import preprocess_features
     28from .utils.eval import evaluate_model
     29
     30# Import managers
     31from ..core.managers import ClassificationManager
     32
     33# Register all classification models with the manager
     34def _register_models():
     35    """Register all available classification models with the ClassificationManager."""
     36    manager = ClassificationManager()
     37    manager.register_model("random_forest", RandomForestModel)
     38
     39# Auto-register models when module is imported
     40_register_models()
     41
     42# Convenient access to the classification manager
     43def get_classification_manager():
     44    """Get the singleton ClassificationManager instance."""
     45    return ClassificationManager()
     46
     47# Helper function to get available models
     48def get_available_models():
     49    """Get list of available classification model names."""
     50    return ClassificationManager().get_available_components()
     51
     52# Helper function to train model using manager
     53def train_model(model_name: str, features, **kwargs):
     54    """
     55    Train a classification model using the ClassificationManager.
     56
     57    Args:
     58        model_name: Name of the classification model
     59        features: List of feature dictionaries
     60        **kwargs: Additional arguments for training
     61
     62    Returns:
     63        Trained model instance
     64    """
     65    return ClassificationManager().train_model(model_name, features, **kwargs)
     66
     67# Helper function to make predictions using manager
     68def predict(model_name: str, features, **kwargs):
     69    """
     70    Make predictions using the ClassificationManager.
     71
     72    Args:
     73        model_name: Name of the classification model
     74        features: List of feature dictionaries
     75        **kwargs: Additional arguments for prediction
     76
     77    Returns:
     78        Predictions array
     79    """
     80    return ClassificationManager().predict(model_name, features, **kwargs)
     81
     82# Helper function to evaluate model using manager
     83def evaluate_model_performance(model_name: str, features, **kwargs):
     84    """
     85    Evaluate a classification model using the ClassificationManager.
     86
     87    Args:
     88        model_name: Name of the classification model
     89        features: List of feature dictionaries
     90        **kwargs: Additional arguments for evaluation
     91
     92    Returns:
     93        Evaluation metrics dictionary
     94    """
     95    return ClassificationManager().evaluate_model(model_name, features, **kwargs)
     96
     97# Convenient wrapper functions for Random Forest
     98def create_random_forest(n_estimators=100, random_state=42, max_depth=None):
     99    """
    100    Create a Random Forest model with specified parameters.
    101
    102    Args:
    103        n_estimators: Number of trees in the forest
    104        random_state: Random state for reproducibility
    105        max_depth: Maximum depth of the tree
    106
    107    Returns:
    108        RandomForestModel instance
    109    """
    110    return RandomForestModel(n_estimators=n_estimators, random_state=random_state, max_depth=max_depth)
    111
    112def train_random_forest(features, **kwargs):
    113    """
    114    Train a Random Forest model on the given features.
    115
    116    Args:
    117        features: List of feature dictionaries
    118        **kwargs: Additional arguments for training
    119
    120    Returns:
    121        Trained RandomForestModel instance
    122    """
    123    model = RandomForestModel()
    124    model.train(features, **kwargs)
    125    return model
    126
    127__all__ = [
    128    # New class-based models
    129    'RandomForestModel',
    130    # Legacy functions for backward compatibility
    131    'create_random_forest_model',
    132    'preprocess_features',
    133    'evaluate_model',
    134    # Manager functions
    135    'get_classification_manager',
    136    'get_available_models',
    137    'train_model',
    138    'predict',
    139    'evaluate_model_performance',
    140    # Convenient wrapper functions
    141    'create_random_forest',
    142    'train_random_forest'
    143]

class RandomForestModel(gaitsetpy.core.base_classes.BaseClassificationModel): View Source

     21class RandomForestModel(BaseClassificationModel):
     22    """
     23    Random Forest classification model.
     24
     25    This class provides Random Forest classification functionality with
     26    comprehensive training, prediction, and evaluation capabilities.
     27    """
     28
     29    def __init__(self, n_estimators: int = 100, random_state: int = 42, max_depth: Optional[int] = None):
     30        super().__init__(
     31            name="random_forest",
     32            description="Random Forest classifier for gait data classification"
     33        )
     34        self.config = {
     35            'n_estimators': n_estimators,
     36            'random_state': random_state,
     37            'max_depth': max_depth
     38        }
     39        self.model = RandomForestClassifier(
     40            n_estimators=n_estimators,
     41            random_state=random_state,
     42            max_depth=max_depth
     43        )
     44        self.feature_names = []
     45        self.class_names = []
     46
     47    def train(self, features: List[Dict], **kwargs):
     48        """
     49        Train the Random Forest model on the given features.
     50
     51        Args:
     52            features: List of feature dictionaries
     53            **kwargs: Additional arguments including test_size, validation_split
     54        """
     55        # Preprocess features
     56        X, y = preprocess_features(features)
     57
     58        # Store feature and class information
     59        self.feature_names = [f"feature_{i}" for i in range(X.shape[1])]
     60        self.class_names = list(set(y))
     61
     62        # Split data if test_size is specified
     63        test_size = kwargs.get('test_size', 0.2)
     64        validation_split = kwargs.get('validation_split', True)
     65
     66        if validation_split:
     67            X_train, X_test, y_train, y_test = train_test_split(
     68                X, y, test_size=test_size, random_state=self.config['random_state']
     69            )
     70
     71            # Train model
     72            self.model.fit(X_train, y_train)
     73
     74            # Store validation data for later evaluation
     75            self.X_test = X_test
     76            self.y_test = y_test
     77
     78            # Print training accuracy
     79            train_accuracy = self.model.score(X_train, y_train)
     80            test_accuracy = self.model.score(X_test, y_test)
     81
     82            print(f"Training accuracy: {train_accuracy:.4f}")
     83            print(f"Validation accuracy: {test_accuracy:.4f}")
     84        else:
     85            # Train on all data
     86            self.model.fit(X, y)
     87            train_accuracy = self.model.score(X, y)
     88            print(f"Training accuracy: {train_accuracy:.4f}")
     89
     90        self.trained = True
     91        print("Random Forest model trained successfully.")
     92
     93    def predict(self, features: List[Dict], **kwargs) -> Union[np.ndarray, Any]:
     94        """
     95        Make predictions using the trained Random Forest model.
     96
     97        Args:
     98            features: List of feature dictionaries
     99            **kwargs: Additional arguments including return_probabilities
    100
    101        Returns:
    102            Array of predictions or probabilities
    103        """
    104        if not self.trained:
    105            raise ValueError("Model must be trained before making predictions")
    106
    107        # Preprocess features
    108        X, _ = preprocess_features(features)
    109
    110        # Make predictions
    111        return_probabilities = kwargs.get('return_probabilities', False)
    112
    113        if return_probabilities:
    114            return self.model.predict_proba(X)
    115        else:
    116            return self.model.predict(X)
    117
    118    def evaluate(self, features: List[Dict], **kwargs) -> Dict[str, float]:
    119        """
    120        Evaluate the Random Forest model performance.
    121
    122        Args:
    123            features: List of feature dictionaries
    124            **kwargs: Additional arguments including detailed_report
    125
    126        Returns:
    127            Dictionary containing evaluation metrics
    128        """
    129        if not self.trained:
    130            raise ValueError("Model must be trained before evaluation")
    131
    132        # Use validation data if available, otherwise use provided features
    133        if hasattr(self, 'X_test') and hasattr(self, 'y_test'):
    134            X_test, y_test = self.X_test, self.y_test
    135        else:
    136            X_test, y_test = preprocess_features(features)
    137
    138        # Make predictions
    139        y_pred = self.model.predict(X_test)
    140
    141        # Calculate metrics
    142        accuracy = accuracy_score(y_test, y_pred)
    143        conf_matrix = confusion_matrix(y_test, y_pred)
    144
    145        # Basic metrics
    146        metrics = {
    147            'accuracy': accuracy,
    148            'confusion_matrix': conf_matrix.tolist()
    149        }
    150
    151        # Detailed report if requested
    152        detailed_report = kwargs.get('detailed_report', False)
    153        if detailed_report:
    154            class_report = classification_report(y_test, y_pred, output_dict=True)
    155            metrics['classification_report'] = class_report
    156
    157            # Feature importance
    158            if hasattr(self.model, 'feature_importances_'):
    159                feature_importance = dict(zip(self.feature_names, self.model.feature_importances_))
    160                metrics['feature_importance'] = feature_importance
    161
    162        return metrics
    163
    164    def save_model(self, filepath: str):
    165        """
    166        Save the trained Random Forest model to a file.
    167
    168        Args:
    169            filepath: Path to save the model
    170        """
    171        if not self.trained:
    172            raise ValueError("Model must be trained before saving")
    173
    174        # Save model with additional metadata
    175        model_data = {
    176            'model': self.model,
    177            'config': self.config,
    178            'feature_names': self.feature_names,
    179            'class_names': self.class_names,
    180            'trained': self.trained
    181        }
    182
    183        joblib.dump(model_data, filepath)
    184        print(f"Random Forest model saved to {filepath}")
    185
    186    def load_model(self, filepath: str):
    187        """
    188        Load a trained Random Forest model from a file.
    189
    190        Args:
    191            filepath: Path to the saved model
    192        """
    193        try:
    194            model_data = joblib.load(filepath)
    195
    196            # Handle legacy model format
    197            if isinstance(model_data, dict):
    198                self.model = model_data['model']
    199                self.config = model_data.get('config', self.config)
    200                self.feature_names = model_data.get('feature_names', [])
    201                self.class_names = model_data.get('class_names', [])
    202                self.trained = model_data.get('trained', True)
    203            else:
    204                # Legacy format - just the model
    205                self.model = model_data
    206                self.trained = True
    207
    208            print(f"Random Forest model loaded from {filepath}")
    209        except Exception as e:
    210            print(f"Error loading model: {e}")
    211            raise
    212
    213    def get_feature_importance(self) -> Dict[str, float]:
    214        """
    215        Get feature importance scores.
    216
    217        Returns:
    218            Dictionary mapping feature names to importance scores
    219        """
    220        if not self.trained:
    221            raise ValueError("Model must be trained to get feature importance")
    222
    223        if hasattr(self.model, 'feature_importances_'):
    224            return dict(zip(self.feature_names, self.model.feature_importances_))
    225        else:
    226            return {}
    227
    228    def predict_single(self, single_features: Dict) -> int:
    229        """
    230        Make prediction for a single feature vector.
    231
    232        Args:
    233            single_features: Dictionary containing features for a single sample
    234
    235        Returns:
    236            Predicted class
    237        """
    238        if not self.trained:
    239            raise ValueError("Model must be trained before making predictions")
    240
    241        # Convert single feature dict to format expected by preprocess_features
    242        features_list = [single_features]
    243        X, _ = preprocess_features(features_list)
    244
    245        return self.model.predict(X)[0]

Random Forest classification model.

This class provides Random Forest classification functionality with comprehensive training, prediction, and evaluation capabilities.

RandomForestModel( n_estimators: int = 100, random_state: int = 42, max_depth: Optional[int] = None) View Source

    29    def __init__(self, n_estimators: int = 100, random_state: int = 42, max_depth: Optional[int] = None):
    30        super().__init__(
    31            name="random_forest",
    32            description="Random Forest classifier for gait data classification"
    33        )
    34        self.config = {
    35            'n_estimators': n_estimators,
    36            'random_state': random_state,
    37            'max_depth': max_depth
    38        }
    39        self.model = RandomForestClassifier(
    40            n_estimators=n_estimators,
    41            random_state=random_state,
    42            max_depth=max_depth
    43        )
    44        self.feature_names = []
    45        self.class_names = []

Initialize the classification model.

Args: name: Name of the classification model description: Description of the classification model

config
model
feature_names
class_names
def train(self, features: List[Dict], **kwargs): View Source

    47    def train(self, features: List[Dict], **kwargs):
    48        """
    49        Train the Random Forest model on the given features.
    50
    51        Args:
    52            features: List of feature dictionaries
    53            **kwargs: Additional arguments including test_size, validation_split
    54        """
    55        # Preprocess features
    56        X, y = preprocess_features(features)
    57
    58        # Store feature and class information
    59        self.feature_names = [f"feature_{i}" for i in range(X.shape[1])]
    60        self.class_names = list(set(y))
    61
    62        # Split data if test_size is specified
    63        test_size = kwargs.get('test_size', 0.2)
    64        validation_split = kwargs.get('validation_split', True)
    65
    66        if validation_split:
    67            X_train, X_test, y_train, y_test = train_test_split(
    68                X, y, test_size=test_size, random_state=self.config['random_state']
    69            )
    70
    71            # Train model
    72            self.model.fit(X_train, y_train)
    73
    74            # Store validation data for later evaluation
    75            self.X_test = X_test
    76            self.y_test = y_test
    77
    78            # Print training accuracy
    79            train_accuracy = self.model.score(X_train, y_train)
    80            test_accuracy = self.model.score(X_test, y_test)
    81
    82            print(f"Training accuracy: {train_accuracy:.4f}")
    83            print(f"Validation accuracy: {test_accuracy:.4f}")
    84        else:
    85            # Train on all data
    86            self.model.fit(X, y)
    87            train_accuracy = self.model.score(X, y)
    88            print(f"Training accuracy: {train_accuracy:.4f}")
    89
    90        self.trained = True
    91        print("Random Forest model trained successfully.")

Train the Random Forest model on the given features.

Args: features: List of feature dictionaries **kwargs: Additional arguments including test_size, validation_split

def predict(self, features: List[Dict], **kwargs) -> Union[numpy.ndarray, Any]: View Source

     93    def predict(self, features: List[Dict], **kwargs) -> Union[np.ndarray, Any]:
     94        """
     95        Make predictions using the trained Random Forest model.
     96
     97        Args:
     98            features: List of feature dictionaries
     99            **kwargs: Additional arguments including return_probabilities
    100
    101        Returns:
    102            Array of predictions or probabilities
    103        """
    104        if not self.trained:
    105            raise ValueError("Model must be trained before making predictions")
    106
    107        # Preprocess features
    108        X, _ = preprocess_features(features)
    109
    110        # Make predictions
    111        return_probabilities = kwargs.get('return_probabilities', False)
    112
    113        if return_probabilities:
    114            return self.model.predict_proba(X)
    115        else:
    116            return self.model.predict(X)

Make predictions using the trained Random Forest model.

Args: features: List of feature dictionaries **kwargs: Additional arguments including return_probabilities

Returns: Array of predictions or probabilities

def evaluate(self, features: List[Dict], **kwargs) -> Dict[str, float]: View Source

    118    def evaluate(self, features: List[Dict], **kwargs) -> Dict[str, float]:
    119        """
    120        Evaluate the Random Forest model performance.
    121
    122        Args:
    123            features: List of feature dictionaries
    124            **kwargs: Additional arguments including detailed_report
    125
    126        Returns:
    127            Dictionary containing evaluation metrics
    128        """
    129        if not self.trained:
    130            raise ValueError("Model must be trained before evaluation")
    131
    132        # Use validation data if available, otherwise use provided features
    133        if hasattr(self, 'X_test') and hasattr(self, 'y_test'):
    134            X_test, y_test = self.X_test, self.y_test
    135        else:
    136            X_test, y_test = preprocess_features(features)
    137
    138        # Make predictions
    139        y_pred = self.model.predict(X_test)
    140
    141        # Calculate metrics
    142        accuracy = accuracy_score(y_test, y_pred)
    143        conf_matrix = confusion_matrix(y_test, y_pred)
    144
    145        # Basic metrics
    146        metrics = {
    147            'accuracy': accuracy,
    148            'confusion_matrix': conf_matrix.tolist()
    149        }
    150
    151        # Detailed report if requested
    152        detailed_report = kwargs.get('detailed_report', False)
    153        if detailed_report:
    154            class_report = classification_report(y_test, y_pred, output_dict=True)
    155            metrics['classification_report'] = class_report
    156
    157            # Feature importance
    158            if hasattr(self.model, 'feature_importances_'):
    159                feature_importance = dict(zip(self.feature_names, self.model.feature_importances_))
    160                metrics['feature_importance'] = feature_importance
    161
    162        return metrics

Evaluate the Random Forest model performance.

Args: features: List of feature dictionaries **kwargs: Additional arguments including detailed_report

Returns: Dictionary containing evaluation metrics

def save_model(self, filepath: str): View Source

    164    def save_model(self, filepath: str):
    165        """
    166        Save the trained Random Forest model to a file.
    167
    168        Args:
    169            filepath: Path to save the model
    170        """
    171        if not self.trained:
    172            raise ValueError("Model must be trained before saving")
    173
    174        # Save model with additional metadata
    175        model_data = {
    176            'model': self.model,
    177            'config': self.config,
    178            'feature_names': self.feature_names,
    179            'class_names': self.class_names,
    180            'trained': self.trained
    181        }
    182
    183        joblib.dump(model_data, filepath)
    184        print(f"Random Forest model saved to {filepath}")

Save the trained Random Forest model to a file.

Args: filepath: Path to save the model

def load_model(self, filepath: str): View Source

    186    def load_model(self, filepath: str):
    187        """
    188        Load a trained Random Forest model from a file.
    189
    190        Args:
    191            filepath: Path to the saved model
    192        """
    193        try:
    194            model_data = joblib.load(filepath)
    195
    196            # Handle legacy model format
    197            if isinstance(model_data, dict):
    198                self.model = model_data['model']
    199                self.config = model_data.get('config', self.config)
    200                self.feature_names = model_data.get('feature_names', [])
    201                self.class_names = model_data.get('class_names', [])
    202                self.trained = model_data.get('trained', True)
    203            else:
    204                # Legacy format - just the model
    205                self.model = model_data
    206                self.trained = True
    207
    208            print(f"Random Forest model loaded from {filepath}")
    209        except Exception as e:
    210            print(f"Error loading model: {e}")
    211            raise

Load a trained Random Forest model from a file.

Args: filepath: Path to the saved model

def get_feature_importance(self) -> Dict[str, float]: View Source

    213    def get_feature_importance(self) -> Dict[str, float]:
    214        """
    215        Get feature importance scores.
    216
    217        Returns:
    218            Dictionary mapping feature names to importance scores
    219        """
    220        if not self.trained:
    221            raise ValueError("Model must be trained to get feature importance")
    222
    223        if hasattr(self.model, 'feature_importances_'):
    224            return dict(zip(self.feature_names, self.model.feature_importances_))
    225        else:
    226            return {}

Get feature importance scores.

Returns: Dictionary mapping feature names to importance scores

def predict_single(self, single_features: Dict) -> int: View Source

    228    def predict_single(self, single_features: Dict) -> int:
    229        """
    230        Make prediction for a single feature vector.
    231
    232        Args:
    233            single_features: Dictionary containing features for a single sample
    234
    235        Returns:
    236            Predicted class
    237        """
    238        if not self.trained:
    239            raise ValueError("Model must be trained before making predictions")
    240
    241        # Convert single feature dict to format expected by preprocess_features
    242        features_list = [single_features]
    243        X, _ = preprocess_features(features_list)
    244
    245        return self.model.predict(X)[0]

Make prediction for a single feature vector.

Args: single_features: Dictionary containing features for a single sample

Returns: Predicted class

##### Inherited Members

gaitsetpy.core.base_classes.BaseClassificationModel
  name
  description
  trained
  configure
  get_info

def create_random_forest_model(n_estimators=100, random_state=42, max_depth=None): View Source

    249def create_random_forest_model(n_estimators=100, random_state=42, max_depth=None):
    250    """
    251    Create a Random Forest model with specified parameters.
    252
    253    Args:
    254        n_estimators: Number of trees in the forest
    255        random_state: Random state for reproducibility
    256        max_depth: Maximum depth of the tree
    257
    258    Returns:
    259        RandomForestModel instance
    260    """
    261    return RandomForestModel(n_estimators=n_estimators, random_state=random_state, max_depth=max_depth)

Create a Random Forest model with specified parameters.

Args: n_estimators: Number of trees in the forest random_state: Random state for reproducibility max_depth: Maximum depth of the tree

Returns: RandomForestModel instance

def preprocess_features(features): View Source

    14def preprocess_features(features):
    15    """
    16    Convert the features dictionary into X (feature matrix) and y (labels),
    17    ensuring all feature vectors have a consistent length.
    18    """
    19    X = []
    20    y = []
    21    feature_lengths = []  # Track feature lengths to standardize across sensors
    22
    23    for sensor_dict in features:
    24        sensor_name = sensor_dict["name"]
    25        sensor_features = sensor_dict["features"]
    26        sensor_annotations = sensor_dict["annotations"]
    27
    28        num_windows = len(sensor_annotations)  # Expected number of windows
    29        feature_arrays = []
    30
    31        for key in sensor_features:
    32            feature_array = sensor_features[key]  # Extract the feature list
    33            feature_array = np.array(feature_array, dtype=object)  # Convert to NumPy object array
    34
    35            # Ensure it's a list of equal-length vectors
    36            if isinstance(feature_array[0], (list, np.ndarray)):
    37                print(f"Fixing inconsistent feature '{key}' in sensor '{sensor_name}'.")
    38
    39                # Find max length for this feature across all windows
    40                max_length = max(len(f) if isinstance(f, (list, np.ndarray)) else 1 for f in feature_array)
    41                feature_lengths.append(max_length)  # Store max feature length for later
    42
    43                # Pad/truncate each feature to be the same length
    44                feature_array = np.array([
    45                    np.pad(np.ravel(f), (0, max_length - len(f)), 'constant', constant_values=0)
    46                    if isinstance(f, (list, np.ndarray)) else np.array([f] + [0] * (max_length - 1))
    47                    for f in feature_array
    48                ])
    49
    50            # Ensure consistency in number of windows
    51            if len(feature_array) != num_windows:
    52                print(f"Skipping feature '{key}' due to mismatched length: {len(feature_array)} instead of {num_windows}.")
    53                continue
    54
    55            feature_arrays.append(feature_array)
    56
    57        if not feature_arrays:
    58            continue
    59
    60        # Concatenate features per window
    61        try:
    62            feature_matrix = np.column_stack(feature_arrays)
    63        except ValueError:
    64            print(f"Error: Features in sensor '{sensor_name}' have inconsistent shapes. Skipping sensor.")
    65            continue
    66
    67        X.append(feature_matrix)
    68        y.append(np.array(sensor_annotations))
    69
    70    if not X or not y:
    71        raise ValueError("No valid features or labels found.")
    72
    73    # **Fix: Standardize feature matrix sizes across sensors**
    74    max_feature_dim = max(map(lambda x: x.shape[1], X))  # Get the max feature size
    75    print(f"Standardizing all feature vectors to {max_feature_dim} dimensions.")
    76
    77    # Pad/truncate all feature matrices to match max_feature_dim
    78    X = [np.pad(x, ((0, 0), (0, max_feature_dim - x.shape[1])), 'constant', constant_values=0) if x.shape[1] < max_feature_dim else x[:, :max_feature_dim] for x in X]
    79
    80    # Stack all feature matrices
    81    X = np.vstack(X).astype(np.float32)
    82    y = np.concatenate(y)
    83
    84    # Remap labels to zero-based contiguous integers
    85    unique_labels = np.unique(y)
    86    label_map = {label: idx for idx, label in enumerate(unique_labels)}
    87    y_remapped = np.array([label_map[label] for label in y])
    88
    89    # Also update annotations in feature_dicts
    90    # This part of the code was not provided in the original file,
    91    # so I'm not adding it as per instruction 1.
    92
    93    return X, y_remapped

Convert the features dictionary into X (feature matrix) and y (labels), ensuring all feature vectors have a consistent length.

def evaluate_model(model, features): View Source

    12def evaluate_model(model, features):
    13    """
    14    Evaluates the given model on the provided features and prints accuracy and confusion matrix.
    15    """
    16    X, y = preprocess_features(features)
    17    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    18
    19    y_pred = model.predict(X_test)
    20
    21    acc = accuracy_score(y_test, y_pred)
    22    # conf_matrix = confusion_matrix(y_test, y_pred)
    23
    24    print(f"Accuracy: {acc:.4f}")
    25    # print(f"Confusion Matrix:\n{conf_matrix}")

Evaluates the given model on the provided features and prints accuracy and confusion matrix.

def get_classification_manager(): View Source

    44def get_classification_manager():
    45    """Get the singleton ClassificationManager instance."""
    46    return ClassificationManager()

Get the singleton ClassificationManager instance.

def get_available_models(): View Source

    49def get_available_models():
    50    """Get list of available classification model names."""
    51    return ClassificationManager().get_available_components()

Get list of available classification model names.

def train_model(model_name: str, features, **kwargs): View Source

    54def train_model(model_name: str, features, **kwargs):
    55    """
    56    Train a classification model using the ClassificationManager.
    57
    58    Args:
    59        model_name: Name of the classification model
    60        features: List of feature dictionaries
    61        **kwargs: Additional arguments for training
    62
    63    Returns:
    64        Trained model instance
    65    """
    66    return ClassificationManager().train_model(model_name, features, **kwargs)

Train a classification model using the ClassificationManager.

Args: model_name: Name of the classification model features: List of feature dictionaries **kwargs: Additional arguments for training

Returns: Trained model instance

def predict(model_name: str, features, **kwargs): View Source

    69def predict(model_name: str, features, **kwargs):
    70    """
    71    Make predictions using the ClassificationManager.
    72
    73    Args:
    74        model_name: Name of the classification model
    75        features: List of feature dictionaries
    76        **kwargs: Additional arguments for prediction
    77
    78    Returns:
    79        Predictions array
    80    """
    81    return ClassificationManager().predict(model_name, features, **kwargs)

Make predictions using the ClassificationManager.

Args: model_name: Name of the classification model features: List of feature dictionaries **kwargs: Additional arguments for prediction

Returns: Predictions array

def evaluate_model_performance(model_name: str, features, **kwargs): View Source

    84def evaluate_model_performance(model_name: str, features, **kwargs):
    85    """
    86    Evaluate a classification model using the ClassificationManager.
    87
    88    Args:
    89        model_name: Name of the classification model
    90        features: List of feature dictionaries
    91        **kwargs: Additional arguments for evaluation
    92
    93    Returns:
    94        Evaluation metrics dictionary
    95    """
    96    return ClassificationManager().evaluate_model(model_name, features, **kwargs)

Evaluate a classification model using the ClassificationManager.

Args: model_name: Name of the classification model features: List of feature dictionaries **kwargs: Additional arguments for evaluation

Returns: Evaluation metrics dictionary

def create_random_forest(n_estimators=100, random_state=42, max_depth=None): View Source

     99def create_random_forest(n_estimators=100, random_state=42, max_depth=None):
    100    """
    101    Create a Random Forest model with specified parameters.
    102
    103    Args:
    104        n_estimators: Number of trees in the forest
    105        random_state: Random state for reproducibility
    106        max_depth: Maximum depth of the tree
    107
    108    Returns:
    109        RandomForestModel instance
    110    """
    111    return RandomForestModel(n_estimators=n_estimators, random_state=random_state, max_depth=max_depth)

Create a Random Forest model with specified parameters.

Args: n_estimators: Number of trees in the forest random_state: Random state for reproducibility max_depth: Maximum depth of the tree

Returns: RandomForestModel instance

def train_random_forest(features, **kwargs): View Source

    113def train_random_forest(features, **kwargs):
    114    """
    115    Train a Random Forest model on the given features.
    116
    117    Args:
    118        features: List of feature dictionaries
    119        **kwargs: Additional arguments for training
    120
    121    Returns:
    122        Trained RandomForestModel instance
    123    """
    124    model = RandomForestModel()
    125    model.train(features, **kwargs)
    126    return model

Train a Random Forest model on the given features.

Args: features: List of feature dictionaries **kwargs: Additional arguments for training

Returns: Trained RandomForestModel instance