转换numpy数组dtype。 ValueError:使用序列设置数组元素

时间:2022-08-09 21:23:12

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

我得到了一个numpy.array,其dtype = object如下。

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.

但是,我想转换为使用reshape()方法使其dtype = float。

When I try codes as follows,

当我尝试如下代码时,

    fuzz_np.astype(float)

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

我收到错误消息'设置一个带序列的数组元素'。怎么了?

1 个解决方案

#1


1  

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

astype不起作用

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.

对象数组包含指向列表的指针(否则在内存中)。所以astype和view无法将其转换为float数组。相反,我们必须从等效的嵌套列表中创建一个全新的,新的数组。


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

只要大小匹配,tolist也可以在一个或多个元素是数组时起作用

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]]])

#1


1  

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

astype不起作用

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.

对象数组包含指向列表的指针(否则在内存中)。所以astype和view无法将其转换为float数组。相反,我们必须从等效的嵌套列表中创建一个全新的,新的数组。


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

只要大小匹配,tolist也可以在一个或多个元素是数组时起作用

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]]])