0

I have a defaultdict which maps certain integers to a numpy array of size 20.

In addition, I have an existing array of indices. I want to turn that array of indices into a 2D array, where each original index is converted into an array via my defaultdict.

Finally, in the case that an index isn't found in the defaultdict, I want to create an array of zeros for that index.

Here's what I have so far

    converter = lambda x: np.zeros((d), dtype='float32') if x == -1 else cVf[x]
    vfunc = np.vectorize(converter)
    cvf = vfunc(indices)

np.zeros((d), dtype='float32') and cVf[x] are identical data types/ shapes:

(Pdb) np.shape(cVf[0])
(20,)

Yet I get the error in the title (*** ValueError: setting an array element with a sequence.) when I try to run this code.

Any ideas?

1 Answer 1

1

You should give us a some sample arrays or dictionaries (in the case of cVF, so we can make a test run.

Read what vectorize has to say about the return value. Since you don't define otypes, it makes a test calculation to determine the dtype of the returned array. My first thought was that the test calc and subsequent one might be returning different things. But you claim converter will always be returning the same dtype and shape array.

But let's try something simpler:

In [609]: fv = np.vectorize(lambda x: np.array([x,x]))
In [610]: fv([1,2,3])
...
ValueError: setting an array element with a sequence.

It's having trouble with returning any array.

But if I give an otypes, it works

In [611]: fv = np.vectorize(lambda x: np.array([x,x]), otypes=[object])
In [612]: fv([1,2,3])
Out[612]: array([array([1, 1]), array([2, 2]), array([3, 3])], dtype=object)

In fact in this case I could use frompyfunc, which returns object dtype, and is the underlying function for vectorize (and a bit faster).

In [613]: fv = np.frompyfunc(lambda x: np.array([x,x]), 1,1)
In [614]: fv([1,2,3])
Out[614]: array([array([1, 1]), array([2, 2]), array([3, 3])], dtype=object)

vectorize and frompyfunc are designed for functions that are scalar in- scalar out. That scalar may be an object, even array, but is still treated as a scalar.

1
  • This worked perfectly. I particularly appreciate your in depth response despite my post lacking sample variables. Thanks! Commented Jun 19, 2017 at 5:32

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.