MCPcopy Index your code
hub / github.com/numpy/numpy / apply_along_fields

Function apply_along_fields

numpy/lib/recfunctions.py:1186–1227  ·  view source on GitHub ↗

Apply function 'func' as a reduction across fields of a structured array. This is similar to `numpy.apply_along_axis`, but treats the fields of a structured array as an extra axis. The fields are all first cast to a common type following the type-promotion rules from `numpy.result_

(func, arr)

Source from the content-addressed store, hash-verified

1184
1185@array_function_dispatch(_apply_along_fields_dispatcher)
1186def apply_along_fields(func, arr):
1187 """
1188 Apply function 'func' as a reduction across fields of a structured array.
1189
1190 This is similar to `numpy.apply_along_axis`, but treats the fields of a
1191 structured array as an extra axis. The fields are all first cast to a
1192 common type following the type-promotion rules from `numpy.result_type`
1193 applied to the field's dtypes.
1194
1195 Parameters
1196 ----------
1197 func : function
1198 Function to apply on the "field" dimension. This function must
1199 support an `axis` argument, like `numpy.mean`, `numpy.sum`, etc.
1200 arr : ndarray
1201 Structured array for which to apply func.
1202
1203 Returns
1204 -------
1205 out : ndarray
1206 Result of the reduction operation
1207
1208 Examples
1209 --------
1210 >>> import numpy as np
1211
1212 >>> from numpy.lib import recfunctions as rfn
1213 >>> b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)],
1214 ... dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')])
1215 >>> rfn.apply_along_fields(np.mean, b)
1216 array([ 2.66666667, 5.33333333, 8.66666667, 11. ])
1217 >>> rfn.apply_along_fields(np.mean, b[['x', 'z']])
1218 array([ 3. , 5.5, 9. , 11. ])
1219
1220 """
1221 if arr.dtype.names is None:
1222 raise ValueError('arr must be a structured array')
1223
1224 uarr = structured_to_unstructured(arr)
1225 return func(uarr, axis=-1)
1226 # works and avoids axis requirement, but very, very slow:
1227 #return np.apply_along_axis(func, -1, uarr)
1228
1229def _assign_fields_by_name_dispatcher(dst, src, zero_unassigned=None):
1230 return dst, src

Callers 1

Calls 2

funcFunction · 0.70

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…