How to convert array into special items of structured array and revert it back?

 ̄綄美尐妖づ 提交于 2021-01-29 07:11:50

问题


I want to perform some numpy methods on items of structured array instead of numbers. So, for example, while working with array of integers of shape (4, 3), I need to convert it to array of items of length 3 and perform some operations as it were a single one dimensional array of shape (4,). These conversions itself, unfortunately, looks really complicated for me. Let's take another example:

n, m, r = 2, 3, 4
array = np.arange(n*m).reshape((n,m))
dt = np.dtype(','.join('i'*m))
arr1 = np.array([tuple(x) for x in array], dtype=dt)
>>> arr1
array([(0, 1, 2), (3, 4, 5)],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4')])

Then I call some methods on it, let it for the sake of simplicity be np.tile (but they might be completely different):

arr2 = np.tile(arr1[:,None], r)
>>> arr2
array([[(0, 1, 2), (0, 1, 2), (0, 1, 2), (0, 1, 2)],
       [(3, 4, 5), (3, 4, 5), (3, 4, 5), (3, 4, 5)]],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4')])

I want to convert it into this array instead:

array([[[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]],
       [[3, 4, 5], [3, 4, 5], [3, 4, 5], [3, 4, 5]]]

I have two questions:

  1. How to convert array to arr1 (a bunch of special items) without iteration?
  2. How to convert arr2 (a bunch of special items) back to array of single items I want?

回答1:


numpy provides a helper functions to do this:

>>> n, m, r = 2, 3, 4
>>> array = np.arange(n*m).reshape((n,m))
>>> import numpy.lib.recfunctions as recfunctions
>>> recfunctions.unstructured_to_structured(array, dtype=np.dtype(','.join('i'*m)))
array([(0, 1, 2), (3, 4, 5)],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4')])

And in the other direction:

>>> import numpy.lib.recfunctions as recfunctions
>>> recfunctions.structured_to_unstructured(arr2)
array([[[0, 1, 2],
        [0, 1, 2],
        [0, 1, 2],
        [0, 1, 2]],

       [[3, 4, 5],
        [3, 4, 5],
        [3, 4, 5],
        [3, 4, 5]]], dtype=int32)

In this particular case, if the original array is dtype=np.int32, you could use a view:

>>> array = np.arange(n*m, dtype=np.int32).reshape((n,m))
>>> structured_view = array.view(dtype=np.dtype(','.join('i'*m)))
>>> structured_view
array([[(0, 1, 2)],
       [(3, 4, 5)]], dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4')])

The advantage of a view is that it create a new array. Of course, this can be a disadvantage if you mutate your view and don't expect the original array to change as well.

In the reverse, it doesn't handle the shape you want, but you could always reshape:

>>> arr2.view(dtype=np.int32)
array([[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5, 3, 4, 5, 3, 4, 5]], dtype=int32)

Using views can get tricky, fast.



来源:https://stackoverflow.com/questions/64613276/how-to-convert-array-into-special-items-of-structured-array-and-revert-it-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!