(
    estimator, X, y=None, scoring=accuracy_score, cv=5, args_to_scorer=None
)
return result
Use the given estimator to perform fit and predict for splits defined by 'cv' and compute the given score on
    each of the splits.

    Parameters
    ----------

    estimator: A valid sklearn_wrapper estimator
    X, y: Valid data and target values that work with the estimator
    scoring: string or a scorer object created using
        https://scikit-learn.org/stable/modules/generated/sklearn.metrics.make_scorer.html#sklearn.metrics.make_scorer.
        A string from sklearn.metrics.SCORERS.keys() can be used or a scorer created from one of
        sklearn.metrics (https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics).
        A completely custom scorer object can be created from a python function following the example at
        https://scikit-learn.org/stable/modules/model_evaluation.html
        The metric has to return a scalar value,
    cv: an integer or an object that has a split function as a generator yielding (train, test) splits as arrays of indices.
        Integer value is used as number of folds in sklearn.model_selection.StratifiedKFold, default is 5.
        Note that any of the iterators from https://scikit-learn.org/stable/modules/cross_validation.html#cross-validation-iterators can be used here.
    args_to_scorer: A dictionary of additional keyword arguments to pass to the scorer.
                Used for cases where the scorer has a signature such as ``scorer(estimator, X, y, **kwargs)``.
    Returns
    -------
        cv_results: a list of scores corresponding to each cross validation fold
Use the given estimator to perform fit and predict for splits defined by 'cv' and compute the given score on
    each of the splits.
Use the given estimator to perform fit and predict for splits defined by cv and compute the given score on each of the splits .
def cross_val_score_track_trials(
    estimator, X, y=None, scoring=accuracy_score, cv=5, args_to_scorer=None
):
    """
    Use the given estimator to perform fit and predict for splits defined by 'cv' and compute the given score on
    each of the splits.

    Parameters
    ----------

    estimator: A valid sklearn_wrapper estimator
    X, y: Valid data and target values that work with the estimator
    scoring: string or a scorer object created using
        https://scikit-learn.org/stable/modules/generated/sklearn.metrics.make_scorer.html#sklearn.metrics.make_scorer.
        A string from sklearn.metrics.SCORERS.keys() can be used or a scorer created from one of
        sklearn.metrics (https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics).
        A completely custom scorer object can be created from a python function following the example at
        https://scikit-learn.org/stable/modules/model_evaluation.html
        The metric has to return a scalar value,
    cv: an integer or an object that has a split function as a generator yielding (train, test) splits as arrays of indices.
        Integer value is used as number of folds in sklearn.model_selection.StratifiedKFold, default is 5.
        Note that any of the iterators from https://scikit-learn.org/stable/modules/cross_validation.html#cross-validation-iterators can be used here.
    args_to_scorer: A dictionary of additional keyword arguments to pass to the scorer.
                Used for cases where the scorer has a signature such as ``scorer(estimator, X, y, **kwargs)``.
    Returns
    -------
        cv_results: a list of scores corresponding to each cross validation fold
    """
    if isinstance(cv, int):
        cv = StratifiedKFold(cv)

    if args_to_scorer is None:
        args_to_scorer = {}
    scorer = check_scoring(estimator, scoring=scoring)
    cv_results: List[float] = []
    log_loss_results = []
    time_results = []
    for train, test in cv.split(X, y):
        X_train, y_train = split_with_schemas(estimator, X, y, train)
        X_test, y_test = split_with_schemas(estimator, X, y, test, train)
        start = time.time()
        # Not calling sklearn.base.clone() here, because:
        #  (1) For Lale pipelines, clone() calls the pipeline constructor
        #      with edges=None, so the resulting topology is incorrect.
        #  (2) For Lale individual operators, the fit() method already
        #      clones the impl object, so cloning again is redundant.
        trained = estimator.fit(X_train, y_train)
        score_value = scorer(trained, X_test, y_test, **args_to_scorer)
        execution_time = time.time() - start
        # not all estimators have predict probability
        try:
            y_pred_proba = trained.predict_proba(X_test)
            logloss = log_loss(y_true=y_test, y_pred=y_pred_proba)
            log_loss_results.append(logloss)
        except BaseException:
            logger.debug("Warning, log loss cannot be computed")
        cv_results.append(score_value)
        time_results.append(execution_time)
    result = (
        np.array(cv_results).mean(),
        np.array(log_loss_results).mean(),
        np.array(time_results).mean(),
    )
    return result
def cross_val_score_track_trials ( estimator , X , y = None , scoring = accuracy_score , cv = 5 , args_to_scorer = None ) : if isinstance ( cv , int ) : cv = StratifiedKFold ( cv ) if args_to_scorer is None : args_to_scorer = { } scorer = check_scoring ( estimator , scoring = scoring ) cv_results : List [ float ] = [ ] log_loss_results = [ ] time_results = [ ] for train , test in cv . split ( X , y ) : X_train , y_train = split_with_schemas ( estimator , X , y , train ) X_test , y_test = split_with_schemas ( estimator , X , y , test , train ) start = time . time ( ) # Not calling sklearn.base.clone() here, because: #  (1) For Lale pipelines, clone() calls the pipeline constructor #      with edges=None, so the resulting topology is incorrect. #  (2) For Lale individual operators, the fit() method already #      clones the impl object, so cloning again is redundant. trained = estimator . fit ( X_train , y_train ) score_value = scorer ( trained , X_test , y_test , * * args_to_scorer ) execution_time = time . time ( ) - start # not all estimators have predict probability try : y_pred_proba = trained . predict_proba ( X_test ) logloss = log_loss ( y_true = y_test , y_pred = y_pred_proba ) log_loss_results . append ( logloss ) except BaseException : logger . debug ( "Warning, log loss cannot be computed" ) cv_results . append ( score_value ) time_results . append ( execution_time ) result = ( np . array ( cv_results ) . mean ( ) , np . array ( log_loss_results ) . mean ( ) , np . array ( time_results ) . mean ( ) , ) return result