我使用facebook Prophet预测库对未来价格进行建模,我可以获得明年的所有数据,但明天的价格会停滞不前。
这是预测代码:
m = Prophet(
seasonality_mode="multiplicative",
yearly_seasonality=True
)
m.fit(df_crypto_market_chart_exploded)
future = m.make_future_dataframe(periods = 365)
forecast = m.predict(future)
forecast_result_tail = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
#print('n--- selected: FORECAST RESULT TAIL ---')
#print(forecast_result_tail)
forecast_result_head = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head()
print('n--- selected: FORECAST RESULT HEAD ---')
print(forecast_result_head)
next_day = (datetime.today() + timedelta(days=3)).strftime('%Y-%m-%d')
print('n--- selected: FORECAST DS TYPE ---')
print(forecast['ds'].dtype)
tomorrow_result = forecast['ds'].dt.date == datetime.today() + timedelta(days=1)
print('n--- selected: TOMORROW RESULTS ---')
print(tomorrow_result.head())
forecast[tomorrow_result]['yhat'].item()
**编辑-**我根据建议的解决方案修改了代码,但我仍然遇到下面列出的numpy错误
--- selected: FORECAST RESULT HEAD ---
ds yhat yhat_lower yhat_upper
0 2017-06-02 00:00:00.000 2359.464229 605.624033 4037.385324
1 2017-06-03 00:00:00.000 2371.962255 661.690821 4155.470592
2 2017-06-04 00:00:00.000 2382.640816 683.686292 4249.680911
3 2017-06-05 00:00:00.000 2387.433948 570.835239 4240.934785
4 2017-06-06 23:29:55.701 2226.178108 344.391258 4062.279165
--- selected: FORECAST DS TYPE ---
datetime64[ns]
--- selected: TOMORROW RESULTS ---
0 False
1 False
2 False
3 False
4 False
Name: ds, dtype: bool
Traceback (most recent call last):
File "d:/Projects/price_prediction.py", line 69, in <module>
forecast[tomorrow_result]['yhat'].item()
File "C:Usersabcanaconda3envsvenvlibsite-packagespandascorebase.py", line 331, in item
raise ValueError("can only convert an array of size 1 to a Python scalar")
ValueError: can only convert an array of size 1 to a Python scalar
pandas.dataframe
可以将ds
保留为datetime
对象,并且还可以比较hours, minutes, seconds
。
使用.strftime()
也不起作用,因为它很可能不会比较datetime
和string
,但如果它可以比较它们,那么它仍然会使用hours, minutes, seconds
作为ds
中的值。
您可能需要将.dt.date
用于dataframe
,将.date()
用于单个datetime
以仅比较日期。
最小示例
import datetime
import time
one_day = datetime.timedelta(days=1)
tomorrow_1 = datetime.datetime.today() + one_day
time.sleep(2) # to create values with different seconds
tomorrow_2 = datetime.datetime.today() + one_day
print('tomorrow_1:', tomorrow_1, '| strftime:', tomorrow_1.strftime('%Y-%m-%d'))
print('tomorrow_2:', tomorrow_2, '| strftime:', tomorrow_2.strftime('%Y-%m-%d'))
print(' dt == dt :', tomorrow_1 == tomorrow_2)
print(' dt == dt.strftime() :', tomorrow_1 == tomorrow_2.strftime('%Y-%m-%d'))
print('dt.strftime() == dt.strftime() :', tomorrow_1.strftime('%Y-%m-%d') == tomorrow_2.strftime('%Y-%m-%d'))
print(' dt.date() == dt.date() :', tomorrow_1.date() == tomorrow_2.date())
# ------------------------------------------
import pandas as pd
df = pd.DataFrame({
'ds': [tomorrow_1, tomorrow_2]
})
print('n--- dtypes ---')
print(df.dtypes)
print('n--- selected: dt == dt ---')
selected = df[ df['ds'] == tomorrow_1 ]
print(selected)
print('n--- selected: dt == dt.strftime() ---')
selected = df[ df['ds'] == tomorrow_1.strftime('%Y-%m-%d') ]
print(selected)
print('n--- selected: dt.strftime() == dt.strftime() ---')
selected = df[ df['ds'].dt.strftime('%Y-%m-%d') == tomorrow_1.strftime('%Y-%m-%d') ]
print(selected)
print('n--- selected: dt.date() == dt.date() ---')
selected = df[ df['ds'].dt.date == tomorrow_1.date() ]
print(selected)
结果:
tomorrow_1: 2021-07-11 17:31:50.259787 | strftime: 2021-07-11
tomorrow_2: 2021-07-11 17:31:52.260139 | strftime: 2021-07-11
dt == dt : False
dt == dt.strftime() : False
dt.strftime() == dt.strftime() : True
dt.date() == dt.date() : True
--- dtypes ---
ds datetime64[ns]
dtype: object
--- selected: dt == dt ---
ds
0 2021-07-11 17:31:50.259787
--- selected: dt == dt.strftime() ---
Empty DataFrame
Columns: [ds]
Index: []
--- selected: dt.strftime() == dt.strftime() ---
ds
0 2021-07-11 17:31:50.259787
1 2021-07-11 17:31:52.260139
--- selected: dt.date() == dt.date() ---
ds
0 2021-07-11 17:31:50.259787
1 2021-07-11 17:31:52.260139
编辑:
其他示例
import datetime
one_day = datetime.timedelta(days=1)
today = datetime.datetime.today()
yesterday = today - one_day
tomorrow = today + one_day
two_days_later = today + 2*one_day
# --- generate some data ---
import pandas as pd
forecast = pd.DataFrame({
'ds': [yesterday, today, tomorrow, two_days_later],
'yhat': [111, 222, 333, 444],
})
print('- forecast -')
print(forecast)
# --- find tomorrow ---
tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
mask = ( forecast['ds'].dt.date == tomorrow.date() )
print('- mask -')
print(mask)
selected = forecast[ mask ]
print('- selected -')
print(selected['yhat'].item())