darts icon indicating copy to clipboard operation
darts copied to clipboard

Custom metric for LinearRegression.gridsearch

Open brianreinke95 opened this issue 1 year ago • 1 comments

Hi!

I would like to pass this custom metric to gridsearch:

def asymmetric_custom_metric(y_true, y_pred, penalization_factor = 5):
    """
    Custom loss function that penalizes predictions below the true value more than predictions above the value.

    Parameters:
        y_true (ndarray): Array of true values.
        y_pred (ndarray): Array of predicted values.

    Returns:
        float: Custom loss value.

    """

    # Calculate the difference between true and predicted values
    diff = (y_pred - y_true).astype(float)

    # Calculate the loss
    loss = np.where(diff < 0, np.square(y_true - y_pred) * (1 + penalization_factor) , np.square(y_true - y_pred))

    # Calculate the average loss
    avg_loss = np.mean(loss)
    return avg_loss

But gridsearch only accepts metrics comming directly from darts.metrics.

There is a Wrapper to transform sklearn or custom metrics into a darts metric?

Thanks! Brian.

brianreinke95 avatar Feb 21 '24 14:02 brianreinke95

Hi @brianreinke95,

gridsearch() accepts Callable in as metric argument (no darts/sklearn requirements), however, you custom loss is missing some parts of logic:

  • the variables passed to the function are TimeSeries, not np.ndarray and you need to take care of the conversion.
  • the timeseries might have different time indexes (hence array shape)

Adding the code snippet below to your code made it work with gridsearch():


    # intersect indexes, assume prediction is the shortest
    y_true = y_true[y_pred.time_index]

    # convert to array
    y_pred = y_pred.all_values()
    y_true = y_true.all_values() 

madtoinou avatar Feb 22 '24 09:02 madtoinou