cursus.steps.scripts.feature_selection¶
Feature Selection Script
A comprehensive feature selection script that implements multiple statistical and machine learning-based feature selection methods. Follows the cursus framework’s testability patterns and provides a self-contained solution.
Author: Cursus Framework Date: 2025-10-25
- check_call(cmd, *a, **k)¶
- install_packages_from_public_pypi(packages)[source]¶
Install packages from standard public PyPI.
- Parameters:
packages (list) – List of package specifications (e.g., [“pandas==1.5.0”, “numpy”])
- install_packages_from_secure_pypi(packages)[source]¶
Install packages from secure CodeArtifact PyPI.
- Parameters:
packages (list) – List of package specifications (e.g., [“pandas==1.5.0”, “numpy”])
- install_packages(packages, use_secure=False)[source]¶
Install packages from PyPI source based on configuration.
This is the main installation function that delegates to either public or secure PyPI based on the USE_SECURE_PYPI environment variable.
- Parameters:
- Environment Variables:
USE_SECURE_PYPI: Set to “true” to use secure PyPI, “false” for public PyPI
Example
# Install from public PyPI (default) install_packages([“xgboost==1.7.3”])
# Install from secure PyPI os.environ[“USE_SECURE_PYPI”] = “true” install_packages([“xgboost==1.7.3”])
- copy_existing_artifacts(src_dir, dst_dir)[source]¶
Copy all existing model artifacts from previous processing steps.
This enables the parameter accumulator pattern where each step: 1. Copies artifacts from previous steps 2. Adds its own artifacts 3. Passes all artifacts to the next step
- load_selected_features(model_artifacts_dir)[source]¶
Load pre-computed selected features from training job.
- load_single_split_data(input_data_dir, job_type)[source]¶
Load single split data for non-training job types with format detection.
- load_preprocessed_data(input_data_dir)[source]¶
Load train/val/test splits from tabular preprocessing output with format detection.
- save_selected_data(splits, selected_features, target_variable, output_dir)[source]¶
Save feature-selected splits preserving input format.
- correlation_based_selection(X, y, threshold=0.95)[source]¶
Remove highly correlated features, keeping those with higher target correlation.
- mutual_info_selection(X, y, k=10, random_state=42)[source]¶
Select features based on mutual information with target.
- chi2_selection(X, y, k=10)[source]¶
Select features using chi-square test (for non-negative features).
- rfe_selection(X, y, estimator_type='rf', n_features=10, random_state=42)[source]¶
Recursive Feature Elimination with various estimators.
- feature_importance_selection(X, y, method='random_forest', n_features=10, random_state=42)[source]¶
Select features based on model feature importance.
- Parameters:
- Returns:
Dictionary with selected features and scores
- Return type:
- lasso_selection(X, y, alpha=0.01, random_state=42)[source]¶
Select features using LASSO regularization.
- permutation_importance_selection(X, y, estimator_type='rf', n_features=10, random_state=42)[source]¶
Select features using permutation importance.
- combine_selection_results(method_results, combination_strategy='voting', final_k=10)[source]¶
Combine results from multiple feature selection methods.
- apply_feature_selection_pipeline(splits, target_variable, methods, method_configs)[source]¶
Apply feature selection pipeline using training data, then apply to all splits.
- Parameters:
- Returns:
Dictionary with selection results and metadata
- Return type:
- save_selection_results(selection_results, model_artifacts_dir)[source]¶
Save feature selection results and metadata to model artifacts directory.
- main(input_paths, output_paths, environ_vars, job_args)[source]¶
Main function for feature selection processing.
- Parameters:
input_paths (Dict[str, str]) – Dictionary of input paths with logical names - “input_data”: Directory containing train/val/test splits from tabular preprocessing - “model_artifacts_input”: Model artifacts from previous steps (standardized)
output_paths (Dict[str, str]) – Dictionary of output paths with logical names - “processed_data”: Directory for feature-selected train/val/test splits (XGBoost input format) - “model_artifacts_output”: Model artifacts output for next steps (standardized)
environ_vars (Dict[str, str]) – Dictionary of environment variables - “FEATURE_SELECTION_METHODS”: Comma-separated list of methods - “LABEL_FIELD”: Target column name (standard across framework) - “N_FEATURES_TO_SELECT”: Number/percentage of features to select - “CORRELATION_THRESHOLD”: Threshold for correlation filtering - “VARIANCE_THRESHOLD”: Threshold for variance filtering - “RANDOM_STATE”: Random seed for reproducibility - “COMBINATION_STRATEGY”: Method combination strategy
job_args (Namespace) – Command line arguments - job_type: Must be “training” to process all splits