Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop row and column + 1
#1
Hi everyone,

I've made an empty 24,31 (24 columns and 31 rows if I've done the numbers wrong) matrix with all 0. I've also created a loop that a model out puts a 1,8 array. I'd like the model output of 1,8 to be the first column at 0,0 in the the empty matrix and then the second loop model output to input into the empty matrix at 1,1 and the third output to be 2,2 etc. How do I create a the loop to do that? My append statement is inputting into the matrix how I want it to.

for i in range(24):


  LSTM_matrix.append(y_pred)
There more code in-between which is the model. Do I need to add another variable to my loop and add it to the append?

thanks for any help
Reply
#2
What is y_pred?

Please post reproducible code, with 2 lines it is impossible to see what you are trying to do.
Reply
#3
 #build a 0 matrix to populate with LSTM output
def build_matrix(rows, cols):
    matrix = []

    for r in range(0, rows):
        matrix.append([0 for c in range(0, cols)])

    return matrix

LSTM_matrix = build_matrix(31, 24)
#i need to iterate over the dataframe rows one step at a time
for i in range(24):
    #24 is based off 0 indexing and looping up until dec-2017
    df2 = df.iloc[:88+i]
    number_column1 = df2.loc[:,'NZ']
    number_column2 = df2.loc[:,'Unemployment']
    number_column3 = df2.loc[:,'CPI']
    number_column4 = df2.loc[:,'Export prices']
    number_column5 = df2.loc[:,'Import prices']

    numbers1 = number_column1.values
    numbers2 = number_column2.values
    numbers3 = number_column3.values
    numbers4 = number_column4.values
    numbers5 = number_column5.values

    # convert to [rows, columns] structure
    numbers1 = numbers1.reshape((len(numbers1), 1))
    numbers2 = numbers2.reshape((len(numbers2), 1))
    numbers3 = numbers3.reshape((len(numbers3), 1))
    numbers4 = numbers4.reshape((len(numbers4), 1))
    numbers5 = numbers5.reshape((len(numbers5), 1))
    
    # horizontally stack columns
    dataset = hstack((numbers2, numbers3,numbers4,numbers5, numbers1))

    # covert into input/output
    X, y = split_sequences(dataset, n_steps_in, n_steps_out)
    # the dataset knows the number of features, e.g. 2
    n_features = X.shape[2]
    
    # define model
    model = Sequential()
    model.add(LSTM(120, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
    model.add(LSTM(120, activation='relu'))
    model.add(Dense(n_steps_out))
    model.compile(optimizer='adam', loss='mse')
    
    # fit model
    history = model.fit(X, y, epochs=200, verbose=0)
    
    x_input = df.iloc[80+i:88+i]
    test_column2 = x_input.loc[:,'Unemployment']
    test_column3 = x_input.loc[:,'CPI']
    test_column4 = x_input.loc[:,'Export prices']
    test_column5 = x_input.loc[:,'Import prices']



    test2 = test_column2.values
    test3 = test_column3.values
    test4 = test_column4.values
    test5 = test_column5.values


    # convert to [rows, columns] structure
    test2 = test2.reshape((len(test2), 1))
    test3 = test3.reshape((len(test3), 1))
    test4 = test4.reshape((len(test4), 1))
    test5 = test5.reshape((len(test5), 1))


    # horizontally stack columns
    x_input = hstack((test2, test3,test4,test5))
    
    #make prediction
    x_input = x_input.reshape((1, n_steps_in, n_features))
    y_pred = model.predict(x_input, verbose=0)

    
    y_pred = y_pred[0:,8:16]
    y_pred = y_pred.transpose()
    
    LSTM_matrix.append(y_pred)
    print(y_pred)
Reply
#4
df2 = df.iloc[:88+i]

What is df? pandas DataFrame? Where is it defined?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with For Loop logic for reading docx Table column data vintysaw 2 3,902 Jan-10-2020, 06:36 AM
Last Post: vintysaw
  Loop through folder of Excel Files and extract single column fioranosnake 2 4,532 Oct-28-2019, 05:19 PM
Last Post: fioranosnake
  change array column values without loop khalidreemy 2 3,792 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  Slow loop due to row & column consolidation Etrigan 0 1,933 Nov-27-2018, 05:06 AM
Last Post: Etrigan

Forum Jump:

User Panel Messages

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