首页 > 解决方案 > FbProphet 模型给出 TypeError: float() argument must be a string or a number, not 'datetime.datetime'

问题描述

您好我正在尝试使用 python FbProphet 模型生成时间序列预测模型。我在实施过程中遇到以下错误。以下是我收到的错误。

-------------------------------------------------- ------------------------- TypeError Traceback (last last call last) in () 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 in plot(self,fcst,ax,不确定性,plot_cap,xlabel,ylabel)1520 返回图(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 in plot(m,fcst,ax,不确定性,plot_cap,xlabel,ylabel,figsize) 68 fig = 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 in inner(ax, *args, **kwargs) 1853 "Matplotlib 列表!)" % (label_namer, func.name ), 1854 RuntimeWarning, stacklevel
=2) -> 1855 返回 func(ax, *args, **kwargs) 1856 1857 内部。doc = _add_data_doc( inner.doc ,

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\axes_axes.py in plot(self, *args, **kwargs) 1526 1527 for line in self._get_lines(* args, **kwargs): -> 1528 self.add_line(line) 1529lines.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 in _update_line_limits(self, line) 1952 计算给定行的数据限制,更新 self.dataLim . 1953
""" -> 1954 path = line.get_path() 1955 if path.vertices.size == 0: 1956 返回

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\lines.py in get_path(self) 949 """ 950 if self._invalidy or self._invalidx: --> 951 self.recache() 952 返回 self._path 953

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\lines.py in recache(self, always) 650 if always or self._invalidx: 651 xconv = self.convert_xunits( self._xorig) --> 652 x = _to_unmasked_float_array(xconv).ravel() 653 else: 654 x = self._x

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\matplotlib\cbook__init__.py in _to_unmasked_float_array(x) 2048 返回 np.ma.asarray(x, float).filled(np. nan) 2049 else: -> 2050 return np.asarray(x, float) 2051 2052

C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\numpy\core\numeric.py in 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

标签: pythontime-series

解决方案


plot()尝试在通话前添加以下内容

pd.plotting.register_matplotlib_converters()

它对我有用。


推荐阅读