首页 > 解决方案 > 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) 和整数或布尔数组是有效的索引

标签: pythonnumpydata-scienceforecasting

解决方案


推荐阅读