Python Prophet TypeError:arg 必须是列表、元组、一维数组或系列

问题描述 投票:0回答:1

我正在尝试使用 Prophet 来预测 Lululemon 的股价。但是,我在拟合模型时遇到以下错误:

TypeError                                 Traceback (most recent call last)
Cell In[3], line 17
     15 # Fit the data to a Prophet model
     16 model = Prophet()
---> 17 model.fit(lululemon_data)
     19 # Create a dataframe to hold predictions for the next 5 years
     20 future = model.make_future_dataframe(periods=5*365)
TypeError: arg must be a list, tuple, 1-d array, or Series

这是我的代码:

python

import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
from prophet import Prophet

# Download Lululemon stock data
ticker = 'LULU'
lululemon_data = yf.download(ticker, start='2007-07-27')  # Lululemon IPO date

# Prepare the data for Prophet
lululemon_data.reset_index(inplace=True)
lululemon_data = lululemon_data[['Date', 'Close']]
lululemon_data.rename(columns={'Date': 'ds', 'Close': 'y'}, inplace=True)

# Fit the data to a Prophet model
model = Prophet()
model.fit(lululemon_data)

# Create a dataframe to hold predictions for the next 5 years
future = model.make_future_dataframe(periods=5*365)

# Make predictions
forecast = model.predict(future)

# Plot the forecast
fig = model.plot(forecast)
plt.title('Lululemon Stock Price Forecast for Next 5 Years')
plt.xlabel('Date')
plt.ylabel('Closing Price (USD)')
plt.show()

我怀疑问题在于 lululemon_data DataFrame 的结构,因为当我使用 Prophet 提供的示例数据时,代码可以正常工作:

python

df = pd.read_csv('https://raw.githubusercontent.com/facebook/prophet/main/examples/example_wp_log_peyton_manning.csv')
df.head()

我已尝试确保列名称正确重命名为 ds 和 y。

python-3.x arguments typeerror facebook-prophet
1个回答
0
投票

在摆弄你的代码和chatgpt之后,这里有不专业的意见。

对于您的特定设置,数据帧之间的

.columns
存在一些差异。

>>> df.columns
Index(['ds', 'y'], dtype='object')
>>> ld.columns  # lululemon_data
MultiIndex([('ds',     ''),
            ( 'y', 'LULU')],
           names=['Price', 'Ticker'])

我不确定确切的区别是什么,但

yfinance
可能使用它来区分多个股票。来自 yfinance.download
文档

多级索引:布尔

可选。总是返回多索引数据帧?默认为 True

我将

multi_level_index
切换为
False
,这似乎修复了您的代码。

lululemon_data = yf.download(ticker, start='2007-07-27', multi_level_index=False)  # Lululemon IPO date

对于其他获取底层库无法输出适用类型的情况,也许这个线程可以帮助压平

MultiIndex

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.