mlchem.ml.feature_selection.wrappers.crossval¶
- crossval(estimator, X: ndarray | DataFrame, y: ndarray | DataFrame, metric_function: Callable, n_fold: int = 5, task_type: Literal['classification', 'regression'] = 'classification', random_state: int | None = None) ndarray¶
Evaluate an estimator using cross-validation.
This function performs K-fold cross-validation on the given dataset using the specified estimator and metric function. It supports both classification and regression tasks.
- Parameters:
estimator (object) – A scikit-learn compatible estimator.
X (numpy.ndarray or pandas.DataFrame) – Feature matrix of shape (n_samples, n_features).
y (numpy.ndarray or pandas.DataFrame) – Target vector of shape (n_samples,) or (n_samples, 1).
metric_function (callable) – A scoring function that accepts (y_true, y_pred) as arguments.
n_fold (int, optional (default=5)) – Number of folds for cross-validation. If equal to n_samples, performs leave-one-out cross-validation.
task_type ({'classification', 'regression'}, optional (default='classification')) – Type of task to determine the cross-validation strategy.
random_state (int or None, optional (default=None)) – Random seed for reproducibility.
- Returns:
An array of cross-validation scores.
- Return type:
numpy.ndarray