to_xarray#
- pymc_marketing.clv.utils.to_xarray(customer_id, *arrays, dim='customer_id', extra_dims=None)[source]#
Convert arrays to xarray objects sharing a common dim (default “customer_id”).
The leading axis of every array is labelled
dimand takescustomer_idas its coordinate. Arrays with more than one axis are supported, but the names of their trailing axes must be supplied throughextra_dims. Auto-generated names are deliberately not provided: they would make the unrelated trailing axes of two arrays returned by the same call align with each other.For the same reason every array must be one-dimensional or carry the full set of trailing axes; a rank in between is rejected rather than guessed at, since
extra_dimswould not say which of its names the missing axis dropped. Arrays with different trailing axes need one call each.- Parameters:
- customer_idarray_like
Coordinate values for
dim. Must match the length of the leading axis of every array.- *arraysarray_like
Arrays to convert. Each must be one-dimensional or have
1 + len(extra_dims)dimensions.- dim
str, default “customer_id” Name of the leading dimension.
- extra_dimssequence of
str, optional Names of the trailing dimensions, e.g.
("channel",). Required as soon as an array is multidimensional. A bare string is rejected: it would be read as one name per character.
- Returns:
xarray.DataArrayortupleofxarray.DataArrayOne
DataArrayper entry inarrays, or a bareDataArraywhen a single array is passed.
- Raises:
TypeErrorIf
extra_dimsis a string rather than a sequence of names.ValueErrorIf
extra_dimsrepeats a name or clashes withdim, or if an array is neither one-dimensional nor of dimension1 + len(extra_dims).
Examples
frequency, recency = to_xarray( data["customer_id"], data["frequency"], data["recency"] ) spend = to_xarray(data["customer_id"], channel_matrix, extra_dims=("channel",))
Trailing dimensions are named but not populated; attach their labels with
assign_coords:spend = to_xarray( data["customer_id"], channel_df, extra_dims=("channel",) ).assign_coords(channel=channel_df.columns)