价格预测适用于未来一年,但不适用于明天

myss37ts  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(310)

我使用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:\Users\abc\anaconda3\envs\venv\lib\site-packages\pandas\core\base.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
vyu0f0g1

vyu0f0g11#

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())

相关问题