我在预测有 17 行的数据帧,但它会引发ValueError: Length of pass values is 17, index means 1。如何预测未来数据帧的任何长度?
运行在 python=3.6.8,fbprophet 版本=0.3 然后我只预测 1 行的数据帧。有用。我改变了增长,n_changepoints,changepoint_range,weekly_seasonality,daily_seasonality。它不起作用。如果模型适合不同的数据,长度限制就会改变。
def first_nonzero_index(serial):
# drop 0 ahead
for i in serial.index:
if serial[i] != 0:
return i
return None
def model_data_convert(sale_series, has_cap = False, has_floor = False):
item_id = sale_series.name
category_id = items.at[item_id, 'item_category_id']
start_month = first_nonzero_index(sale_series)
data = sale_series[start_month:].to_frame('y').reset_index()
data.columns = ['ds', 'y']
end_time = pd.Period('2015-10', 'M')
start_time = pd.Period('2013-01') + start_month
data['ds'] = pd.period_range(start_time.strftime('%Y%m'), end_time.strftime('%Y%m'), freq='M').to_timestamp(how = 'E')
if has_cap:
data['cap'] = catergory_cap.loc[category_id][start_month:].values
if has_floor:
data['floor'] = catergory_floor.loc[category_id][start_month:].values
return data
data = common_items_month_sales_without_seasonal.loc[38]
data = model_data_convert(data, has_cap=True, has_floor=True)
m = fbprophet.Prophet(n_changepoints=5,changepoint_range=0.5)
m.fit(data)
future = m.make_future_dataframe(periods=1, freq='M')
future['cap'] = data['cap']
future['floor'] = data['floor']
future = future.fillna(method='ffill')
forecast = m.predict(future)
data
ds y cap floor
0 2014-07-31 23:59:59.999999999 4.075182 72 0
1 2014-08-31 23:59:59.999999999 1.018694 43 0
2 2014-09-30 23:59:59.999999999 6.059121 35 0
3 2014-10-31 23:59:59.999999999 3.514318 258 0
4 2014-11-30 23:59:59.999999999 9.382967 57 0
5 2014-12-31 23:59:59.999999999 7.302505 99 0
6 2015-01-31 23:59:59.999999999 6.587640 47 0
7 2015-02-28 23:59:59.999999999 3.985545 55 0
8 2015-03-31 23:59:59.999999999 0.999565 78 0
9 2015-04-30 23:59:59.999999999 0.000000 61 0
10 2015-05-31 23:59:59.999999999 4.026230 47 0
11 2015-06-30 23:59:59.999999999 2.715869 38 0
12 2015-07-31 23:59:59.999999999 5.093977 53 0
13 2015-08-31 23:59:59.999999999 7.130856 44 0
14 2015-09-30 23:59:59.999999999 2.423648 37 0
15 2015-10-31 23:59:59.999999999 0.000000 39 0
future
ds cap floor
0 2014-07-31 23:59:59.999999999 72.0 0.0
1 2014-08-31 23:59:59.999999999 43.0 0.0
2 2014-09-30 23:59:59.999999999 35.0 0.0
3 2014-10-31 23:59:59.999999999 258.0 0.0
4 2014-11-30 23:59:59.999999999 57.0 0.0
5 2014-12-31 23:59:59.999999999 99.0 0.0
6 2015-01-31 23:59:59.999999999 47.0 0.0
7 2015-02-28 23:59:59.999999999 55.0 0.0
8 2015-03-31 23:59:59.999999999 78.0 0.0
9 2015-04-30 23:59:59.999999999 61.0 0.0
10 2015-05-31 23:59:59.999999999 47.0 0.0
11 2015-06-30 23:59:59.999999999 38.0 0.0
12 2015-07-31 23:59:59.999999999 53.0 0.0
13 2015-08-31 23:59:59.999999999 44.0 0.0
14 2015-09-30 23:59:59.999999999 37.0 0.0
15 2015-10-31 23:59:59.999999999 39.0 0.0
16 2015-11-30 23:59:59.999999999 39.0 0.0
Traceback (most recent call last):
File "<input>", line 9, in <module>
File "C:\Users\***\.conda\envs\Data\lib\site-packages\fbprophet\forecaster.py", line 1042, in predict
intervals = self.predict_uncertainty(df)
File "C:\Users\***\.conda\envs\Data\lib\site-packages\fbprophet\forecaster.py", line 1244, in predict_uncertainty
sim_values = self.sample_posterior_predictive(df)
File "C:\Users\***\.conda\envs\Data\lib\site-packages\fbprophet\forecaster.py", line 1208, in sample_posterior_predictive
s_m=component_cols['multiplicative_terms'],
File "C:\Users\***\.conda\envs\Data\lib\site-packages\fbprophet\forecaster.py", line 1276, in sample_model
Xb_a = np.matmul(seasonal_features.values, beta * s_a) * self.y_scale
File "C:\Users\***\.conda\envs\Data\lib\site-packages\pandas\core\series.py", line 735, in __array_wrap__
copy=False).__finalize__(self)
File "C:\Users\***\.conda\envs\Data\lib\site-packages\pandas\core\series.py", line 249, in __init__
.format(val=len(data), ind=len(index)))
ValueError: Length of passed values is 17, index implies 1