darts
darts copied to clipboard
Custom metric for LinearRegression.gridsearch
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.
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
, notnp.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()