hi all, I'm a newbie to statsforecast, yesterday, ...
# general
p
hi all, I'm a newbie to statsforecast, yesterday, i run this code below, I can get the result. However, today when I continued my work, the line 'model.fit(test)' just did not work and kept showing Keyerror: 'fitted'. I've tried to search on stackoverflow but seems no information regarding how to solve this. I would very, very, very appreciate that you can help me to resolve the problem. Thank you in advance. The code: df = data[['Revenue', 'District', 'Month']] data_grouped = df.groupby(['District', 'Month'])['Revenue'].sum().reset_index() data_grouped = data_grouped.rename(columns={'Revenue': 'y', 'Month': 'ds', 'District': 'unique_id'}) train = data_grouped[data_grouped['ds'] < 9] test = data_grouped[data_grouped['ds'] > 8] h = test['ds'].nunique() print('horizon =', h) print('train:', train) print('test:', test) from statsforecast import StatsForecast from statsforecast.models import Naive, SeasonalNaive, WindowAverage, SeasonalWindowAverage model = StatsForecast(models=[Naive(), SeasonalNaive(season_length=4), WindowAverage(window_size=4), SeasonalWindowAverage(window_size=2, season_length=4)], freq='D', n_jobs=-1) model.fit(train) ---> error as below:
Copy code
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/lib/python3.10/dist-packages/statsforecast/core.py", line 77, in fit
    fm[i, i_model] = new_model.fit(y=y, X=X)
  File "/usr/local/lib/python3.10/dist-packages/statsforecast/models.py", line 3229, in fit
    residuals = y - mod["fitted"]
KeyError: 'fitted'
"""
The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-93-1acab57018a5> in <cell line: 1>()
----> 1 model.fit(train)
      2 predictions = model.predict(test)
6 frames
Copy code
/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in fit(self, df, sort_df, prediction_intervals)
    880             self.fitted_ = self.ga.fit(models=self.models)
    881         else:
--> 882             self.fitted_ = self._fit_parallel()
    883         return self
    884 

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in _fit_parallel(self)
   1309                 future = executor.apply_async(ga.fit, (self.models,))
   1310                 futures.append(future)
-> 1311             fm = np.vstack([f.get() for f in futures])
   1312         return fm
   1313 

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in <listcomp>(.0)
   1309                 future = executor.apply_async(ga.fit, (self.models,))
   1310                 futures.append(future)
-> 1311             fm = np.vstack([f.get() for f in futures])
   1312         return fm
   1313 

/usr/lib/python3.10/multiprocessing/pool.py in get(self, timeout)
    772             return self._value
    773         else:
--> 774             raise self._value
    775 
    776     def _set(self, i, obj):

/usr/lib/python3.10/multiprocessing/pool.py in worker()
    123         job, i, func, args, kwds = task
    124         try:
--> 125             result = (True, func(*args, **kwds))
    126         except Exception as e:
    127             if wrap_exception and func is not _helper_reraises_exception:

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in fit()
     75             for i_model, model in enumerate(models):
     76                 new_model = model.new()
---> 77                 fm[i, i_model] = new_model.fit(y=y, X=X)
     78         return fm
     79 

/usr/local/lib/python3.10/dist-packages/statsforecast/models.py in fit()
   3227         )
   3228         mod = dict(mod)
-> 3229         residuals = y - mod["fitted"]
   3230         mod["sigma"] = _calculate_sigma(residuals, len(y) - self.season_length)
   3231         self.model_ = mod

KeyError: 'fitted'RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/lib/python3.10/dist-packages/statsforecast/core.py", line 77, in fit
    fm[i, i_model] = new_model.fit(y=y, X=X)
  File "/usr/local/lib/python3.10/dist-packages/statsforecast/models.py", line 3229, in fit
    residuals = y - mod["fitted"]
KeyError: 'fitted'
"""
The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-93-1acab57018a5> in <cell line: 1>()
----> 1 model.fit(train)
      2 predictions = model.predict(test)
6 frames
Copy code
/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in fit(self, df, sort_df, prediction_intervals)
    880             self.fitted_ = self.ga.fit(models=self.models)
    881         else:
