Python Forum

Full Version: How to replace entries in a list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Python Users,

Please, help me with the following issue. I have two lists: train_X and train_Y. What I want to do is to create a new list that will contain forecasts (F) from the following formula: F_(t+1) = alpha*train_Y + (1-alpha)* F_t. Note: the first entry in Forecast(F) should equal to first entry in train_Y. Look at this example:

Alpha = 0.5

Train_Y  Forecast
10           10
20            10 = 10*0.5 + 10*(1-0.5)
30           15 = 20*0.5 + (1-0.5)*10
40            22.5 = 30*0.5 +(1-0.5)*15

So far I have the following code (but it does not work and I am stuck): train_X is indexing of entries in train_y

train_X = [1,2,3,4,5]
train_Y = [10, 20, 30, 40, 50]

alpha = int(input("Input alpha: "))

forecast = []
for x in range(0, len(train_X)+1):
   if x==0:
       forecast.append(train_Y[0])
   else:
       forecast.append(alpha*train_Y + (1 - alpha) * forecast[x-1])
First, for alpha you need to use float() instead of int(). Otherwise you can't enter 0.5. Second, you need to index train_Y in the last line to get one value out of it. I'm assuming that index should be train_Y[x-1], to match the index of forecast.