atspy icon indicating copy to clipboard operation
atspy copied to clipboard

UnboundLocalError: local variable 'periodocity' referenced before assignment

Open jtfields opened this issue 4 years ago • 3 comments

I'm running AtsPy with a date-time index with frequency of A-DEC and receive a "periodicity' error. Could this be due to the "season = infer_from_data" not supporting A-DEC? If so, is there another option to manually specify a season value?

DatetimeIndex(['1997-12-31', '1998-12-31', '1999-12-31', '2000-12-31', '2001-12-31', '2002-12-31', '2003-12-31', '2004-12-31', '2005-12-31', '2006-12-31', '2007-12-31', '2008-12-31', '2009-12-31', '2010-12-31', '2011-12-31', '2012-12-31', '2013-12-31', '2014-12-31', '2015-12-31', '2016-12-31', '2017-12-31'], dtype='datetime64[ns]', freq='A-DEC')

<class 'pandas.core.frame.DataFrame'> The data has been successfully parsed by infering a frequency, and establishing a 'Date' index and 'Target' column. 15 An insample split of training size 15 and testing size 6 has been constructed

UnboundLocalError Traceback (most recent call last) in () 10 model_list=["Prophet"] 11 am = AutomatedModel(df = zillowByZip, model_list=model_list, season="infer_from_data",forecast_len=20) ---> 12 forecast_in, performance = am.forecast_insample() 13 forecast_out = am.forecast_outsample() 14 all_ensemble_in, all_ensemble_out, all_performance = am.ensemble(forecast_in, forecast_out)

4 frames /usr/local/lib/python3.6/dist-packages/atspy/init.py in infer_periodocity(train) 145 periodocity = 1000 146 --> 147 return periodocity 148 149 def select_seasonality(train, season):

UnboundLocalError: local variable 'periodocity' referenced before assignment

jtfields avatar Apr 28 '20 14:04 jtfields

'root cause - it is not able to infer_freq of the date column for that reason it is not able to satisfy any condition of def time_feature(df,perd): for that reason you are getting above error use that command to see infer_freq pd.infer_freq(df.index)

Solution -

  1. goto /usr/local/lib/python3.6/dist-packages/atspy/init.py
  2. change elif perd=="W": to elif perd=="W-SUN": if you are doing weekly prediction.
  3. restart your runtime or Jupyter notebook.
  4. re-run entire notebook again and the issue will get fixed.
  5. Done

@Team can we connect as def time_feature(df,perd): function is not generic.

rahul4tripathi2 avatar Jun 08 '20 18:06 rahul4tripathi2

@rahul4tripathi2 Thanks I always appreciate when someone comes with solutions. Would you be able to submit a pull request when you see changes needed? Best, Derek.

firmai avatar Jun 08 '20 19:06 firmai

Below are the changes which i think should be done in infer_periodicity function. I'm not sure it is optimal code or not. https://pandas.pydata.org/pandas-docs/version/0.9.1/timeseries.html

def infer_periodocity(train): perd = pd.infer_freq(train.index) if perd in ["MS","M","BM","BMS"]: periodocity = 12 elif perd in ["BH","H"]: periodocity = 24 elif perd=="B": periodocity = 5 elif perd=="D": periodocity = 7 elif perd in ["W-SUN","W-MON","W-TUE","W-WED","W-THU","W-FRI","W-SAT"]: periodocity = 52 elif perd in ["Q","QS","BQ","BQS"]: periodocity = 4 elif perd in ["A","BA","AS","BAS"]: periodocity = 10 elif perd in ["T","min"]: periodocity = 60 elif perd=="S": periodocity = 60 elif perd in ["L","ms"]: periodocity = 1000 elif perd in ["U","us"]: periodocity = 1000 elif perd=="N": periodocity = 1000

return periodocity

rahul4tripathi2 avatar Jun 08 '20 20:06 rahul4tripathi2