6

I have an object array which looks something like this

array([array([[2.4567]],dtype=object), array([[3.4567]],dtype=object), array([[4.4567]],dtype=object), array([[5.4567]],dtype=object) ... array([[6.4567]],dtype=object))

This is just an example, actual one is much bigger.

So, how do I convert this into a normal floating value numpy array.

1
  • b = np.array([float(i) for i in arr])[:, np.newaxis] might work Commented Jun 5, 2015 at 12:34

4 Answers 4

14

Use numpy.concatenate:

>>> arr = array([array([[2.4567]],dtype=object),array([[3.4567]],dtype=object),array([[4.4567]],dtype=object),array([[5.4567]],dtype=object),array([[6.4567]], dtype=object)])
>>> np.concatenate(arr).astype(None)
array([[ 2.4567],
       [ 3.4567],
       [ 4.4567],
       [ 5.4567],
       [ 6.4567]])
Sign up to request clarification or add additional context in comments.

3 Comments

what if the above array I mentioned is just the first row of a multi dimensional array and there are several rows. "np.concatenate" joins all of them into a 1d array which I do not want. So how to concatenate them rowwise without using for loops?
I have the same problem as @Shashank. The above method does not work for N x M arrays.
@Darcy then ask a new question instead of downvoting existing ones. o_O
2

You can use np.stack, works also for the multi dimensional case:

import numpy as np
from numpy import array

arr = array([array([[2.4567]],dtype=object),array([[3.4567]],dtype=object),array([[4.4567]],dtype=object),array([[5.4567]],dtype=object),array([[6.4567]],
dtype=object)])
np.stack(arr).astype(None)
array([[[2.4567]],
       [[3.4567]],
       [[4.4567]],
       [[5.4567]],
       [[6.4567]]])

Comments

0

Or, using reshape:

In [1]: a = array([array([[2.4567]],dtype=object), array([[3.4567]],dtype=object), array([[4.4567]],dtype=object)])
In [2]: a.astype(float).reshape(a.size,1)
Out[2]:
array([[ 2.4567],
       [ 3.4567],
       [ 4.4567]])

Comments

0

Solution for arbitrary number of dimensions:

As of now, all other answers fail for multi-dimensional cases, so I came up with a general function. It converts an object array (of arbitrary shape) of same-shape numpy arrays (of arbritrary dimensions) to a normal Numpy array:

def obj_array_to_type(arr, typ):
    """
    Convert an object array of same-sized arrays to a normal 3D array
    with dtype=typ. This is a workaround as numpy doesn't realize that
    the object arrays are numpy arrays of the same legth, so just using
    array.astype(typ) fails. Technically works if the items are numbers
    and not arrays, but then `arr.astype(typ)` should be used.
    """
    full_shape = (*arr.shape, *np.shape(arr.flat[0]))
    return np.vstack(arr.flatten()).astype(typ).reshape(full_shape)

Explanation:

  1. First we expand the original dimensions of the object array with the dimensions of the very first item of the object array (arr.flat[0]).
  2. We flatten the array and then apply np.vstack on it, as other answers have shown.
  3. We apply the type, and then reshape the array to the full size.

As every function only modifies numpy's view on the array, the actual content is never copied/duplicated, so this is also fast on very huge arrays.

Example:

>>> # Just generate an object array of arrays
>>> a = np.empty((3,3), dtype=object)
>>> for i in range(9): 
...     a[i//3, i%3] = np.array([1,2,3,4])
... 
>>> a
array([[array([1, 2, 3, 4]), array([1, 2, 3, 4]), array([1, 2, 3, 4])],
       [array([1, 2, 3, 4]), array([1, 2, 3, 4]), array([1, 2, 3, 4])],
       [array([1, 2, 3, 4]), array([1, 2, 3, 4]), array([1, 2, 3, 4])]],
      dtype=object)
>>> # This will fail:
>>> a.astype(float)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
>>> obj_array_to_type(a, float)
array([[[1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.]],

       [[1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.]],

       [[1., 2., 3., 4.],
        [1., 2., 3., 4.],
        [1., 2., 3., 4.]]])

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.