indexError:只有整数,切片(`:`),省略(`...

发布于 2025-01-19 11:19:44 字数 628 浏览 5 评论 0原文

我想实现先知算法并在模型上使用我的数据集。 在拟合过程之后,我在预测过程中遇到了以下错误。 我该如何解决这个问题?

import pandas as pd
import pystan
from prophet import Prophet
df_prophet = df[['date', 'rate']]
train_df = df_prophet[:-5]
train_df.columns = ['ds', 'y']
train_df['ds']= to_datetime(train_df['ds'])
model = Prophet()
model.fit(train_df)
test_df = df_prophet[-5:][['date']]
test_list = to_datetime(test_df.date).values
forecast = model.predict(test_list)

----> 11预测= model.predict(test_list)

indexError:只有整数,切片(),省略(...), numpy.newaxis()和整数或布尔数组是有效的索引

I want to implement prophet algorithm and use my dataset on the model.
After fit process i got following error in predict process.
How can i solve this problem.?

import pandas as pd
import pystan
from prophet import Prophet
df_prophet = df[['date', 'rate']]
train_df = df_prophet[:-5]
train_df.columns = ['ds', 'y']
train_df['ds']= to_datetime(train_df['ds'])
model = Prophet()
model.fit(train_df)
test_df = df_prophet[-5:][['date']]
test_list = to_datetime(test_df.date).values
forecast = model.predict(test_list)

---> 11 forecast = model.predict(test_list)

IndexError: only integers, slices (:), ellipsis (...),
numpy.newaxis (None) and integer or boolean arrays are valid indices

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-01-26 11:19:44

这是一个很好的尝试。您只需要进行一些调整。

  1. 通过传递 to_datetime(test_df.date).values ,您正在创建一个 numpy 数组而不是数据帧。 Prophet 需要一个数据帧。
  2. Prophet 模型和预测器需要标记为 dsy 的列,并且您在拆分数据帧后更改列名称,因此您的测试部分没有重命名列。
  3. 您不需要导入 pystan,因为 Prophet 模块已经构建在它的基础上。

试试这个:

import pandas as pd
from prophet import Prophet

df_prophet = df[['Date', 'Volume']]
df_prophet.columns = ['ds', 'y']
train_df = df_prophet[:-5]
train_df['ds']= pd.to_datetime(train_df['ds'])
model = Prophet()
model.fit(train_df)
test_df = df_prophet[-5:][['ds']]
forecast = model.predict(test_df)
forecast

It's a nice attempt. You just need a few tweaks.

  1. By passing to_datetime(test_df.date).values you were creating a numpy array instead of a dataframe. Prophet expects a dataframe.
  2. The Prophet model and predictor expects columns labeled ds and y and you're changing the column names after you split the dataframe, so your test part isn't getting the columns renamed.
  3. You shouldn't need to import pystan since the Prophet module is built on it already.

Try this:

import pandas as pd
from prophet import Prophet

df_prophet = df[['Date', 'Volume']]
df_prophet.columns = ['ds', 'y']
train_df = df_prophet[:-5]
train_df['ds']= pd.to_datetime(train_df['ds'])
model = Prophet()
model.fit(train_df)
test_df = df_prophet[-5:][['ds']]
forecast = model.predict(test_df)
forecast
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文