Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError from eval_model function within Chapter 4: 02-Baseline Forecasts using darts.ipynb Cell 12 #39

Open
beliu opened this issue May 8, 2024 · 2 comments

Comments

@beliu
Copy link

beliu commented May 8, 2024

When running the eval_model function within Chapter 4: 02-Baseline Forecasts using darts.ipynb at cell 12, I get the following error:

Cell In[12], line 4
      2 naive_model = NaiveSeasonal(K=1)
      3 with LogTime() as timer:
----> 4     y_pred, metrics = eval_model(naive_model, ts_train, ts_val, name=name)
      5 metrics['Time Elapsed'] = timer.elapsed
      6 metric_record.append(metrics)

Cell In[10], line 11
      4 model.fit(ts_train)
      5 y_pred = model.predict(len(ts_test))
      6 return y_pred, {
      7     "Algorithm": name,
      8     "MAE": mae(actual_series = ts_test, pred_series = y_pred),
      9     "MSE": mse(actual_series = ts_test, pred_series = y_pred),
     10     "MASE": mase(actual_series = ts_test, pred_series = y_pred, insample=ts_train),
---> 11     "Forecast Bias": forecast_bias(actual_series = ts_test, pred_series = y_pred)
     12 }

File ~/Modern-Time-Series-Forecasting-with-Python/src/utils/ts_utils.py:102, in forecast_bias(actual_series, pred_series, intersect, reduction, inter_reduction, n_jobs, verbose)
    100 else:
    101     y_true, y_pred = _get_values_or_raise(actual_series, pred_series, intersect)
--> 102 y_true, y_pred = _remove_nan_union(y_true, y_pred)
    103 y_true_sum, y_pred_sum = np.sum(y_true), np.sum(y_pred)
...
   5348                          'length of {}'.format(N))
   5350     # optimization, the other branch is slower
   5351     keep = ~obj

ValueError: boolean array argument obj to delete must be one dimensional and match the axis length of 1488

I went into ts_utils.py and made the following changes:

def _remove_nan_union(array_a: np.ndarray,
                      array_b: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
    """
    Returns the two inputs arrays where all elements are deleted that have an index that corresponds to
    a NaN value in either of the two input arrays.
    """

    isnan_mask = np.logical_or(np.isnan(array_a), np.isnan(array_b))

    # I added the line below
    isnan_mask = isnan_mask.reshape(-1,)

    return np.delete(array_a, isnan_mask), np.delete(array_b, isnan_mask)

This resolved the ValueError but I do not know if there are any side-effects of my modification.

@abdullahozkoc
Copy link

Thank you for the modification. I added your change to my code, and when I generated the graph afterward, I found that it matched the one in the book. I think your change didn't affect the result.

@Laoban-man
Copy link

That modification corrected the problem for me too, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants