How to fix IndexError: invalid index to scalar variable

We Are Going To Discuss About How to fix IndexError: invalid index to scalar variable. So lets Start this Python Article.

How to fix IndexError: invalid index to scalar variable

How to solve How to fix IndexError: invalid index to scalar variable

You are trying to index into a scalar (non-iterable) value:
[y[1] for y in y_test] # ^ this is the problem
When you call [y for y in test] you are iterating over the values already, so you get a single value in y.
Your code is the same as trying to do the following:
y_test = [1, 2, 3] y = y_test[0] # y = 1 print(y[0]) # this line will fail
I’m not sure what you’re trying to get into your results array, but you need to get rid of [y[1] for y in y_test].
If you want to append each y in y_test to results, you’ll need to expand your list comprehension out further to something like this:
[results.append(..., y) for y in y_test]
Or just use a for loop:
for y in y_test: results.append(..., y)

How to fix IndexError: invalid index to scalar variable

You are trying to index into a scalar (non-iterable) value:
[y[1] for y in y_test] # ^ this is the problem
When you call [y for y in test] you are iterating over the values already, so you get a single value in y.
Your code is the same as trying to do the following:
y_test = [1, 2, 3] y = y_test[0] # y = 1 print(y[0]) # this line will fail
I’m not sure what you’re trying to get into your results array, but you need to get rid of [y[1] for y in y_test].
If you want to append each y in y_test to results, you’ll need to expand your list comprehension out further to something like this:
[results.append(..., y) for y in y_test]
Or just use a for loop:
for y in y_test: results.append(..., y)

Solution 1

You are trying to index into a scalar (non-iterable) value:

[y[1] for y in y_test]
#  ^ this is the problem

When you call [y for y in test] you are iterating over the values already, so you get a single value in y.

Your code is the same as trying to do the following:

y_test = [1, 2, 3]
y = y_test[0] # y = 1
print(y[0]) # this line will fail

I’m not sure what you’re trying to get into your results array, but you need to get rid of [y[1] for y in y_test].

If you want to append each y in y_test to results, you’ll need to expand your list comprehension out further to something like this:

[results.append(..., y) for y in y_test]

Or just use a for loop:

for y in y_test:
    results.append(..., y)

Original Author Monkpit Of This Content

Solution 2

YOLO Object Detection

layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

Don’t need to indexing i in layer_names[i[0] – 1] . Just remove it and do layer_names[i – 1]

layer_names = net.getLayerNames() output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

It Work For Me

Original Author Tejas Veer Of This Content

Solution 3

Basically, 1 is not a valid index of y. If the visitor is coming from his own code he should check if his y contains the index which he tries to access (in this case the index is 1).

Original Author gies0r Of This Content

Solution 4

YOLO Object Detection

python <= 3.7

ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]

python >3.7

ln = net.getLayerNames() 
ln = [ln[i - 1] for i in net.getUnconnectedOutLayers()]

Original Author Bharath Kumar Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment