7. Structured arrays wrapping¶
Taking derivatives on structured numpy arrays is not supported by jax
,
so structured arrays are internally wrapped with StructuredArray
.
7.1. StructuredArray¶
- class lsqfitgp.StructuredArray(array)¶
JAX-friendly imitation of a numpy structured array.
It behaves like a read-only numpy structured array, and you can create a copy with a modified field with a jax-like syntax.
- Parameters:
- arraynumpy array, StructuredArray
A structured array. An array qualifies as structured if
array.dtype.names is not None
.
Notes
The StructuredArray is a readonly view on the input array. When you change the content of a field of the StructuredArray, however, the reference to the original array for that field is lost.
Examples
>>> a = numpy.empty(3, dtype=[('f', float), ('g', float)]) >>> a = StructuredArray(a) >>> a = a.at['f'].set(numpy.arange(3)) is equivalent to >>> a['f'] = numpy.arange(3)
- classmethod from_dataframe(df)¶
Make a Structured array from a DataFrame. Data is not copied.
7.2. Functions¶
- lsqfitgp.asarray(x, dtype=None)¶
Version of numpy.asarray that works with StructuredArray and JAX arrays. If x is not an array already, returns a JAX array if possible.
- lsqfitgp.broadcast(*arrays)¶
Version of numpy.broadcast that works with StructuredArray.
- lsqfitgp.broadcast_arrays(*arrays, **kw)¶
Implementation of numpy.broadcast_arrays for lgp.StructuredArray.
Broadcast any number of arrays against each other.
- Parameters:
- `*args`array_likes
The arrays to broadcast.
- subokbool, optional
If True, then sub-classes will be passed-through, otherwise the returned arrays will be forced to be a base-class array (default).
- Returns:
- broadcastedlist of arrays
These arrays are views on the original arrays. They are typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location. If you need to write to the arrays, make copies first. While you can set the
writable
flag True, writing to a single output value may end up changing more than one location in the output array.Deprecated since version 1.17: The output is currently marked so that if written to, a deprecation warning will be emitted. A future version will set the
writable
flag False so writing to it will raise an error.
See also
broadcast
broadcast_to
broadcast_shapes
Examples
>>> x = np.array([[1,2,3]]) >>> y = np.array([[4],[5]]) >>> np.broadcast_arrays(x, y) [array([[1, 2, 3], [1, 2, 3]]), array([[4, 4, 4], [5, 5, 5]])]
Here is a useful idiom for getting contiguous copies instead of non-contiguous views.
>>> [np.array(a) for a in np.broadcast_arrays(x, y)] [array([[1, 2, 3], [1, 2, 3]]), array([[4, 4, 4], [5, 5, 5]])]
- lsqfitgp.broadcast_to(x, shape, **kw)¶
Implementation of numpy.broadcast_to for lgp.StructuredArray. Broadcast an array to a new shape.
- Parameters:
- arrayarray_like
The array to broadcast.
- shapetuple or int
The shape of the desired array. A single integer
i
is interpreted as(i,)
.- subokbool, optional
If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default).
- Returns:
- broadcastarray
A readonly view on the original array with the given shape. It is typically not contiguous. Furthermore, more than one element of a broadcasted array may refer to a single memory location.
- Raises:
- ValueError
If the array is not compatible with the new shape according to NumPy’s broadcasting rules.
See also
broadcast
broadcast_arrays
broadcast_shapes
Notes
New in version 1.10.0.
Examples
>>> x = np.array([1, 2, 3]) >>> np.broadcast_to(x, (3, 3)) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
- lsqfitgp.unstructured_to_structured(arr, dtype=None, names=None, align=False, copy=False, casting='unsafe')¶
Like
numpy.lib.recfunctions.unstructured_to_structured
, but outputs a StructuredArray.