0

i have a array response from a api and i got this poly:

poly = [(0,525),(961,525),(961,1003),(0,1003)]

i need to multiply each item for 0.35, them my code was it:

poly = ak.Array(poly) * (35/100)

i got a <class 'awkward1.highlevel.Array'> object then i changed it to a np array

poly = np.array(poly)

but this is converted to a wrong format

[(  0.  , 183.75) (336.35, 183.75) (336.35, 351.05) (  0.  , 351.05)]

i need that my resultant array to be this as np.array int32:

[[0, 184], [336, 184], [336, 351], [0, 351]]

some one can help me please?

Gillu13
  • 898
  • 6
  • 11
Jasar Orion
  • 626
  • 7
  • 26

2 Answers2

1

Each of the subarrays in your poly array have type numpy.void. Directly converting the numpy.void into a numpy.array didn't work, i.e., it was not giving the shape of the array that you wanted.

Iterate over each of the numpy.void arrays, convert them to tuples and then convert them to numpy.arrays, while typecasting the floats to np.int32s.

This works, as explained above:

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)

I'm not familiar with the awkward1 package, so this may be a hack solution. But it works.

Nikhil Kumar
  • 1,015
  • 1
  • 9
  • 14
0

i did ^^

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

i found this solution talking to a friend.

Jasar Orion
  • 626
  • 7
  • 26