-1

I have two lists. Even though the length are the same, the size of them are different. While parameters_train consists of 4 elements in one row, score1 consists of only one. I am trying to make a function that stores the generalization error. Here's the code and the error.

enter image description here

To give you an idea here is preview of lists.

enter image description here

My expected outcome is this.

generalization_error= [0, 0, 0.06, and so on...
diabolik
  • 55
  • 6

1 Answers1

1

As each element in your x is tuple, when you iterate you get a tuple, not integer, so you can use enumerate:

for i,_ in enumerate(x):
    generalization_error.append((x[i][3] - y[i])

This will ensure you get the right index. If you also need the actual tuple along with it you can do

for i, item in enumerate(x):
    generalization_error.append(item[3] - y[i])

You might also need to check your y[i] is a legitimate value, as this comes under assumption that for any given x[i] there's an existing y[i].

dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • I used the first code. But I got "unexpected EOF while parsing" this error. When I used the second code I didn't get an error but generalization_error turned up empty. – diabolik Apr 30 '20 at 14:34
  • You have to check if your lists have same size and you can actually iterate over them, but if they are non empty and you know they have n elements both, you can just use `enumerate`. – dmitryro Apr 30 '20 at 14:36
  • The length of the lists are the same. But parameters_train consists of 4 elements in one row, score1 consists of only one. I used both of the codes you gave but they didn't work. I also updated the question considering the other comments. – diabolik Apr 30 '20 at 14:54