Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
lshpaner committed Nov 22, 2024
2 parents 68a1f47 + 69b1f9a commit dd15484
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ __pycache__/

# C extensions
*.so

_build/
# CSV files (because most (if not all) contain identifying patient information
*.csv
.DS_Store
Expand Down
58 changes: 26 additions & 32 deletions src/model_tuner/model_tuner_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,27 @@ def __init__(
These use the naming convention that we enforce in the assemble_pipeline method.
"""

def get_preprocessing_and_feature_selection_pipeline(self, pipeline):
def get_preprocessing_and_feature_selection_pipeline(self):
steps = [
(name, transformer)
for name, transformer in pipeline.steps
for name, transformer in self.estimator.steps
if name.startswith("preprocess_") or name.startswith("feature_selection_")
]
return self.PipelineClass(steps)

def get_feature_selection_pipeline(self, pipeline):
def get_feature_selection_pipeline(self):
steps = [
(name, transformer)
for name, transformer in pipeline.steps
for name, transformer in self.estimator.steps
if name.startswith("feature_selection_")
]
return steps
return self.PipelineClass(steps)

def get_preprocessing_pipeline(self, pipeline):
def get_preprocessing_pipeline(self):
# Extract steps names that start with 'preprocess_'
preprocessing_steps = [
(name, transformer)
for name, transformer in pipeline.steps
for name, transformer in self.estimator.steps
if name.startswith("preprocess_")
]
return self.PipelineClass(preprocessing_steps)
Expand Down Expand Up @@ -396,7 +396,7 @@ def process_imbalance_sampler(self, X_train, y_train):
#### Preprocessor, Resampler, rfe, Estimator

if self.pipeline_steps:
preproc_test = self.get_preprocessing_pipeline(self.estimator)
preproc_test = self.get_preprocessing_pipeline()
else:
pass

Expand Down Expand Up @@ -654,18 +654,16 @@ def fit(self, X, y, validation_data=None, score=None):

# Get the combined preprocessing and feature selection pipeline
preproc_feat_select_pipe = (
self.get_preprocessing_and_feature_selection_pipeline(
self.estimator
)
self.get_preprocessing_and_feature_selection_pipeline()
)

### IF we have preprocessing steps then they need applying
### Otherwise do not apply them
if preproc_feat_select_pipe:
# Set parameters and fit the pipeline
preproc_feat_select_pipe.set_params(**params_no_estimator).fit(
X, y
)
preproc_feat_select_pipe.set_params(
**params_no_estimator
).fit(X, y)

# Transform the validation data
X_valid_transformed = preproc_feat_select_pipe.transform(
Expand Down Expand Up @@ -724,9 +722,7 @@ def fit(self, X, y, validation_data=None, score=None):

# Get the combined preprocessing and feature selection pipeline
preproc_feat_select_pipe = (
self.get_preprocessing_and_feature_selection_pipeline(
self.estimator
)
self.get_preprocessing_and_feature_selection_pipeline()
)

### IF we have preprocessing steps then they need applying
Expand Down Expand Up @@ -874,12 +870,12 @@ def return_metrics(self, X, y, optimal_threshold=False):
print("-" * 80)

if self.feature_selection:
k_best_features = self.print_selected_best_features(X)
best_features = self.print_selected_best_features(X)

return {
"Classification Report": self.classification_report,
"Confusion Matrix": conf_mat,
"K Best Features": k_best_features,
"Best Features": best_features,
}
else:
return {
Expand All @@ -889,10 +885,10 @@ def return_metrics(self, X, y, optimal_threshold=False):
else:
reg_report = self.regression_report(y, y_pred_valid)
if self.feature_selection:
k_best_features = self.print_selected_best_features(X)
best_features = self.print_selected_best_features(X)
return {
"Regression Report": reg_report,
"K Best Features": k_best_features,
"Best Features": best_features,
}
else:
return reg_report
Expand Down Expand Up @@ -1046,22 +1042,20 @@ def grid_search_param_tuning(
}

preproc_feat_select_pipe = (
self.get_preprocessing_and_feature_selection_pipeline(
self.estimator
)
self.get_preprocessing_and_feature_selection_pipeline()
)

### IF we have preprocessing steps then they need applying
### Otherwise do not apply them
### IF we have preprocessing steps then they need applying
### Otherwise do not apply them
if preproc_feat_select_pipe:
# Set parameters and fit the pipeline
preproc_feat_select_pipe.set_params(**params_no_estimator).fit(
X, y
)
preproc_feat_select_pipe.set_params(
**params_no_estimator
).fit(X, y)

# Transform the validation data
X_valid_transformed = preproc_feat_select_pipe.transform(
X_valid
X_valid_transformed = (
preproc_feat_select_pipe.transform(X_valid)
)
else:
X_valid_transformed = X_valid
Expand Down Expand Up @@ -1155,7 +1149,7 @@ def grid_search_param_tuning(

def print_selected_best_features(self, X):

feat_select_pipeline = self.get_feature_selection_pipeline(self.estimator)
feat_select_pipeline = self.get_feature_selection_pipeline()
feat_select_pipeline = feat_select_pipeline[0][1]
print()
support = feat_select_pipeline.get_support()
Expand Down

0 comments on commit dd15484

Please sign in to comment.