将对象数组转换为float数组

时间:2018-08-28 17:19:31

标签: python-2.7 numpy

我有一个数组数组,如下所示:

segments[:30]
array([array([ 131.2]), array([ 124.1]), 0.23679025210440158,
       array([ 133.65]), array([ 123.3]), 0.3221912760287523,
       array([ 116.7]), array([ 147.7]), 0.24318619072437286,
       array([ 102.3]), array([ 120.55]), 0.07436020392924547,
       array([ 130.25]), array([ 100.5625]), 0.029634355247253552,
       array([ 143.6]), array([ 132.4]), 0.5843092009425164,
       array([ 151.65]), array([ 131.6]), 0.4865431547164917,
       array([ 143.3]), array([ 152.05]), 0.2774583905003965,
       array([ 111.65]), array([ 125.]), 0.23880321211181582,
       array([ 123.1875]), array([ 79.5625]), 0.1562070251966361], dtype=object)

我想摆脱array([ 131.2])并仅提取值131.2

我的预期输出是:

array([131.2, 124.1, 0.23679025210440158,
           133.65, 123.3, 0.3221912760287523,
           116.7,147.7, 0.24318619072437286,
           102.3, 120.55, 0.07436020392924547,....])

我尝试了以下方法:

np.array(segments)

但是它并没有改变我的数据。

2 个答案:

答案 0 :(得分:2)

concatenate使所有元素都变成数组,但是尺寸存在问题。有些是1d,有些是0d:

In [109]: np.concatenate(arr)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-109-062a20dcc2f7> in <module>()
----> 1 np.concatenate(arr)

ValueError: all the input arrays must have same number of dimensions

hstack之所以有效,是因为它首先使用[atleast_1d(_m) for _m in tup]将所有内容转换为一维数组:

In [110]: np.hstack(arr)
Out[110]: 
array([1.31200000e+02, 1.24100000e+02, 2.36790252e-01, 1.33650000e+02,
       1.23300000e+02, 3.22191276e-01, 1.16700000e+02, 1.47700000e+02,
       2.43186191e-01, 1.02300000e+02, 1.20550000e+02, 7.43602039e-02,
       1.30250000e+02, 1.00562500e+02, 2.96343552e-02, 1.43600000e+02,
       1.32400000e+02, 5.84309201e-01, 1.51650000e+02, 1.31600000e+02,
       4.86543155e-01, 1.43300000e+02, 1.52050000e+02, 2.77458391e-01,
       1.11650000e+02, 1.25000000e+02, 2.38803212e-01, 1.23187500e+02,
       7.95625000e+01, 1.56207025e-01])

结果是数字dtype(不是对象)。

处理对象数组需要某种python级别的迭代-除了像reshape这样的有限操作实际上并不操纵元素之外。而且,对象的迭代比列表的迭代慢(但比数字数组的Python级别的迭代快)。


In [113]: [np.atleast_1d(i) for i in arr]   # consistent dimensions
Out[113]: 
[array([131.2]),
 array([124.1]),
 array([0.23679025]),
 array([133.65]),
 array([123.3]),
 ...]

In [116]: [np.asarray(i) for i in arr]  # mixed dimensions
Out[116]: 
[array([131.2]),
 array([124.1]),
 array(0.23679025),
 array([133.65]),
 array([123.3]),...]

内部atleast_1d对尺寸进行一些测试。它也可以与*args一起使用,因此我们可以编写

In [123]: np.atleast_1d(*arr)
Out[123]: 
[array([131.2]),
 array([124.1]),
 array([0.23679025]),
 array([133.65]),
 array([123.3]),
 ...]

因此

np.concatenate(np.atleast_1d(*arr))

定时测试表明@sacul's的“天真”列表理解最快:np.array([i[0] if isinstance(i, np.ndarray) else i for i in segments])

答案 1 :(得分:1)

方法1:列表理解

一种方法是在列表理解为np.arrays时迭代并提取浮点数:

np.array([i[0] if isinstance(i, np.ndarray) else i for i in segments])

哪个返回:

array([1.31200000e+02, 1.24100000e+02, 2.36790252e-01, 1.33650000e+02,
       1.23300000e+02, 3.22191276e-01, 1.16700000e+02, 1.47700000e+02,
       2.43186191e-01, 1.02300000e+02, 1.20550000e+02, 7.43602039e-02,
       1.30250000e+02, 1.00562500e+02, 2.96343552e-02, 1.43600000e+02,
       1.32400000e+02, 5.84309201e-01, 1.51650000e+02, 1.31600000e+02,
       4.86543155e-01, 1.43300000e+02, 1.52050000e+02, 2.77458391e-01,
       1.11650000e+02, 1.25000000e+02, 2.38803212e-01, 1.23187500e+02,
       7.95625000e+01, 1.56207025e-01])

这是一种幼稚但直接的做事方式。但这在非常大的阵列上可能会非常慢。

方法2:重塑

如果您的结构始终与示例相同,即 ie 2个数组,后跟一个浮点数,那么您可以调整数组的形状,从每3个值中提取2个浮点数,然后进行连接数据以相同顺序一起返回:

x = segments.reshape(-1,3)

f = np.concatenate(x[:,[0,1]].flatten()).reshape(-1,2)

l = x[:,2].reshape(-1,1)

np.concatenate((f,l),1).flatten()

哪个返回:

array([131.2, 124.1, 0.23679025210440158, 133.65, 123.3,
       0.3221912760287523, 116.7, 147.7, 0.24318619072437286, 102.3,
       120.55, 0.07436020392924547, 130.25, 100.5625,
       0.029634355247253552, 143.6, 132.4, 0.5843092009425164, 151.65,
       131.6, 0.4865431547164917, 143.3, 152.05, 0.2774583905003965,
       111.65, 125.0, 0.23880321211181582, 123.1875, 79.5625,
       0.1562070251966361], dtype=object)

说明

仅是为了帮助直观了解此处发生的情况,您可以查看一下在重新连接在一起之前提取的重塑数据。

>>> x
array([[array([131.2]), array([124.1]), 0.23679025210440158],
       [array([133.65]), array([123.3]), 0.3221912760287523],
       [array([116.7]), array([147.7]), 0.24318619072437286],
       [array([102.3]), array([120.55]), 0.07436020392924547],
       [array([130.25]), array([100.5625]), 0.029634355247253552],
       [array([143.6]), array([132.4]), 0.5843092009425164],
       [array([151.65]), array([131.6]), 0.4865431547164917],
       [array([143.3]), array([152.05]), 0.2774583905003965],
       [array([111.65]), array([125.]), 0.23880321211181582],
       [array([123.1875]), array([79.5625]), 0.1562070251966361]],
      dtype=object)

>>> f
array([[131.2   , 124.1   ],
       [133.65  , 123.3   ],
       [116.7   , 147.7   ],
       [102.3   , 120.55  ],
       [130.25  , 100.5625],
       [143.6   , 132.4   ],
       [151.65  , 131.6   ],
       [143.3   , 152.05  ],
       [111.65  , 125.    ],
       [123.1875,  79.5625]])
>>> l
array([[0.23679025210440158],
       [0.3221912760287523],
       [0.24318619072437286],
       [0.07436020392924547],
       [0.029634355247253552],
       [0.5843092009425164],
       [0.4865431547164917],
       [0.2774583905003965],
       [0.23880321211181582],
       [0.1562070251966361]], dtype=object)