1

I have an array of floats vec which I want to reshape

vec.shape
>>> (3,)
len(vec[0]) # all 3 rows of vec have 150 columns
>>> 150
np.reshape(vec, (3,150))
>>> ValueError: cannot reshape array of size 3 into shape (3,150)

but I get the error above.

What's going wrong? How can I fix this? Thanks!

7
  • 2
    np.concatenate(vec).reshape(3,150)? Commented Nov 16, 2017 at 17:20
  • vec.dtype yields dtype('O') Commented Nov 16, 2017 at 17:22
  • what is type(vec[0]) ? Commented Nov 16, 2017 at 17:31
  • @B.M. numpy.ndarray Commented Nov 16, 2017 at 17:53
  • @Divakar I get the error ValueError: all the input arrays must have same number of dimensions if I try np.concatenate - which is strange because I have inspected all my vectors and they all have 150 elements of type dtype=float32 Commented Nov 16, 2017 at 17:59

1 Answer 1

3

The vec.shape means that the array has 3 items. But they are dtype object, that is, pointers to items else where in memory.

Apparently the items are arrays themselves. One of the concatenate or stack functions can join them into one array, provided the dimensions match.

I'd suggest printing

[x.shape for x in vec]

to verify the shape. And of course make sure that those sub arrays are not, themselves object dtypes.


In [261]: vec = np.empty(3, object)
In [262]: vec[:] = [np.arange(10), np.ones(10), np.zeros(10)]
In [263]: vec
Out[263]: 
array([array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
       array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]),
       array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])], dtype=object)
In [264]: vec.reshape(3,10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-264-cd555975140c> in <module>()
----> 1 vec.reshape(3,10)

ValueError: cannot reshape array of size 3 into shape (3,10)
In [265]: [x.shape for x in vec]
Out[265]: [(10,), (10,), (10,)]
In [266]: np.stack(vec)
Out[266]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
In [267]: np.concatenate(vec)
Out[267]: 
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.,  1.,  1.,  1.,
        1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])
In [268]: np.concatenate(vec).reshape(3,10)
Out[268]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

len is not a good test; use shape. For example if I change one array to be 2d

In [269]: vec[1]=np.ones((10,1))
In [270]: vec
Out[270]: 
array([array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
       array([[ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.],
       [ 1.]]),
       array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])], dtype=object)
In [271]: [len(x) for x in vec]
Out[271]: [10, 10, 10]
In [272]: [x.shape for x in vec]
Out[272]: [(10,), (10, 1), (10,)]
In [273]: np.concatenate(vec)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-273-a253d8b9b25d> in <module>()
----> 1 np.concatenate(vec)

ValueError: all the input arrays must have same number of dimensions
Sign up to request clarification or add additional context in comments.

Comments

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.