--> 882             self.fitted_ = self._fit_parallel()
    883         return self
    884 

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in _fit_parallel(self)
   1309                 future = executor.apply_async(ga.fit, (self.models,))
   1310                 futures.append(future)
-> 1311             fm = np.vstack([f.get() for f in futures])
   1312         return fm
   1313 

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in <listcomp>(.0)
   1309                 future = executor.apply_async(ga.fit, (self.models,))
   1310                 futures.append(future)
-> 1311             fm = np.vstack([f.get() for f in futures])
   1312         return fm
   1313 

/usr/lib/python3.10/multiprocessing/pool.py in get(self, timeout)
    772             return self._value
    773         else:
--> 774             raise self._value
    775 
    776     def _set(self, i, obj):

/usr/lib/python3.10/multiprocessing/pool.py in worker()
    123         job, i, func, args, kwds = task
    124         try:
--> 125             result = (True, func(*args, **kwds))
    126         except Exception as e:
    127             if wrap_exception and func is not _helper_reraises_exception:

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in fit()
     75             for i_model, model in enumerate(models):
     76                 new_model = model.new()
---> 77                 fm[i, i_model] = new_model.fit(y=y, X=X)
     78         return fm
     79 

/usr/local/lib/python3.10/dist-packages/statsforecast/models.py in fit()
   3227         )
   3228         mod = dict(mod)
-> 3229         residuals = y - mod["fitted"]
   3230         mod["sigma"] = _calculate_sigma(residuals, len(y) - self.season_length)
   3231         self.model_ = mod
KeyError: 'fitted'
Copy code
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/lib/python3.10/dist-packages/statsforecast/core.py", line 77, in fit
    fm[i, i_model] = new_model.fit(y=y, X=X)
  File "/usr/local/lib/python3.10/dist-packages/statsforecast/models.py", line 3229, in fit
    residuals = y - mod["fitted"]
KeyError: 'fitted'
"""
The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-93-1acab57018a5> in <cell line: 1>()
----> 1 model.fit(train)
      2 predictions = model.predict(test)
6 frames
Copy code
/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in fit(self, df, sort_df, prediction_intervals)
    880             self.fitted_ = self.ga.fit(models=self.models)
    881         else:
--> 882             self.fitted_ = self._fit_parallel()
    883         return self
    884 

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in _fit_parallel(self)
   1309                 future = executor.apply_async(ga.fit, (self.models,))
   1310                 futures.append(future)
-> 1311             fm = np.vstack([f.get() for f in futures])
   1312         return fm
   1313 

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in <listcomp>(.0)
   1309                 future = executor.apply_async(ga.fit, (self.models,))
   1310                 futures.append(future)
-> 1311             fm = np.vstack([f.get() for f in futures])
   1312         return fm
   1313 

/usr/lib/python3.10/multiprocessing/pool.py in get(self, timeout)
    772             return self._value
    773         else:
--> 774             raise self._value
    775 
    776     def _set(self, i, obj):

/usr/lib/python3.10/multiprocessing/pool.py in worker()
    123         job, i, func, args, kwds = task
    124         try:
--> 125             result = (True, func(*args, **kwds))
    126         except Exception as e:
    127             if wrap_exception and func is not _helper_reraises_exception:

/usr/local/lib/python3.10/dist-packages/statsforecast/core.py in fit()
     75             for i_model, model in enumerate(models):
     76                 new_model = model.new()
---> 77                 fm[i, i_model] = new_model.fit(y=y, X=X)
     78         return fm
     79 

/usr/local/lib/python3.10/dist-packages/statsforecast/models.py in fit()
   3227         )
   3228         mod = dict(mod)
-> 3229         residuals = y - mod["fitted"]
   3230         mod["sigma"] = _calculate_sigma(residuals, len(y) - self.season_length)
   3231         self.model_ = mod
KeyError: 'fitted'
j
Hey. Can you run this with n_jobs=1 to see if you get a clearer error?
p
thank you, i'll try now and get back soon
I still get the same error
j
Are you able to reproduce this error with simulated data so that I can take a look?
p
Because of dataset privacy issues, I can only share with you the screenshot. I rerun and the same error still occurred
j
Are you on the latest version (1.6.0)?