python - IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引"
问题描述
我正在尝试使用函数将 3 个月的简单移动平均线作为需求系列(d)的预测,但出现以下错误!
def move_avg(d,extra_periods=1,n=3):
# transfer the input in to numpy array
d=np.array(d)
# historical period length
cols=len(d)
# append np.nan in to the demand array to cover future period
d=np.append(d[np.nan]*extra_periods)
# Define the forecast array
f= np.full(cols+extra_periods,np.nan)
# create all the t+1 forecast untill end of historical period
for t in range (n,cols+1):
f[t]=np.mean(d[t-n:t])
# forecast for all extra periods
f[cols+1:]=f[t]
# Return the dataframe with demand , forecast and error
df=pd.DataFrame.from_dict({"Demand":d,"Forecast":f,"Error":d-f})
return df
d=[37,60,85,112,132,145,179,198,212,232]
df=moving_avg(d,extra_periods=1,n=3)
IndexError:只有整数、切片 (:)、省略号 (...)、numpy.newaxis (None) 和整数或布尔数组是有效的索引
解决方案
推荐阅读
- sql - SQL 查询,返回 field_w,其中 field_x 为最大值——按 field_y、field_z 分组
- python - 图像质量机器学习
- typescript - 为什么强制类型转换的类型安全性低于显式类型变量声明?
- c - 这段代码、数组和指针的输出是什么
- mongodb - MongoDB:$project 和 $filter,包括所有其他属性
- python - 一起运行 Tkinter 窗口和 PysTray 图标
- generics - 错误 Kotlin:类型不匹配:推断类型为 T?但没有预料到
- r - 调整雅虎股票数据网络抓取以循环日期
- python-3.x - 用于 Python 的简易前端/GUI
- java - Spring Boot 应用程序无法通过 VPN 连接到服务器