MCPcopy
hub / github.com/numpy/numpy / structured_to_unstructured

Function structured_to_unstructured

numpy/lib/recfunctions.py:940–1067  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

938
939@array_function_dispatch(_structured_to_unstructured_dispatcher)
940def 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

Callers 3

inspectMethod · 0.90
apply_along_fieldsFunction · 0.85

Calls 9

_get_fields_and_offsetsFunction · 0.85
allFunction · 0.85
_common_strideFunction · 0.85
sumFunction · 0.85
wrapFunction · 0.85
astypeMethod · 0.80
minFunction · 0.50
dtypeMethod · 0.45
viewMethod · 0.45

Tested by 2

inspectMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…