我有一个来自api的数组响应,我得到了这个多边形:
poly = [(0,525),(961,525),(961,1003),(0,1003)]
我需要将每个项目乘以0.35,我的代码就是这样:
poly = ak.Array(poly) * (35/100)
我有一个<class 'awkward1.highlevel.Array'>
对象,然后将其更改为np array
poly = np.array(poly)
但是这被转换为错误的格式
[( 0. , 183.75) (336.35, 183.75) (336.35, 351.05) ( 0. , 351.05)]
我需要我的结果数组为np.array int32
:
[[0, 184], [336, 184], [336, 351], [0, 351]]
有人可以帮我吗?
答案 0 :(得分:1)
poly
数组中的每个子数组的类型均为numpy.void
。直接将numpy.void
转换为numpy.array
是行不通的,即,它没有提供所需的数组形状。
遍历每个numpy.void
数组,将它们转换为元组,然后将它们转换为numpy.array
,同时将float
转换为np.int32
。
这有效,如上所述:
import numpy as np
import awkward1.highlevel as ak
poly = [(0,525),(961,525),(961,1003),(0,1003)]
poly = ak.Array(poly) * (35/100)
poly = np.array(poly)
# Initializing a list, as NumPy was throwing some error that I haven't resolved.
poly_list = list()
for i, tup in enumerate(poly.copy()):
# Converting to tuple and then to np.array to fix an error.
poly_list.append(np.array(tuple(tup)))
# Converting back to a NumPy array.
poly = np.array(poly_list)
print(poly)
# Rounding and converting elements to np.int32 type
poly = poly.astype(np.int32)
我对awkward1
软件包不熟悉,因此这可能是一个hack解决方案。但这有效。
答案 1 :(得分:0)
我做了^^
import numpy as np
import awkward1 as ak
poly = [(0,525),(961,525),(961,1003),(0,1003)]
poly = ak.Array(poly) * (35/100)
print(poly)
print(type(poly))
poly = np.array(poly)
poly = [[int(i), int(j)] for i, j in poly]
print(poly)
print(type(poly))
我找到了与朋友交谈的解决方案。