Here I have a data file and I designed neural network to predict value. I have a three inputs. These three inputs affect to predict value bysubtarcting and adding. If my three inputs are x1,x2,x3 . X1 and X2 add together and that value will subtract with the X3 value and give the predict value. This process will happened in x_train. I wrote a code. But it gives me this error. Can nyone help me to solve this problem? My code is,

data = pd.DataFrame(data,columns=['X1','X2','X3'])
data = data.values
a = []
for i in range(0,len(data)):
    X1 = data[i][0]
b = []
for i in range(0,len(data)):
    X2 = data[i][1]
c = []
for i in range(0,len(data)):
    X3 = data[i][2]
x_train =[]
x =[]
for i in range(0,len(data)):
    x_train.append([a[i] ,b[i], c[i]])
    x.append((a[i] - b[i] + c[i]))
x_train =np.array(x_train)
x = np_array(x)

1 Answer 1


You are facing the error on the line x.append((a[i] - b[i] + c[i])) due to expression a[i] - b[i] + c[i]. Let us see the how it is being calculated. Suppose I have the following data:

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Then the values of a, b and c are:

Out[20]: [[1], [4], [7]]

Out[21]: [[2], [5], [8]]

Out[22]: [[3], [6], [9]]

Let us take i=0 and print a[0]:

Out[23]: [1]

Hence what you are calculating is [1] - [4] + [7] instead you should be calculating 1 - 4 + 7. To do this change the line with error to:

x.append((a[i][0] - b[i][0] + c[i][0]))
  • $\begingroup$ Thank you this is work. But when I add this code to my model it gave me an error like "Input arrays should have the same number of samples as target arrays. Found 125 input samples and 100 target samples." $\endgroup$
    – user59020
    Feb 8, 2019 at 6:02
  • $\begingroup$ It might be because you are running the loops for complete length of data, and your model might be splitting training and test set after that. Try to split the data first and run the loop for len(X_train). $\endgroup$
    – bkshi
    Feb 8, 2019 at 6:05
  • $\begingroup$ I changed then it gave me an error like ''numpy.ndarray' object has no attribute 'append' ' . I upload my code. If you are okay, can you look at this error and can give me any suggestion to solve this problem? I'm stuck and I can't predict my value properly. $\endgroup$
    – user59020
    Feb 8, 2019 at 6:25
  • $\begingroup$ If it's okay ask a new question with the problem you are facing, as the problem is completely different. $\endgroup$
    – bkshi
    Feb 8, 2019 at 6:29
  • $\begingroup$ sure I will Thank you veru much. I will ask a new question. I hope you will help me to solve this problem. $\endgroup$
    – user59020
    Feb 8, 2019 at 6:31

Your Answer

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