Ensures x_train and y_train are either pandas DataFrames or NumPy ndarrays. This check ensures that the data inputs are compatible with the models being tuned.
if not isinstance(x_train, (pd.DataFrame, np.ndarray)):
raise TypeError("model_tuning_core(): 'x_train' must be a pandas DataFrame or NumPy ndarray.")
if not isinstance(y_train, (pd.Series, np.ndarray)):
raise TypeError("model_tuning_core(): 'y_train' must be a pandas Series or NumPy ndarray.")
Parameter Type Checks
Validates types for all parameters to ensure they match expected types, preventing errors during processing. For example, ensuring task_type is a string, models is a dictionary, and integers are used where required.
if not isinstance(task_type, str):
raise TypeError("model_tuning_core(): 'task_type' must be a string.")
if not isinstance(models, dict):
raise TypeError("model_tuning_core(): 'models' must be a dictionary with model names as keys and model instances as values.")
if priority_metrics is not None and not isinstance(priority_metrics, list):
raise TypeError("model_tuning_core(): 'priority_metrics' must be a list of strings.")
if refit_metric is not None and not isinstance(refit_metric, (str, Callable)):
raise TypeError("model_tuning_core(): 'refit_metric' must be either a string or a callable object.")
Value Validations
Logical Consistency Checks
Checks for logical consistency, such as ensuring task_type is one of the allowed types ('classification' or 'regression') and the n_top_models parameter is greater than zero and less than the total available models.
if task_type not in ['classification', 'regression']:
raise ValueError("model_tuning_core(): 'task_type' must be either 'classification' or 'regression'.")
if n_top_models <= 0:
raise ValueError("model_tuning_core(): 'n_top_models' must be an integer greater than 0.")
Data Integrity Checks
Ensures that x_train and y_train have the same length and are not empty, which are fundamental requirements for fitting models.
if x_train.shape[0] != y_train.shape[0]:
raise ValueError("model_tuning_core(): 'x_train' and 'y_train' must have the same number of rows.")
if x_train.size == 0 or y_train.size == 0:
raise ValueError("model_tuning_core(): 'x_train' and 'y_train' cannot be empty.")
Specific Parameter Checks
Ensures all elements in priority_metrics and priority_tuners are strings and that the metrics and tuners specified are valid. Also checks the range of cv, n_iter_random, and n_iter_bayesian.
if any(not isinstance(metric, str) for metric in priority_metrics):
raise ValueError("model_tuning_core(): All elements in 'priority_metrics' must be strings.")
if priority_tuners and any(tuner not in ['grid', 'random', 'bayesian'] for tuner in priority_tuners):
raise ValueError("model_tuning_core(): 'priority_tuners' contains unrecognized tuners. Valid tuners are 'grid', 'random', 'bayesian'.")
if cv < 1:
raise ValueError("model_tuning_core(): 'cv' must be an integer greater than 0.")
if n_iter_random is not None and n_iter_random < 1:
raise ValueError("model_tuning_core(): 'n_iter_random' must be a non-negative integer greater than 0.")
if n_iter_bayesian is not None and n_iter_bayesian < 1:
raise ValueError("model_tuning_core(): 'n_iter_bayesian' must be a non-negative integer greater than 0.")
Error Handling in
model_tuning_core()
Type Validations
x_train
andy_train
are either pandas DataFrames or NumPy ndarrays. This check ensures that the data inputs are compatible with the models being tuned.task_type
is a string,models
is a dictionary, and integers are used where required.Value Validations
task_type
is one of the allowed types ('classification' or 'regression') and then_top_models
parameter is greater than zero and less than the total available models.x_train
andy_train
have the same length and are not empty, which are fundamental requirements for fitting models.priority_metrics
andpriority_tuners
are strings and that the metrics and tuners specified are valid. Also checks the range ofcv
,n_iter_random
, andn_iter_bayesian
.