I'm very new, and attempting to teach myself Python through online resources.
I've attempted the following code I found online to conduct a Single Exponential Smoothing Analysis on my time series data:
# Simple Exponential Smoothing
fit1 = SimpleExpSmoothing(Data).fit(smoothing_level=0.2,optimized=False)
fcast1 = fit1.forecast(12).rename(r'$\alpha=0.2$')
# plot
fcast1.plot(marker='o', color='blue', legend=True)
fit1.fittedvalues.plot(marker='o', color='blue')
fit2 = SimpleExpSmoothing(Data).fit(smoothing_level=0.6,optimized=False)
fcast2 = fit2.forecast(12).rename(r'$\alpha=0.6$')
# plot
fcast2.plot(marker='o', color='red', legend=True)
fit2.fittedvalues.plot(marker='o', color='red')
fit3 = SimpleExpSmoothing(Data).fit()
fcast3 = fit3.forecast(12).rename(r'$\alpha=%s$'%fit3.model.params['smoothing_level'])
# plot
fcast3.plot(marker='o', color='green', legend=True)
fit3.fittedvalues.plot(marker='o', color='green')
plt.show()
This code returns the error:
"Pandas data cast to numpy dtype of object. Check input data with np.asarray(data)."
I have researched online and attempted to create dummy variables within my dataset, however the result remains the same.
Month |
Product Key |
Income |
01-Apr-18 |
A100 |
97426.50 |
|
01-Apr-18 |
A101 |
8289.05 |
|
01-Apr-18 |
A102 |
0.00 |
|
Above is a 3 row snippet of the dataset, could it be the structure of the data that causes the forecast/SES code to result in an error?
Entire ValueError code:
ValueError Traceback (most recent call last) <ipython-input-6-c73edf2e1e6d> in <module> 1 # Simple Exponential Smoothing ----> 2 fit1 = SimpleExpSmoothing(Data).fit(smoothing_level=0.2,optimized=False) 3 fcast1 = fit1.forecast(12).rename(r'$\alpha=0.2$') 4 # plot 5 fcast1.plot(marker='o', color='blue', legend=True)
~\Anaconda3\lib\site-packages\statsmodels\tsa\holtwinters.py in __init__(self, endog) 1005 1006 def __init__(self, endog): -> 1007 super(SimpleExpSmoothing, self).__init__(endog) 1008 1009 def fit(self, smoothing_level=None, optimized=True, start_params=None,
~\Anaconda3\lib\site-packages\statsmodels\tsa\holtwinters.py in __init__(self, endog, trend, damped, seasonal, seasonal_periods, dates, freq, missing) 484 seasonal_periods=None, dates=None, freq=None, missing='none'): 485 super(ExponentialSmoothing, self).__init__( --> 486 endog, None, dates, freq, missing=missing) 487 if trend in ['additive', 'multiplicative']: 488 trend = {'additive': 'add', 'multiplicative': 'mul'}[trend]
~\Anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py in __init__(self, endog, exog, dates, freq, missing, **kwargs) 47 missing='none', **kwargs): 48 super(TimeSeriesModel, self).__init__(endog, exog, missing=missing, ---> 49 **kwargs) 50 51 # Date handling in indexes ~\Anaconda3\lib\site-packages\statsmodels\base\model.py in __init__(self, endog, exog, **kwargs) 214 215 def __init__(self, endog, exog=None, **kwargs): --> 216 super(LikelihoodModel, self).__init__(endog, exog, **kwargs) 217 self.initialize() 218 ~\Anaconda3\lib\site-packages\statsmodels\base\model.py in __init__(self, endog, exog, **kwargs) 66 hasconst = kwargs.pop('hasconst', None) 67 self.data = self._handle_data(endog, exog, missing, hasconst, ---> 68 **kwargs) 69 self.k_constant = self.data.k_constant 70 self.exog = self.data.exog
~\Anaconda3\lib\site-packages\statsmodels\base\model.py in _handle_data(self, endog, exog, missing, hasconst, **kwargs) 89 90 def _handle_data(self, endog, exog, missing, hasconst, **kwargs): ---> 91 data = handle_data(endog, exog, missing, hasconst, **kwargs) 92 # kwargs arrays could have changed, easier to just attach here 93 for key in kwargs:
~\Anaconda3\lib\site-packages\statsmodels\base\data.py in handle_data(endog, exog, missing, hasconst, **kwargs) 633 klass = handle_data_class_factory(endog, exog) 634 return klass(endog, exog=exog, missing=missing, hasconst=hasconst, --> 635 **kwargs) ~\Anaconda3\lib\site-packages\statsmodels\base\data.py in __init__(self, endog, exog, missing, hasconst, **kwargs) 74 self.orig_endog = endog 75 self.orig_exog = exog ---> 76 self.endog, self.exog = self._convert_endog_exog(endog, exog) 77 78 self.const_idx = None
~\Anaconda3\lib\site-packages\statsmodels\base\data.py in _convert_endog_exog(self, endog, exog) 473 exog = exog if exog is None else np.asarray(exog) 474 if endog.dtype == object or exog is not None and exog.dtype == object: --> 475 raise ValueError("Pandas data cast to numpy dtype of object. " 476 "Check input data with np.asarray(data).") 477 return super(PandasData, self)._convert_endog_exog(endog, exog)
ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).