def hold_out_validation(
model: BaseEstimator,
X: ArrayLike2dType,
y: ArrayLike1dType,
X_val: ArrayLike2dType | None = None,
y_val: ArrayLike1dType | None = None,
*,
scoring: str | ScorerFuncType | None = None,
test_size: float | None = 0.2,
random_state: int | None = None,
shuffle: bool = True,
stratify: np.ndarray | None = None,
) -> float:
if X_val is not None and y_val is not None:
x_train, x_test, y_train, y_test = X, X_val, y, y_val
elif X_val is not None or y_val is not None:
raise InvalidInputError(
input_value={"X_val": X_val, "y_val": y_val},
message="To manually define validation set, both X_val and y_val must be specified.",
)
else:
x_train, x_test, y_train, y_test = train_test_split(
X,
y,
test_size=test_size,
random_state=random_state,
shuffle=shuffle,
stratify=stratify,
)
check_model_prediction_methods(model)
model.fit(x_train, y_train) # type: ignore
if scoring:
if isinstance(scoring, str):
scoring_func = get_scorer(scoring)
else:
scoring_func = scoring
score = scoring_func(model, x_test, y_test)
else:
score = model.score(x_test, y_test) # type: ignore
return score