How to set an array element with a sequence?

How to set an array element with a sequence?

Hey Priyanka,

Possible Reason 1: Trying to Create a Jagged Array

You may be trying to create an array from a list that isn’t shaped like a multi-dimensional array:

numpy.array([[1, 2], [2, 3, 4]])         # Wrong!
numpy.array([[1, 2], [2, [3, 4]]])       # Wrong!

In these examples, the argument to numpy.array contains sequences of different lengths. This will yield an error message because the input list is not shaped like a “box” that can be turned into a multi-dimensional array.

Possible Reason 2: Providing Elements of Incompatible Types

For example, providing a string as an element in an array of type float:

numpy.array([1.2, "abc"], dtype=float)   # Wrong!

If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which allows the array to hold arbitrary Python objects:

numpy.array([1.2, "abc"], dtype=object)

Dear Prunka.hans,

The Python ValueError:

ValueError: setting an array element with a sequence.

This error means you’re trying to cram a sequence of numbers into a single number slot. It can occur under various circumstances.

1. Passing a Python Tuple or List as a NumPy Array Element:

import numpy

numpy.array([1, 2, 3])              # Good

numpy.array([1, (2, 3)])            # Fail, can't convert a tuple into a NumPy 
                                    # array element

numpy.mean([5, (6+7)])              # Good

numpy.mean([5, tuple(range(2))])    # Fail, can't convert a tuple into a NumPy 
                                    # array element

def foo():
    return 3

numpy.array([2, foo()])             # Good

def foo():
    return [3, 4]

numpy.array([2, foo()])             # Fail, can't convert a list into a NumPy 
                                    # array element

2. Cramming a NumPy Array of Length > 1 into a Single NumPy Array Element:

x = numpy.array([1, 2, 3])
x[0] = numpy.array([4])            # Good

x = numpy.array([1, 2, 3])
x[0] = numpy.array([4, 5])         # Fail, can't convert the NumPy array to fit 
                                   # into a single array element

When creating a NumPy array, NumPy doesn’t know how to cram multi-valued tuples or arrays into single element slots. It expects each element to evaluate to a single number. If it doesn’t, NumPy responds with an error, indicating it doesn’t know how to set an array element with a sequence.

Hello Prynka,

In my case, I encountered this error in TensorFlow because I was trying to feed an array with different lengths or sequences:

Example:

import tensorflow as tf

input_x = tf.placeholder(tf.int32, [None, None])

word_embedding = tf.get_variable('embedding', shape=[len(vocab_), 110], dtype=tf.float32, initializer=tf.random_uniform_initializer(-0.01, 0.01))

embedding_look = tf.nn.embedding_lookup(word_embedding, input_x)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    a, b = sess.run([word_embedding, embedding_look], feed_dict={input_x: example_array})
    print(b)

If my array is:

example_array = [[1, 2, 3], [1, 2]]

Then I will get the error:

ValueError: setting an array element with a sequence.

But if I do padding, like this:

example_array = [[1, 2, 3], [1, 2, 0]]

Now it works.