Python – ValueError : setting an array element with a sequence

ValueError : setting an array element with a sequence… here is a solution to the problem.

ValueError : setting an array element with a sequence

The following code is written using Python 3.6. It should create a final matrix containing binary vectors. During the loop, each batch is taken from a continuous vector for use by method simulate_output(), but the final batch is flattened into a single vector:

tmp_list = []
output = []
num_components = 4
dim = 16
total_vectors = 100000

X = np.random.choice(np.array([0, 1], dtype=np.uint8), size=(total_vectors, dim))
num_vectors = np.unique(X, axis=0)

for i in range(0, len(num_vectors), num_components):
    batch = num_vectors[i:(i + num_components)]
    # output.append(simulate_output(batch)) # Comment this line will not solve the error.
    batch = np.hstack(batch)  # to flatten the list into a single vector
    tmp_list.append(batch)

final_matrix = np.array(tmp_list, dtype=np.int8)
print(final_matrix)

For some runs I get this error :

Traceback (most recent call last):
  File "test.py", line 65, in <module>
    final_matrix = np.array(tmp_list, dtype=np.int8)
ValueError: setting an array element with a sequence.

I

believe the error is in the last line final_matrix=np.array(tmp_list, dtype=np.int8) but I don’t know why and how to fix it, because in some runs it works at the same time in others it doesn’t.

Thanks

Solution

I found your problem.
In this line:

final_matrix = np.array(tmp_list, dtype=np.int8)

You want the final_matrix to be a two-dimensional numpy array. This may be the case if all rows have the same length, but it’s not exactly your case. Your last flattened batch vector is shorter because len(num_vectors) is not divided by num_components(4).

If you simply say:

tmp_list = tmp_list[:-1]

After the for loop, everything will be fine. I think that one thousandth of an element is negligible. If you still don’t want to remove it, try filling it with zeros to reach the desired size – num_components * dim.

Related Problems and Solutions