0

I got a numpy.array whose dtype=object as follows.

fuzz_np = fuzz_df.values
fuzz_np

with results are:

array([[[0.31250000000000044, 0.68749999999999956, 0.0], [0.0, 0.0, 1.0],
    [1.0, 0.0, 0.0], [1.0, 5.2867763077388416e-17, 0.0]],
   [[0.75000000000000044, 0.24999999999999958, 0.0], [1.0, 0.0, 0.0],
    [0.30769230769230765, 0.69230769230769229, 0.0],
    [0.14285714285714257, 0.85714285714285743, 0.0]],
   [[0.0, 0.81250000000000078, 0.18749999999999983],
    [0.33333333333333331, 0.66666666666666663, 0.0],
    [0.0, 0.76923076923076894, 0.23076923076923067],
    [0.0, 0.85714285714285698, 0.14285714285714279]],
   [[0.5625, 0.43749999999999994, 0.0],
    [0.0, 0.13333333333333344, 0.86666666666666659],
    [0.96153846153846168, 0.038461538461538415, 0.0],
    [0.80952380952380942, 0.19047619047619058, 0.0]],
   [[0.0, 5.5511151231257807e-16, 1.0],
    [0.0, 0.26666666666666689, 0.73333333333333306], [0.0, 0.0, 1.0],
    [0.0, 0.28571428571428553, 0.71428571428571441]]], dtype=object)

However, I wanna convert to make its dtype=float for using reshape() method.

When I try codes as follows,

    fuzz_np.astype(float)

I get error message 'setting an array element with a sequence.' What's wrong?

8
  • 1
    What's the result of fuzz_np.dtype? Commented May 9, 2017 at 7:52
  • Isn't it weird that the first element is an array while the others are lists? Commented May 9, 2017 at 8:05
  • 1
    What's its shape? I'm guessing 5x4. But it contains one array, and the rest 3 element lists. You may have convert the embedded array to list, and the outer tolist as well. Commented May 9, 2017 at 8:05
  • dtype=object allows for this non-homogeneous shape. if you want it to be float you need a uniform shape. You can read this up here. Commented May 9, 2017 at 8:18
  • @Kasramvd It is dtype('O') Commented May 10, 2017 at 1:14

1 Answer 1

2

Make an object array and fill it with lists:

In [410]: arr = np.zeros(6,object)
In [411]: for i in range(6): arr[i]=[1,2,3]
In [413]: arr=arr.reshape(2,3)
In [414]: arr
Out[414]: 
array([[[1, 2, 3], [1, 2, 3], [1, 2, 3]],
       [[1, 2, 3], [1, 2, 3], [1, 2, 3]]], dtype=object)

astype does not work

In [415]: arr.astype(float)

but a list intermediary does:

In [416]: np.array(arr.tolist())
Out[416]: 
array([[[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]],

       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]]])

The object array contains pointers to lists (else where in memory). So astype and view cannot convert that to a float array. Instead we have to make a whole new, fresh, array from the equivalent nested list.


tolist also works when one or more of the elements is an array, as long as sizes match

In [417]: arr[0,0]=np.arange(3)
In [418]: arr
Out[418]: 
array([[array([0, 1, 2]), [1, 2, 3], [1, 2, 3]],
       [[1, 2, 3], [1, 2, 3], [1, 2, 3]]], dtype=object)
In [419]: arr.tolist()
Out[419]: [[array([0, 1, 2]), [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]]]
In [420]: np.array(arr.tolist())
Out[420]: 
array([[[0, 1, 2],
        [1, 2, 3],
        [1, 2, 3]],

       [[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]]])
Sign up to request clarification or add additional context in comments.

1 Comment

It really works. Your analysis is deep and accuracy. Thanks guy.

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.