我使用ARIMA来预测数据集未来5天的金额(Amt)。条形图需要显示每个时间值的数量的串联,然后是未来的预测。
一个dataframe的例子,它比这个大得多:
CCY Pair Time Amt
0 GBPUSD 13/05/2023 1000
1 EURUSD 13/05/2023 2000
2 EURUSD 14/05/2023 3000
3 EURUSD 14/05/2023 5000
4 GBPEUR 15/05/2023 4000
字符串
当我尝试绘制模型时,下面的代码给出了以下错误:
Traceback (most recent call last):
File "Graphs.py", line 46, in <module>
plt.bar(combined_data.index, combined_data['Amt'])
File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/pyplot.py", line 2439, in bar
return gca().bar(
^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/__init__.py", line 1442, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/axes/_axes.py", line 2460, in bar
raise TypeError(f'the dtypes of parameters x ({x.dtype}) '
TypeError: the dtypes of parameters x (object) and width (float64) are incompatible
型
代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA
# The "Time" column contains the time periods, and "Amt" contains the values
df = pd.read_csv("Data.csv")
# Convert the "Time" column to a datetime type
df['Time'] = pd.to_datetime(df['Time'], dayfirst=True)
# Set the "Time" column as the index
df.set_index('Time', inplace=True)
# Sort the DataFrame by the index
df.sort_index(inplace=True)
df.index = pd.to_datetime(df.index).to_period('D')
# Prepare the data for ARIMA modeling
data = df['Amt']
# Fit the ARIMA model
model = ARIMA(data, order=(1,0,0))
model_fit = model.fit()
# Predict the next x periods
x = 5 # Number of periods to predict
predictions = model_fit.forecast(steps=x)
# Generate the next x bar charts based on the predictions
next_time_periods = pd.date_range(start=df.index.max().to_timestamp() + pd.DateOffset(days=1), periods=x, freq='D') # Generate x future time periods
next_bar_charts = pd.DataFrame({'Time': next_time_periods, 'Amt': predictions}, index=next_time_periods)
# Concatenate current and predicted bar chart data
combined_data = pd.concat([df, next_bar_charts])
# Plot the combined bar chart
plt.figure(figsize=(10, 6))
plt.bar(combined_data.index, combined_data['Amt'])
plt.xlabel('Time')
plt.ylabel('Amt')
plt.xticks(rotation=45) # Rotate x-axis labels for better readability
plt.show()
型
1条答案
按热度按时间tkqqtvp11#
如果你检查
combined_data
索引,你会注意到它的类型是Object
。这是因为next_bar_charts
索引与df
索引的类型不同。条形图的x轴需要采用日期时间格式。你需要做的改变是:1.注解掉行
df.index = pd.to_datetime(df.index).to_period('D')
1.在创建
next_time_periods
时删除.to_timestamp()
,因为变更#1不再需要此操作1.创建
combined_data
后,添加以下行:combined_data.index=pd.to_datetime(combined_data.index)
将索引转换为datetime类型这应该可以解决您的问题。一些随机数据的输出如下...
的数据