Converts an n-D structured array into an (n+1)-D unstructured array. The new array will have a new last dimension equal in size to the number of field-elements of the input array. If not supplied, the output datatype is determined from the numpy type promotion rules applied to all
(arr, dtype=None, copy=False, casting='unsafe')
| 938 | |
| 939 | @array_function_dispatch(_structured_to_unstructured_dispatcher) |
| 940 | def structured_to_unstructured(arr, dtype=None, copy=False, casting='unsafe'): |
| 941 | """ |
| 942 | Converts an n-D structured array into an (n+1)-D unstructured array. |
| 943 | |
| 944 | The new array will have a new last dimension equal in size to the |
| 945 | number of field-elements of the input array. If not supplied, the output |
| 946 | datatype is determined from the numpy type promotion rules applied to all |
| 947 | the field datatypes. |
| 948 | |
| 949 | Nested fields, as well as each element of any subarray fields, all count |
| 950 | as a single field-elements. |
| 951 | |
| 952 | Parameters |
| 953 | ---------- |
| 954 | arr : ndarray |
| 955 | Structured array or dtype to convert. Cannot contain object datatype. |
| 956 | dtype : dtype, optional |
| 957 | The dtype of the output unstructured array. |
| 958 | copy : bool, optional |
| 959 | If true, always return a copy. If false, a view is returned if |
| 960 | possible, such as when the `dtype` and strides of the fields are |
| 961 | suitable and the array subtype is one of `numpy.ndarray`, |
| 962 | `numpy.recarray` or `numpy.memmap`. |
| 963 | |
| 964 | .. versionchanged:: 1.25.0 |
| 965 | A view can now be returned if the fields are separated by a |
| 966 | uniform stride. |
| 967 | |
| 968 | casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional |
| 969 | See casting argument of `numpy.ndarray.astype`. Controls what kind of |
| 970 | data casting may occur. |
| 971 | |
| 972 | Returns |
| 973 | ------- |
| 974 | unstructured : ndarray |
| 975 | Unstructured array with one more dimension. |
| 976 | |
| 977 | Examples |
| 978 | -------- |
| 979 | >>> import numpy as np |
| 980 | |
| 981 | >>> from numpy.lib import recfunctions as rfn |
| 982 | >>> a = np.zeros(4, dtype=[('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)]) |
| 983 | >>> a |
| 984 | array([(0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.]), |
| 985 | (0, (0., 0), [0., 0.]), (0, (0., 0), [0., 0.])], |
| 986 | dtype=[('a', '<i4'), ('b', [('f0', '<f4'), ('f1', '<u2')]), ('c', '<f4', (2,))]) |
| 987 | >>> rfn.structured_to_unstructured(a) |
| 988 | array([[0., 0., 0., 0., 0.], |
| 989 | [0., 0., 0., 0., 0.], |
| 990 | [0., 0., 0., 0., 0.], |
| 991 | [0., 0., 0., 0., 0.]]) |
| 992 | |
| 993 | >>> b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)], |
| 994 | ... dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')]) |
| 995 | >>> np.mean(rfn.structured_to_unstructured(b[['x', 'z']]), axis=-1) |
| 996 | array([ 3. , 5.5, 9. , 11. ]) |
| 997 |
searching dependent graphs…