嗨,我正在尝试使用python FbProphet模型生成时间序列预测模型。在执行过程中出现以下错误。以下是我收到的错误。
-------------------------------------------------- ---------------------------- TypeError跟踪(最近的呼叫 最后)在() 36 f14.set_figheight(5) 37 f14.set_figwidth(15) ---> 38图= model_prop.plot(预测,ax = ax14)
C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ fbprophet \ forecaster.py 在plot(自我,fcst,ax,不确定性,plot_cap,xlabel,ylabel)中1520 return plot(1521 m = self,fcst = fcst,ax = ax, 不确定性=不确定性 -> 1522 plot_cap = plot_cap,xlabel = xlabel,ylabel = ylabel,1523)1524
C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ fbprophet \ plot.py 绘图(m,fcst,ax,不确定性,plot_cap,xlabel,ylabel,figsize) 68无花果= ax.get_figure() 69 fcst_t = fcst ['ds']。dt.to_pydatetime() ---> 70 ax.plot(m.history ['ds']。dt.to_pydatetime(),m.history ['y'],'k。') 71 ax.plot(fcst_t,fcst ['yhat'],ls ='-',c ='#0072B2') 72如果fcst和plot_cap中的'cap':
C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ matplotlib__init __。py在 内部(ax,* args,** kwargs)1853年“ Matplotlib列表!)“%(label_namer,func。 name ),1854年
RuntimeWarning,堆栈级别= 2) -> 1855 return func(ax,* args,** kwargs)1856 1857内部。 doc = _add_data_doc(内部。 doc ,C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ matplotlib \ axes_axes.py 在plot(self,* args,** kwargs)1526 1527中输入行 self._get_lines(* args,** kwargs): -> 1528 self.add_line(line)1529 lines.append(line)1530
C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ matplotlib \ axes_base.py 在add_line(self,line)1930年
line.set_clip_path(self.patch)1931年 -> 1932 self._update_line_limits(line)1933(如果不是)line.get_label():1934 line.set_label('_ line%d'% len(self.lines))C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ matplotlib \ axes_base.py 在_update_line_limits(self,line)1952中 给定行的数据限制,更新self.dataLim。 1953
“” -> 1954 path = line.get_path()如果path.vertices.size == 0:1956返回1955C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ matplotlib \ lines.py在 get_path() 949“”“ 950如果self._invalidy或self._invalidx: -> 951 self.recache() 第952章 953
C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ matplotlib \ lines.py在 重新缓存(总是) 第650章 651 xconv = self.convert_xunits(self._xorig) -> 652 x = _to_unmasked_float_array(xconv).ravel() 第653章 654 x = self._x
C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ matplotlib \ cbook__init __。py 在_to_unmasked_float_array(x)2048中返回np.ma.asarray(x, float).filled(np.nan)2049否则: -> 2050返回np.asarray(x,float)2051 2052
C:\ Program Files(x86)\ Microsoft Visual Studio \ Shared \ Anaconda3_64 \ lib \ site-packages \ numpy \ core \ numeric.py asarray(a,dtype,order) 536 537“”“ -> 538返回数组(a,dtype,copy = False,order = order) 539 540
TypeError:float()参数必须是字符串或数字,而不是 'datetime.datetime'
以下是我尝试过的代码
for i in Brokers:
series2=TimeSeriesData[TimeSeriesData.BrokerName == b][['Gross_Premium']]
series2['ds']=Broker_Clusters.index
series2['y'] = series2.Gross_Premium
series2=series2.drop('Gross_Premium',axis=1)
series2['ds'] = series2['ds'].apply(str)
#divide into train and validation set
train = series2[:int(0.7*(len(series2)))]
test = series2[int(0.7*(len(series2))):]
model_prop=Prophet()
model_prop.fit(train)
forecast = model_prop.predict(test)
# Plot the forecast
f14, ax14 = plt.subplots(1)
f14.set_figheight(5)
f14.set_figwidth(15)
fig = model_prop.plot(forecast, ax=ax14)
以下是我的测试数据集
ds y
Date
2016-03-15 2016-03-15 00:00:00 0.00000
2016-04-15 2016-04-15 00:00:00 180534.47230
2016-05-15 2016-05-15 00:00:00 0.00000
2016-06-15 2016-06-15 00:00:00 2545.12000
2016-07-15 2016-07-15 00:00:00 0.00000
2016-08-15 2016-08-15 00:00:00 0.00000
2016-09-15 2016-09-15 00:00:00 0.00000
2016-10-15 2016-10-15 00:00:00 20637.88000
2016-11-15 2016-11-15 00:00:00 0.00000
2016-12-15 2016-12-15 00:00:00 0.00000
2017-01-15 2017-01-15 00:00:00 0.00000
2017-02-15 2017-02-15 00:00:00 0.00000
2017-03-15 2017-03-15 00:00:00 8878.08000
2017-04-15 2017-04-15 00:00:00 198174.32710
2017-05-15 2017-05-15 00:00:00 0.00000
2017-06-15 2017-06-15 00:00:00 3458.61875
2017-07-15 2017-07-15 00:00:00 0.00000
2017-08-15 2017-08-15 00:00:00 0.00000
2017-09-15 2017-09-15 00:00:00 0.00000
2017-10-15 2017-10-15 00:00:00 5159.47000
2017-11-15 2017-11-15 00:00:00 0.00000
2017-12-15 2017-12-15 00:00:00 0.00000
答案 0 :(得分:2)
尝试在plot()
通话之前添加以下内容
pd.plotting.register_matplotlib_converters()
它对我有用。
答案 1 :(得分:1)
您的问题是您拥有的日期列中的type
。在您的情况下,它是一个字符串,应该是一个时间戳。
相反:
series2['ds'] = series2['ds'].apply(str)
使用:
series2['ds'] = list(map(lambda x: pd.to_datetime(x), series2['ds']))