Python Forum
How to replace entries in a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to replace entries in a list?
#1
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])
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get unique entries in a list and the count of occurrence james2009 5 2,992 May-08-2022, 04:34 AM
Last Post: ndc85430
  How to efficiently average same entries of lists in a list xquad 5 2,124 Dec-17-2021, 04:44 PM
Last Post: xquad
  find an replace not in all entries Monsterherz 1 1,950 Mar-01-2021, 03:59 PM
Last Post: BashBedlam
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 2,890 May-10-2020, 05:28 AM
Last Post: deanhystad
  List help to split into single entries paul41 3 2,234 Nov-25-2019, 08:09 AM
Last Post: perfringo
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 3,466 Oct-17-2019, 03:04 PM
Last Post: baquerik
  Replace Items in List. mcmxl22 3 2,771 Oct-07-2019, 05:05 AM
Last Post: Larz60+
  Replace a list or string element with a random one.. pianistseb 3 2,442 May-09-2019, 08:24 AM
Last Post: buran
  Replace element in a nested list nagymusic 4 17,892 Nov-19-2018, 08:03 PM
Last Post: nilamo
  clean up list elements and replace metalray 7 4,239 Aug-30-2018, 08:13 AM
Last Post: metalray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020