1

I have a dataset with three inputs and trying to predict next value of X1 with the combination of previous inputs values.

My three inputs are X1, X2, X3, X4.

So here I am trying to predict next future value of X1. To predict the next X1 these four inputs combination affect with:

X1 + X2 - X3 -X4

I wrote this code inside the class. Then I wrote the code to run the lstm . After that I wrote the code for predict value. Then it gave me this error. Can anyone help me to solve this problem?

my code:

def model_predict(data):
pred=[]
for index, row in data.iterrows():
    val = row['X1']
    if np.isnan(val):
        data.iloc[index]['X1'] = pred[-1]
        row['X1'] = pred[-1]
        f = row['X1','X2','X3','X4']
        s = row['X1'] - row['X2'] + row['X3'] -row['X4']
        val = model.predict(s)
        pred.append(val)
return np.array(pred)

After lstm code then I wrote the code for predict value:

pred = model_predict(x_test_n)

Gave me this error:

  ` ---> 5 pred = model_predict(x_test_n)

    def model_predict(data):
     pred=[]
  -->for index, row in data.iterrows():
        val = row['X1']
        if np.isnan(val):`     
   AttributeError: 'numpy.ndarray' object has no attribute 'iterrows'
6
  • This error speaks for itself, you need to transform numpy.ndarray to pandas.DataFrame Commented Jul 27, 2019 at 19:39
  • @Michael O. 3 First of all thank you for the fast reply. I didn't get what you are saying. Can you explain little bit more with a code, if you are okay? Commented Jul 27, 2019 at 19:43
  • What is x_test_n, I guess it is numpy.ndarray? If you want to handle it as Pandas dataframe, you need to convert it first, for example, like it is described here: stackoverflow.com/questions/20763012/… Commented Jul 27, 2019 at 20:02
  • @MichaelO. It's my test set , to predict next value Commented Jul 28, 2019 at 16:47
  • @MichaelO. I wrote it as inside the class as pd dataframe and it gave me an error and code " data = pd.DataFrame(data=data[1:,1:],index=data[1,:])" , error "Must pass 2-d input" Commented Jul 28, 2019 at 17:05

1 Answer 1

0

Apparenty, data argument of your function is a Numpy array, not a DataFrame. Data, as a np.ndarray, has also no named columns.

One of possible solutions, keeping the argument as np.ndarray is:

  • iterate over rows of this array using np.apply_along_axis(),
  • refer to columns by indices (instead of names).

Another solution is to create a DataFrame from data, setting proper column names and iterate on its rows.

One of possible solutions how to write the code without DataFrame

Assume that data is a Numpy table with 4 columns, containing respectively X1, X2, X3 and X4:

[[ 1  2  3  4]
 [10  8  1  3]
 [20  6  2  5]
 [31  3  3  1]]

Then your function can be:

def model_predict(data):
    s = np.apply_along_axis(lambda row: row[0] + row[1] - row[2] - row[3],
        axis=1, arr=data)
    return model.predict(s)

Note that:

  • s - all input values to your model - can be computed in a single instruction, calling apply_along_axis for each row (axis=1),
  • the predictions can also be computed "all at once", passing a Numpy vector - just s.

For demonstration purpose, compute s and print it.

Sign up to request clarification or add additional context in comments.

10 Comments

So I have to write it in inside the class isn't if? If It is I wrote it and it gave me an error " Must pass 2-d input" ,, I wrote the code is " data = pd.DataFrame(data=data[1:,1:],index=data[1,:])"
You wrote data=data[1:,1:],index=data[1,:]. It actually means: 1. Drop the first row and column from data. 2. The index should be just the same as the first column of your DataFrame. Do you really want this? I also noticed that you failed to pass column names, so default names will be consecutive numbers from 0 and your code still will break on an attempt to access a non-existing column. Another hint: Error message shows that you passed only a vector not an array. Make a test printout of your data and look whether it contains all needed columns and more than 1 row.
Thank you for the fast reply, I can understand what you are saying. I am actually stuck with this code , and I can't go forward with this error. I am not having proper idea that how to change the code to run this. If you are okay to help me with the code , it will be really helpful to me go forward.
Before you call model_predict, make a printout of data and add a sample of it to your question. Then: 1. Decide which columns contain values for X1, X2, X3 and X4. 2. In model_predict create a DataFrame from data, passing column names (maybe the default index will be enough). 3. The loop using iterrows should run on rows from just this DataFrame.
Two questions to your code: 1. Your loop contains f = row['X1','X2','X3','X4']. What is the use of this variable? 2. What is the content (and type) of model?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.