Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

np.array - too many values to unpack

I am writing a script to save a part of an image ("submatrix" in the code, reported below) as a tif file. When I run the code, I got the following error:

height, width = np.array(submatrix.shape, dtype = float) / dpi
ValueError: too many values to unpack

If instead of the submatrix I use a random matrix like np.random.random((10, 10)), everything runs fine. Do you spot what I am doing wrong?

Using

import matplotlib.pyplot as plt
import math
import numpy as np

Here's the code section where the error is:

submatrix = im[x_min:x_max, y_min:y_max]
dpi = size_box
height, width = np.array(submatrix.shape, dtype = float) / dpi
like image 433
albus_c Avatar asked Nov 01 '13 10:11

albus_c


People also ask

How do I fix too many values to unpack?

We get this error when there's a mismatch between the number of variables to the amount of values Python receives from a function, list, or other collection. The most straightforward way of avoiding this error is to consider how many values you need to unpack and then have the correct number of available variables.

How do I fix ValueError too many values to unpack expected 3?

The Python "ValueError: too many values to unpack (expected 3) in Python" occurs when the number of variables in the assignment is not the same as the number of values in the iterable. To solve the error, declare exactly as many variables as there are items in the iterable.

What does not enough values to unpack mean?

During a multiple value assignment, the ValueError: not enough values to unpack occurs when either you have fewer objects to assign than variables, or you have more variables than objects. This error caused by the mismatch between the number of values returned and the number of variables in the assignment statement.


2 Answers

The error message tells you what the problem is. There are too many values to unpack. Clearly submatrix.shape has length greater than 2.

I cannot tell why that is so, since I don't know what im is. But take a look at the following output from the interactive prompt:

>>> height, width = np.array([1,2], dtype = float)
>>> height, width = np.array([1,2,3], dtype = float)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: too many values to unpack

Something like that is causing your problem with submatrix fulfilling the role that [1,2,3] does above.

If you strip away all the numpy, this is just a standard sequence unpacking error. The simplest example of that being:

>>> x, y = (1, 2, 3)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: too many values to unpack

When you perform sequence unpacking, the sequences on left and right hand side of the assignment operator must have the same length.

like image 159
David Heffernan Avatar answered Sep 26 '22 15:09

David Heffernan


Any time I run into this, I set the output to a single var and the inspect that var to see what's inside.

>>> blah = np.array(submatrix.shape, dtype = float) / dpi
>>> dir(blah)

98% of the time, I simply miscounted the array size. (i.e. X, Y, and Z) the other 2% is usually due to variable length arrays (which really suck).

Early on, I would never use sequence unpack on the same line as the declaration, just to make debugging easier. I then set a break point on the height, width line to see why it caused a problem.

blah = np.array(submatrix.shape, dtype = float) / dpi
height, width = blah
like image 39
Marcel Wilson Avatar answered Sep 23 '22 15:09

Marcel Wilson