0

Why do i get the indexError?

I have tried to change the slicing to 2001 but i did not help

inputs = training_data[:-1] #EVERYTHING EXCEPT last values
outputs = training_data[-1] #last value

training_inputs = inputs[:2000]
training_outputs = outputs[:2000]
testing_inputs = inputs[2000:]
testing_outputs = outputs[2000:]

IndexError: invalid index to scalar variable.

4
  • 3
    outputs = training_data[-1] will have a single value so you can not apply slicing on it Commented Sep 18, 2019 at 8:35
  • 1
    Change your line to outputs = training_data[-1:] Commented Sep 18, 2019 at 8:36
  • Share these details please: shape of training and test arrays. Commented Sep 18, 2019 at 8:37
  • Share these details please: shape of training and test arrays and inputs. Perhaps you want to use: inputs = training_data[:, :-1] and outputs = training_data[:,-1]. Commented Sep 18, 2019 at 8:45

2 Answers 2

2

This problem occurs when You are trying to index into a scalar non-iterable value.

>> data = [3, 6, 9]
>> result = data[0] # gives you result=3
>> print(result[0]) # Error
Sign up to request clarification or add additional context in comments.

1 Comment

what is the solution?
1

Because output is not a list, and therefore it is not possible to perform slice operations. If you want output to be a list you can use this trick:

outputs = training_data[-1:]

Comments

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.