Converts an n-D unstructured array into an (n-1)-D structured array. The last dimension of the input array is converted into a structure, with number of field-elements equal to the size of the last dimension of the input array. By default all output fields have the input array's dt
(arr, dtype=None, names=None, align=False,
copy=False, casting='unsafe')
| 1073 | |
| 1074 | @array_function_dispatch(_unstructured_to_structured_dispatcher) |
| 1075 | def unstructured_to_structured(arr, dtype=None, names=None, align=False, |
| 1076 | copy=False, casting='unsafe'): |
| 1077 | """ |
| 1078 | Converts an n-D unstructured array into an (n-1)-D structured array. |
| 1079 | |
| 1080 | The last dimension of the input array is converted into a structure, with |
| 1081 | number of field-elements equal to the size of the last dimension of the |
| 1082 | input array. By default all output fields have the input array's dtype, but |
| 1083 | an output structured dtype with an equal number of fields-elements can be |
| 1084 | supplied instead. |
| 1085 | |
| 1086 | Nested fields, as well as each element of any subarray fields, all count |
| 1087 | towards the number of field-elements. |
| 1088 | |
| 1089 | Parameters |
| 1090 | ---------- |
| 1091 | arr : ndarray |
| 1092 | Unstructured array or dtype to convert. |
| 1093 | dtype : dtype, optional |
| 1094 | The structured dtype of the output array |
| 1095 | names : list of strings, optional |
| 1096 | If dtype is not supplied, this specifies the field names for the output |
| 1097 | dtype, in order. The field dtypes will be the same as the input array. |
| 1098 | align : boolean, optional |
| 1099 | Whether to create an aligned memory layout. |
| 1100 | copy : bool, optional |
| 1101 | See copy argument to `numpy.ndarray.astype`. If true, always return a |
| 1102 | copy. If false, and `dtype` requirements are satisfied, a view is |
| 1103 | returned. |
| 1104 | casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional |
| 1105 | See casting argument of `numpy.ndarray.astype`. Controls what kind of |
| 1106 | data casting may occur. |
| 1107 | |
| 1108 | Returns |
| 1109 | ------- |
| 1110 | structured : ndarray |
| 1111 | Structured array with fewer dimensions. |
| 1112 | |
| 1113 | Examples |
| 1114 | -------- |
| 1115 | >>> import numpy as np |
| 1116 | |
| 1117 | >>> from numpy.lib import recfunctions as rfn |
| 1118 | >>> dt = np.dtype([('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)]) |
| 1119 | >>> a = np.arange(20).reshape((4,5)) |
| 1120 | >>> a |
| 1121 | array([[ 0, 1, 2, 3, 4], |
| 1122 | [ 5, 6, 7, 8, 9], |
| 1123 | [10, 11, 12, 13, 14], |
| 1124 | [15, 16, 17, 18, 19]]) |
| 1125 | >>> rfn.unstructured_to_structured(a, dt) |
| 1126 | array([( 0, ( 1., 2), [ 3., 4.]), ( 5, ( 6., 7), [ 8., 9.]), |
| 1127 | (10, (11., 12), [13., 14.]), (15, (16., 17), [18., 19.])], |
| 1128 | dtype=[('a', '<i4'), ('b', [('f0', '<f4'), ('f1', '<u2')]), ('c', '<f4', (2,))]) |
| 1129 | |
| 1130 | """ # noqa: E501 |
| 1131 | if arr.shape == (): |
| 1132 | raise ValueError('arr must have at least one dimension') |
searching dependent graphs…