IndexError: допустимыми являются только целые числа, срезы (`:`), многоточие (`…`), numpy.newaxis (`None`) и целые или логические массивы". - PullRequest
0 голосов
/ 30 октября 2019

Я пытаюсь внедрить простое скользящее среднее за 3 месяца в качестве прогноза для ряда спроса (d), используя функцию, и я получаю следующую ошибку!

def moving_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) и целые или логические массивы

...