dimod.as_samples#
- as_samples(samples_like: SamplesLike, dtype: Optional[DTypeLike] = None, copy: bool = False, order: ArrayOrder = 'C', labels_type: type = <class 'list'>) tuple[np.ndarray, collections.abc.Sequence[Variable]] [source]#
- as_samples(samples_like: collections.abc.Iterator[SampleLike], labels_type: type = <class 'list'>, **kwargs) tuple[np.ndarray, collections.abc.Sequence[Variable]]
- as_samples(samples_like: collections.abc.Mapping[Variable, float], dtype: Optional[DTypeLike] = None, copy: bool = False, order: ArrayOrder = 'C', labels_type: type = <class 'list'>) tuple[np.ndarray, collections.abc.Sequence[Variable]]
- as_samples(samples_like: tuple[ArrayLike, collections.abc.Sequence[Variable]], dtype: Optional[DTypeLike] = None, copy: bool = False, order: ArrayOrder = 'C', labels_type: type = <class 'list'>) tuple[np.ndarray, collections.abc.Sequence[Variable]]
- as_samples(samples_like: ~dimod.sampleset.SampleSet, dtype: ~numpy.dtype[~typing.Any] | None | type[~typing.Any] | ~numpy._typing._dtype_like._SupportsDType[~numpy.dtype[~typing.Any]] | str | tuple[~typing.Any, int] | tuple[~typing.Any, ~typing.SupportsIndex | ~collections.abc.Sequence[~typing.SupportsIndex]] | list[~typing.Any] | ~numpy._typing._dtype_like._DTypeDict | tuple[~typing.Any, ~typing.Any] = None, copy: bool = False, order: ~typing.Literal['K', 'A', 'C', 'F'] = 'C', labels_type: type = <class 'list'>) tuple[ndarray, list[Hashable]]
Convert a samples_like object to a NumPy array and list of labels.
- Parameters:
samples_like – A collection of raw samples. samples_like is an extension of NumPy’s array_like structure. See examples below.
dtype – dtype for the returned samples array. If not provided, it is either derived from samples_like, if that object has a dtype, or set to the smallest dtype that can hold the given values.
copy – If true, then samples_like is guaranteed to be copied, otherwise it is only copied if necessary.
order – Specify the memory layout of the array. See
numpy.array()
.labels_type – The return type of the variables labels.
labels_type
should be aSequence
. Thelabels_type
constructor should accept zero arguments, or an iterable as a single argument.
- Returns:
A 2-tuple containing the samples as a
ndarray
and the variables labels, as alabels_type
.
Examples
The following examples convert a variety of samples_like objects:
NumPy arrays
>>> import numpy as np ... >>> dimod.as_samples(np.ones(5, dtype='int8')) (array([[1, 1, 1, 1, 1]], dtype=int8), [0, 1, 2, 3, 4]) >>> dimod.as_samples(np.zeros((5, 2), dtype='int8')) (array([[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], dtype=int8), [0, 1])
Lists
>>> dimod.as_samples([-1, +1, -1]) (array([[-1, 1, -1]], dtype=int8), [0, 1, 2]) >>> dimod.as_samples([[-1], [+1], [-1]]) (array([[-1], [ 1], [-1]], dtype=int8), [0])
Dicts
>>> dimod.as_samples({'a': 0, 'b': 1, 'c': 0}) (array([[0, 1, 0]], dtype=int8), ['a', 'b', 'c']) >>> dimod.as_samples([{'a': -1, 'b': +1}, {'a': 1, 'b': 1}]) (array([[-1, 1], [ 1, 1]], dtype=int8), ['a', 'b'])
A 2-tuple containing an array_like object and a list of labels
>>> dimod.as_samples(([-1, +1, -1], ['a', 'b', 'c'])) (array([[-1, 1, -1]], dtype=int8), ['a', 'b', 'c']) >>> dimod.as_samples((np.zeros((5, 2), dtype='int8'), ['in', 'out'])) (array([[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], dtype=int8), ['in', 'out'])
Deprecated since version 0.10.13: Support for a 2-tuple of
(dict, list)
as a samples-like will be removed in dimod 0.12.0.